Skip to content

Commit 8a3e87e

Browse files
committed
添加了汲取相关词汇的办法
1 parent dc0f7cc commit 8a3e87e

1 file changed

Lines changed: 45 additions & 8 deletions

File tree

java/CharTree.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package test_jcseg;
22

3+
import java.util.ArrayList;
34
import java.util.Arrays;
45
import java.util.Comparator;
6+
import java.util.List;
57

68
public class Test {
79

10+
private static final CharNode NOT_CONTAIN_NODE = new CharNode(' ', null);
11+
private static final CharNode CONTAIN_BUT_NOT_WORD_NODE = new CharNode(' ',
12+
null);
13+
814
public static void main(String[] args) {
915
CharTree charTree = new CharTree();
1016
charTree.appendWord("abc");
@@ -13,6 +19,9 @@ public static void main(String[] args) {
1319
System.out.println(charTree.containWord("abc"));
1420
System.out.println(charTree.containWord("abd"));
1521
System.out.println(charTree.containWord("ab"));
22+
for (String str : charTree.listContainWords("feoejfoabcjfelfjlabd")) {
23+
System.out.println(str);
24+
}
1625
}
1726

1827
public static class CharTree {
@@ -22,6 +31,24 @@ public static class CharTree {
2231
public CharTree() {
2332
}
2433

34+
public List<String> listContainWords(String str) {
35+
List<String> wordList = new ArrayList<String>();
36+
char[] charArray = str.toCharArray();
37+
for (int i = 0; i < charArray.length; i++) {
38+
for (int j = i + 1; j <= charArray.length; j++) {
39+
String substring = str.substring(i, j);
40+
ContainResult containResult = this.containWord(substring);
41+
if (containResult.equals(ContainResult.NOT_CONTAIN)) {
42+
break;
43+
}
44+
if (containResult.equals(ContainResult.CONTAIN)) {
45+
wordList.add(substring);
46+
}
47+
}
48+
}
49+
return wordList;
50+
}
51+
2552
public void appendWord(String word) {
2653
CharNode currentCharNode = root;
2754
char[] charArray = word.toCharArray();
@@ -67,7 +94,7 @@ public int compare(CharNode o1, CharNode o2) {
6794
return currentCharNode.getCharNodes()[index];
6895
}
6996

70-
public boolean containWord(String word) {
97+
public ContainResult containWord(String word) {
7198
CharNode currentCharNode = root;
7299
char[] charArray = word.toCharArray();
73100
for (int i = 0; i < charArray.length; i++) {
@@ -77,31 +104,41 @@ public boolean containWord(String word) {
77104
isWord = true;
78105
}
79106
currentCharNode = containCharNode(currentCharNode, c, isWord);
80-
if (currentCharNode == null) {
81-
return false;
107+
if (currentCharNode == NOT_CONTAIN_NODE) {
108+
return ContainResult.NOT_CONTAIN;
109+
} else if (currentCharNode == CONTAIN_BUT_NOT_WORD_NODE) {
110+
return ContainResult.CONTAIN_BUT_NOT_WORD;
82111
}
83112
}
84-
return true;
113+
return ContainResult.CONTAIN;
85114
}
86115

87116
private CharNode containCharNode(CharNode currentCharNode, char c,
88117
boolean isWord) {
89118
CharNode[] charNodes = currentCharNode.getCharNodes();
90119
CharNode childCharNode = new CharNode(c, null);
120+
if (charNodes == null || charNodes.length == 0) {
121+
return NOT_CONTAIN_NODE;
122+
}
91123
int index = Arrays.binarySearch(charNodes, childCharNode);
92124
if (index >= 0) {
93-
if (isWord){
94-
if(charNodes[index].isWord) {
125+
if (isWord) {
126+
if (charNodes[index].isWord) {
95127
return charNodes[index];
96128
}
97-
return null;
129+
return CONTAIN_BUT_NOT_WORD_NODE;
98130
}
99131
return charNodes[index];
100132
}
101-
return null;
133+
return NOT_CONTAIN_NODE;
102134
}
103135
}
104136

137+
public static enum ContainResult {
138+
139+
CONTAIN, NOT_CONTAIN, CONTAIN_BUT_NOT_WORD,
140+
}
141+
105142
public static class CharNode implements Comparable<CharNode> {
106143

107144
private char c;

0 commit comments

Comments
 (0)