-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
IDCardRecognition.java
55 lines (44 loc) · 1.13 KB
/
IDCardRecognition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.ansj.recognition.impl;
import org.ansj.domain.Nature;
import org.ansj.domain.Result;
import org.ansj.domain.Term;
import org.ansj.recognition.Recognition;
import java.util.Iterator;
import java.util.List;
/**
* 基于规则的新词发现,身份证号码识别
*
* @author ansj
*
*/
public class IDCardRecognition implements Recognition {
/**
*
*/
private static final long serialVersionUID = -32133440735240290L;
private static final Nature ID_CARD_NATURE = new Nature("idcard");
@Override
public void recognition(Result result) {
List<Term> terms = result.getTerms() ;
for (Term term : terms) {
if ("m".equals(term.getNatureStr())) {
if (term.getName().length() == 18) {
term.setNature(ID_CARD_NATURE);
} else if (term.getName().length() == 17) {
Term to = term.to();
if ("x".equals(to.getName())) {
term.merage(to);
to.setName(null);
term.setNature(ID_CARD_NATURE);
}
}
}
}
for (Iterator<Term> iterator = terms.iterator(); iterator.hasNext();) {
Term term = iterator.next();
if (term.getName() == null) {
iterator.remove();
}
}
}
}