Skip to content

Commit

Permalink
支持jdk1.6
Browse files Browse the repository at this point in the history
提高代码可重用性
  • Loading branch information
hankcs committed Feb 12, 2015
1 parent 0570952 commit bda5129
Show file tree
Hide file tree
Showing 106 changed files with 579 additions and 649 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ hs_err_pid*
# IDE相关,不再同步
/.idea
/*.iml
/.classpath
/*.eml
/.project
# 以下两项是词典和模型的二进制文件,请不要提交到版本库
*.dat
*.bin
# 我做实验用的目录
/data/test
/data/test
2 changes: 1 addition & 1 deletion data/dictionary/person/nr.tr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ C,0,0,0,155393,0,0,0,0,0,0,0,47,461,0,0
D,2409,3427,0,0,0,0,0,0,141568,16990,0,0,0,63,0
E,1491,2198,0,0,1000,0,0,0,81654,9104,0,4,0,45,0
F,0,20,0,0,0,0,0,0,0,0,0,0,0,0,0
G,6,0,0,0,0,0,0,0,2907,1000,0,0,0,0,0
G,6,0,0,1000,0,0,0,0,2907,1000,0,0,0,0,0
K,0,217510,0,0,0,18,0,0,0,0,0,0,0,7506,11
L,256487,0,0,0,0,0,0,8039,0,0,0,7,0,0,0
M,0,29629,0,0,0,0,0,0,0,0,0,0,0,526,0
Expand Down
3 changes: 2 additions & 1 deletion data/dictionary/person/nr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5821,7 +5821,7 @@ ypy L 1
囊中 L 1
囊括 K 1
囚 L 1
四 C 136 L 131 D 11 E 7 K 2
四 C 136 L 131 G 100 D 11 E 7 K 2
四人 V 2
四代 Z 1
四倍 Z 1
Expand Down Expand Up @@ -22159,3 +22159,4 @@ ypy L 1
CEO K 4
R44 L 1
~ L 1
汪洋张 A 1
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.hankcs</groupId>
<artifactId>HanLP</artifactId>
<version>1.0</version>
<version>1.0.1</version>

<name>HanLP</name>
<url>http://www.hankcs.com/</url>
Expand Down Expand Up @@ -103,7 +103,7 @@
</plugins>
</build>
<properties>
<java.version>1.7</java.version>
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven-compiler-plugin.version>3.0</maven-compiler-plugin.version>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/hankcs/hanlp/algoritm/BinarySearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public class BinarySearch
{
public static void main(String[] args)
{
TreeSet<Long> setA = new TreeSet<>();
TreeSet<Long> setA = new TreeSet<Long>();
setA.add(5L);
setA.add(4L);

TreeSet<Long> setB = new TreeSet<>();
TreeSet<Long> setB = new TreeSet<Long>();
setB.add(1L);
setB.add(2L);
setB.add(3L);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/hankcs/hanlp/algoritm/Dijkstra.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Dijkstra
{
public static List<Vertex> compute(Graph graph)
{
List<Vertex> resultList = new LinkedList<>();
List<Vertex> resultList = new LinkedList<Vertex>();
Vertex[] vertexes = graph.getVertexes();
List<EdgeFrom>[] edgesTo = graph.getEdgesTo();
double[] d = new double[vertexes.length];
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/hankcs/hanlp/algoritm/ViterbiEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ViterbiEx(List<EnumItem<E>> roleTagList, TransformMatrixDictionary<E> tra
public List<E> computeTagList()
{
int length = roleTagList.size() - 1;
List<E> tagList = new LinkedList<>();
List<E> tagList = new LinkedList<E>();
double[][] cost = new double[length][];
Iterator<EnumItem<E>> iterator = roleTagList.iterator();
EnumItem<E> start = iterator.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public Collection<Emit> parseText(String text)
private static void remainLongest(Collection<Emit> collectedEmits)
{
if (collectedEmits.size() < 2) return;
Map<Integer, Emit> emitMapStart = new TreeMap<>();
Map<Integer, Emit> emitMapStart = new TreeMap<Integer, Emit>();
for (Emit emit : collectedEmits)
{
Emit pre = emitMapStart.get(emit.getStart());
Expand All @@ -178,7 +178,7 @@ private static void remainLongest(Collection<Emit> collectedEmits)
collectedEmits.addAll(emitMapStart.values());
return;
}
Map<Integer, Emit> emitMapEnd = new TreeMap<>();
Map<Integer, Emit> emitMapEnd = new TreeMap<Integer, Emit>();
for (Emit emit : emitMapStart.values())
{
Emit pre = emitMapEnd.get(emit.getEnd());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public List<Hit<V>> parseText(String text)
{
int position = 1;
int currentState = 0;
List<Hit<V>> collectedEmits = new LinkedList<>();
List<Hit<V>> collectedEmits = new LinkedList<Hit<V>>();
for (int i = 0; i < text.length(); ++i)
{
currentState = getState(currentState, text.charAt(i));
Expand Down Expand Up @@ -425,7 +425,7 @@ private int fetch(State parent, List<Map.Entry<Integer, State>> siblings)
{
State fakeNode = new State(-(parent.getDepth() + 1)); // 此节点是parent的子节点,同时具备parent的输出
fakeNode.addEmit(parent.getLargestValueId());
siblings.add(new AbstractMap.SimpleEntry<>(0, fakeNode));
siblings.add(new AbstractMap.SimpleEntry<Integer, State>(0, fakeNode));
}
for (Map.Entry<Character, State> entry : parent.getSuccess().entrySet())
{
Expand Down Expand Up @@ -814,7 +814,7 @@ private void buildDoubleArrayTrie(Set<String> keySet)

State root_node = this.rootState;

List<Map.Entry<Integer, State>> siblings = new ArrayList<>(root_node.getSuccess().entrySet().size());
List<Map.Entry<Integer, State>> siblings = new ArrayList<Map.Entry<Integer, State>>(root_node.getSuccess().entrySet().size());
fetch(root_node, siblings);
insert(siblings);
}
Expand Down Expand Up @@ -917,7 +917,7 @@ else if (first == 0)

for (Map.Entry<Integer, State> sibling : siblings)
{
List<Map.Entry<Integer, State>> new_siblings = new ArrayList<>(sibling.getValue().getSuccess().entrySet().size() + 1);
List<Map.Entry<Integer, State>> new_siblings = new ArrayList<Map.Entry<Integer, State>>(sibling.getValue().getSuccess().entrySet().size() + 1);

if (fetch(sibling.getValue(), new_siblings) == 0) // 一个词的终止且不为其他词的前缀,其实就是叶子节点
{
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/hankcs/hanlp/collection/MDAG/MDAG.java
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ public void unSimplify()
if (sourceNode == null)
{
sourceNode = new MDAGNode(false);
equivalenceClassMDAGNodeHashMap = new HashMap<>();
equivalenceClassMDAGNodeHashMap = new HashMap<MDAGNode, MDAGNode>();
MDAGNode[] toNodeArray = new MDAGNode[mdagDataArray.length];
createMDAGNode(simplifiedSourceNode, -1, toNodeArray, new MDAGNode[mdagDataArray.length]);
// 构建注册表
Expand Down Expand Up @@ -898,7 +898,7 @@ private void getStrings(HashSet<String> strHashSet, SearchCondition searchCondit
*/
public HashSet<String> getAllStrings()
{
HashSet<String> strHashSet = new LinkedHashSet<>();
HashSet<String> strHashSet = new LinkedHashSet<String>();

if (sourceNode != null)
getStrings(strHashSet, SearchCondition.NO_SEARCH_CONDITION, null, "", sourceNode.getOutgoingTransitions());
Expand Down Expand Up @@ -1066,6 +1066,6 @@ else if (nodeObjClass.equals(SimpleMDAGNode.class))
*/
public HashMap<MDAGNode, MDAGNode> _getEquivalenceClassMDAGNodeHashMap()
{
return new HashMap<>(equivalenceClassMDAGNodeHashMap);
return new HashMap<MDAGNode, MDAGNode>(equivalenceClassMDAGNodeHashMap);
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/hankcs/hanlp/collection/MDAG/MDAGMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
public class MDAGMap<V> extends AbstractMap<String, V>
{
ArrayList<V> valueList = new ArrayList<>();
ArrayList<V> valueList = new ArrayList<V>();
MDAGForMap mdag = new MDAGForMap();

@Override
Expand Down Expand Up @@ -69,7 +69,7 @@ public Set<Entry<String, V>> entrySet()
public Set<String> keySet()
{
HashSet<String> stringSet = mdag.getAllStrings();
LinkedHashSet<String> keySet = new LinkedHashSet<>();
LinkedHashSet<String> keySet = new LinkedHashSet<String>();
Iterator<String> iterator = stringSet.iterator();
while (iterator.hasNext())
{
Expand All @@ -88,7 +88,7 @@ public Set<String> keySet()
public LinkedList<Entry<String, V>> commonPrefixSearchWithValue(char[] key, int begin)
{
LinkedList<Entry<String, Integer>> valueIndex = mdag.commonPrefixSearchWithValueIndex(key, begin);
LinkedList<Entry<String, V>> entryList = new LinkedList<>();
LinkedList<Entry<String, V>> entryList = new LinkedList<Entry<String, V>>();
for (Entry<String, Integer> entry : valueIndex)
{
entryList.add(new SimpleEntry<String, V>(entry.getKey(), valueList.get(entry.getValue())));
Expand Down Expand Up @@ -175,7 +175,7 @@ private int getValueIndex(MDAGNode currentNode)

public LinkedList<Entry<String, Integer>> commonPrefixSearchWithValueIndex(char[] key, int begin)
{
LinkedList<Map.Entry<String, Integer>> result = new LinkedList<>();
LinkedList<Map.Entry<String, Integer>> result = new LinkedList<Map.Entry<String, Integer>>();
if (sourceNode != null)
{
int charCount = key.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public DartMap(List<String> keyList, List<V> valueList)
{
valueArray[i] = i;
}
this.valueArray = new ArrayList<>(valueList);
this.valueArray = new ArrayList<V>(valueList);
build(keyList, valueArray);
}

Expand Down Expand Up @@ -85,7 +85,7 @@ public ArrayList<Pair<String, V>> commonPrefixSearch(String key, int offset, int
{
byte[] keyBytes = key.getBytes(utf8);
List<Pair<Integer, Integer>> pairList = commonPrefixSearch(keyBytes, offset, maxResults);
ArrayList<Pair<String, V>> resultList = new ArrayList<>(pairList.size());
ArrayList<Pair<String, V>> resultList = new ArrayList<Pair<String, V>>(pairList.size());
for (Pair<Integer, Integer> pair : pairList)
{
resultList.add(new Pair<String, V>(new String(keyBytes, 0, pair.first), valueArray.get(pair.second)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ protected void walkToLoad(ByteArray byteArray, ValueArray valueArray)
child = new BaseNode[childSize];
for (int i = 0; i < childSize; ++i)
{
child[i] = new Node<>();
child[i] = new Node<V>();
child[i].walkToLoad(byteArray, valueArray);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public Set<Map.Entry<String, V>> entrySet()
*/
public Set<Map.Entry<String, V>> prefixSearch(String key)
{
Set<Map.Entry<String, V>> entrySet = new TreeSet<>();
Set<Map.Entry<String, V>> entrySet = new TreeSet<Map.Entry<String, V>>();
StringBuilder sb = new StringBuilder(key.substring(0, key.length() - 1));
BaseNode branch = this;
char[] chars = key.toCharArray();
Expand Down Expand Up @@ -202,7 +202,7 @@ public LinkedList<Map.Entry<String, V>> commonPrefixSearchWithValue(String key)
*/
public LinkedList<Map.Entry<String, V>> commonPrefixSearchWithValue(char[] chars, int begin)
{
LinkedList<Map.Entry<String, V>> result = new LinkedList<>();
LinkedList<Map.Entry<String, V>> result = new LinkedList<Map.Entry<String, V>>();
StringBuilder sb = new StringBuilder();
BaseNode branch = this;
for (int i = begin; i < chars.length; ++i)
Expand All @@ -213,7 +213,7 @@ public LinkedList<Map.Entry<String, V>> commonPrefixSearchWithValue(char[] chars
sb.append(aChar);
if (branch.status == Status.WORD_MIDDLE_2 || branch.status == Status.WORD_END_3)
{
result.add(new AbstractMap.SimpleEntry<>(sb.toString(), (V) branch.value));
result.add(new AbstractMap.SimpleEntry<String, V>(sb.toString(), (V) branch.value));
}
}

Expand Down Expand Up @@ -359,7 +359,7 @@ public boolean load(String path, V[] value)
int flag = byteArray.nextInt();
if (flag == 1)
{
child[i] = new Node<>();
child[i] = new Node<V>();
child[i].walkToLoad(byteArray, valueArray);
}
}
Expand All @@ -385,7 +385,7 @@ public boolean load(String path)
int flag = byteArray.nextInt();
if (flag == 1)
{
child[i] = new Node<>();
child[i] = new Node<V>();
child[i].walkToLoad(byteArray, valueArray);
}
}
Expand All @@ -401,7 +401,7 @@ public boolean load(ByteArray byteArray, ValueArray valueArray)
int flag = byteArray.nextInt();
if (flag == 1)
{
child[i] = new Node<>();
child[i] = new Node<V>();
child[i].walkToLoad(byteArray, valueArray);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public class CoNLLLoader
{
public static LinkedList<CoNLLSentence> loadSentenceList(String path)
{
LinkedList<CoNLLSentence> result = new LinkedList<>();
LinkedList<CoNllLine> lineList = new LinkedList<>();
LinkedList<CoNLLSentence> result = new LinkedList<CoNLLSentence>();
LinkedList<CoNllLine> lineList = new LinkedList<CoNllLine>();
for (String line : IOUtil.readLineListWithLessMemory(path))
{
if (line.trim().length() == 0)
{
result.add(new CoNLLSentence(lineList));
lineList = new LinkedList<>();
lineList = new LinkedList<CoNllLine>();
continue;
}
lineList.add(new CoNllLine(line.split("\t")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,36 @@ public class PosTagCompiler
*/
public static String compile(String tag, String name)
{
switch (tag)
{
case "m":
case "mq":
return Predefine.TAG_NUMBER;
case "nr":
case "nr1":
case "nr2":
case "nrf":
case "nrj":
return Predefine.TAG_PEOPLE;
case "ns":
case "nsf":
return Predefine.TAG_PLACE;
case "nt":
return Predefine.TAG_TIME;
case "x":
return Predefine.TAG_CLUSTER;
case "nx":
return Predefine.TAG_PROPER;
}
if (tag.startsWith("m")) return Predefine.TAG_NUMBER;
else if (tag.startsWith("nr")) return Predefine.TAG_PEOPLE;
else if (tag.startsWith("ns")) return Predefine.TAG_PLACE;
else if (tag.startsWith("nt")) return Predefine.TAG_GROUP;
else if (tag.startsWith("t")) return Predefine.TAG_TIME;
else if (tag.equals("x")) return Predefine.TAG_CLUSTER;
else if (tag.equals("nx")) return Predefine.TAG_PROPER;
else if (tag.equals("xx")) return Predefine.TAG_OTHER;

// switch (tag)
// {
// case "m":
// case "mq":
// return Predefine.TAG_NUMBER;
// case "nr":
// case "nr1":
// case "nr2":
// case "nrf":
// case "nrj":
// return Predefine.TAG_PEOPLE;
// case "ns":
// case "nsf":
// return Predefine.TAG_PLACE;
// case "nt":
// return Predefine.TAG_TIME;
// case "x":
// return Predefine.TAG_CLUSTER;
// case "nx":
// return Predefine.TAG_PROPER;
// }

return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static boolean makeModel(String corpusLoadPath, String modelSavePath) thr
{
if (i == j) continue;
// 这就是一个边的实例,从i出发,到j,当然它可能存在也可能不存在,不存在取null照样是一个实例
List<String> contextList = new LinkedList<>();
List<String> contextList = new LinkedList<String>();
// 先生成i和j的原子特征
contextList.addAll(generateSingleWordContext(word, i, "i"));
contextList.addAll(generateSingleWordContext(word, j, "j"));
Expand All @@ -68,7 +68,7 @@ public static boolean makeModel(String corpusLoadPath, String modelSavePath) thr

public static Collection<String> generateSingleWordContext(CoNLLWord[] word, int index, String mark)
{
Collection<String> context = new LinkedList<>();
Collection<String> context = new LinkedList<String>();
for (int i = index - 2; i < index + 2 + 1; ++i)
{
CoNLLWord w = i >= 0 && i < word.length ? word[i] : CoNLLWord.NULL;
Expand All @@ -81,7 +81,7 @@ public static Collection<String> generateSingleWordContext(CoNLLWord[] word, int

public static Collection<String> generateUniContext(CoNLLWord[] word, int i, int j)
{
Collection<String> context = new LinkedList<>();
Collection<String> context = new LinkedList<String>();
context.add(word[i].NAME + '→' + word[j].NAME);
context.add(word[i].POSTAG + '→' + word[j].POSTAG);
context.add(word[i].NAME + '→' + word[j].NAME + (i - j));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class WordNatureWeightModelMaker
{
public static boolean makeModel(String corpusLoadPath, String modelSavePath)
{
Set<String> posSet = new TreeSet<>();
Set<String> posSet = new TreeSet<String>();
DictionaryMaker dictionaryMaker = new DictionaryMaker();
for (CoNLLSentence sentence : CoNLLLoader.loadSentenceList(corpusLoadPath))
{
Expand Down
Loading

0 comments on commit bda5129

Please sign in to comment.