mirror of
https://github.com/toolgood/ToolGood.Words.git
synced 2026-10-28 12:45:54 +08:00
更新 WordsSearch.java PinyinDict.java
This commit is contained in:
@@ -1,145 +1,13 @@
|
||||
package toolgood.words;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import toolgood.words.internals.TrieNode;
|
||||
import toolgood.words.internals.BaseSearch;
|
||||
import toolgood.words.internals.TrieNode2;
|
||||
|
||||
public class WordsSearch {
|
||||
protected TrieNode2[] _first = new TrieNode2[Character.MAX_VALUE + 1];
|
||||
String[] _keywords;
|
||||
public class WordsSearch extends BaseSearch {
|
||||
public String[] _others;
|
||||
int[] _indexs;
|
||||
|
||||
/**
|
||||
* 设置关键字
|
||||
*
|
||||
* @param keywords
|
||||
*/
|
||||
public void SetKeywords(List<String> keywords) {
|
||||
_keywords = new String[keywords.size()];
|
||||
_keywords = keywords.toArray(_keywords);
|
||||
_indexs = new int[_keywords.length];
|
||||
for (int i = 0; i < keywords.size(); i++) {
|
||||
_indexs[i] = i;
|
||||
}
|
||||
SetKeywords();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置关键字
|
||||
*
|
||||
* @param _keywords
|
||||
*/
|
||||
public void SetKeywords(Map<String, Integer> keywords) {
|
||||
int size = keywords.size();
|
||||
Integer i = 0;
|
||||
_keywords = new String[size];
|
||||
_indexs = new int[size];
|
||||
|
||||
for (String p : keywords.keySet()) {
|
||||
_keywords[i] = p;
|
||||
_indexs[i] = keywords.get(p);
|
||||
i++;
|
||||
}
|
||||
SetKeywords();
|
||||
}
|
||||
|
||||
private void SetKeywords() {
|
||||
TrieNode root = new TrieNode();
|
||||
Map<Integer, List<TrieNode>> allNodeLayers = new Hashtable<Integer, List<TrieNode>>();
|
||||
for (int i = 0; i < _keywords.length; i++) {
|
||||
String p = _keywords[i];
|
||||
TrieNode nd = root;
|
||||
for (int j = 0; j < p.length(); j++) {
|
||||
nd = nd.Add(p.charAt(j));
|
||||
if (nd.Layer == 0) {
|
||||
nd.Layer = j + 1;
|
||||
if (allNodeLayers.containsKey(nd.Layer) == false) {
|
||||
List<TrieNode> nodes = new ArrayList<TrieNode>();
|
||||
nodes.add(nd);
|
||||
allNodeLayers.put(nd.Layer, nodes);
|
||||
} else {
|
||||
allNodeLayers.get(nd.Layer).add(nd);
|
||||
}
|
||||
}
|
||||
}
|
||||
nd.SetResults(i);
|
||||
}
|
||||
|
||||
List<TrieNode> allNode = new ArrayList<TrieNode>();
|
||||
allNode.add(root);
|
||||
for (int i = 0; i < allNodeLayers.size(); i++) { //注意 这里不能用 keySet()
|
||||
List<TrieNode> nodes = allNodeLayers.get(i+1);
|
||||
for (int j = 0; j < nodes.size(); j++) {
|
||||
allNode.add(nodes.get(j));
|
||||
}
|
||||
}
|
||||
allNodeLayers.clear();
|
||||
allNodeLayers = null;
|
||||
|
||||
for (int i = 1; i < allNode.size(); i++) {
|
||||
TrieNode nd = allNode.get(i);
|
||||
nd.Index = i;
|
||||
TrieNode r = nd.Parent.Failure;
|
||||
Character c = nd.Char;
|
||||
while (r != null && !r.m_values.containsKey(c)) r = r.Failure;
|
||||
if (r == null)
|
||||
nd.Failure = root;
|
||||
else {
|
||||
nd.Failure = r.m_values.get(c);
|
||||
for (Integer result : nd.Failure.Results) {
|
||||
nd.SetResults(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
root.Failure = root;
|
||||
|
||||
List<TrieNode2> allNode2 = new ArrayList<TrieNode2>();
|
||||
for (int i = 0; i < allNode.size(); i++) {
|
||||
allNode2.add(new TrieNode2());
|
||||
}
|
||||
for (int i = 0; i < allNode2.size(); i++) {
|
||||
TrieNode oldNode = allNode.get(i);
|
||||
TrieNode2 newNode = allNode2.get(i);
|
||||
|
||||
for (Character key : oldNode.m_values.keySet()) {
|
||||
TrieNode nd = oldNode.m_values.get(key);
|
||||
newNode.Add(key, allNode2.get(nd.Index));
|
||||
}
|
||||
oldNode.Results.forEach(item -> {
|
||||
newNode.SetResults(item);
|
||||
});
|
||||
|
||||
oldNode = oldNode.Failure;
|
||||
while (oldNode != root) {
|
||||
for (Character key : oldNode.m_values.keySet()) {
|
||||
TrieNode nd = oldNode.m_values.get(key);
|
||||
if (newNode.HasKey(key) == false) {
|
||||
newNode.Add(key, allNode2.get(nd.Index));
|
||||
}
|
||||
}
|
||||
oldNode.Results.forEach(item -> {
|
||||
newNode.SetResults(item);
|
||||
});
|
||||
oldNode = oldNode.Failure;
|
||||
}
|
||||
}
|
||||
allNode.clear();
|
||||
allNode = null;
|
||||
root = null;
|
||||
|
||||
TrieNode2[] first = new TrieNode2[Character.MAX_VALUE + 1];
|
||||
TrieNode2 root2 = allNode2.get(0);
|
||||
for (Character key : root2.m_values.keySet()) {
|
||||
TrieNode2 nd = root2.m_values.get(key);
|
||||
first[(int) key] = nd;
|
||||
}
|
||||
_first = first;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在文本中查找第一个关键字
|
||||
@@ -165,7 +33,7 @@ public class WordsSearch {
|
||||
if (tn.End) {
|
||||
for (Integer index : tn.Results) {
|
||||
String key = _keywords[index];
|
||||
return new WordsSearchResult(key, i + 1 - key.length(), i, _indexs[index]);
|
||||
return new WordsSearchResult(key, i + 1 - key.length(), i, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -200,7 +68,7 @@ public class WordsSearch {
|
||||
if (tn.End) {
|
||||
for (Integer index : tn.Results) {
|
||||
String key = _keywords[index];
|
||||
WordsSearchResult item = new WordsSearchResult(key, i + 1 - key.length(), i, _indexs[index]);
|
||||
WordsSearchResult item = new WordsSearchResult(key, i + 1 - key.length(), i, index);
|
||||
list.add(item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ public class PinyinDict {
|
||||
private static String[] _pyShow;
|
||||
private static Integer[] _pyIndex;
|
||||
private static Integer[] _pyData;
|
||||
private static Integer[] _wordPyIndex;
|
||||
private static Integer[] _wordPy;
|
||||
private static WordsSearch _search;
|
||||
|
||||
@@ -36,11 +37,11 @@ public class PinyinDict {
|
||||
for (WordsSearchResult p : pos) {
|
||||
if (p.Start > pindex) {
|
||||
for (int i = 0; i < p.Keyword.length(); i++) {
|
||||
list[i + p.Start] = _pyShow[_wordPy[i + p.Index] + tone];
|
||||
list[i + p.Start] = _pyShow[_wordPy[i + _wordPyIndex[p.Index]] + tone];
|
||||
}
|
||||
pindex = p.End;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
if (list[i] != null)
|
||||
@@ -122,8 +123,7 @@ public class PinyinDict {
|
||||
return c.toString();
|
||||
}
|
||||
|
||||
public static List<String> GetPinyinForName(String name, int tone ) throws NumberFormatException, IOException
|
||||
{
|
||||
public static List<String> GetPinyinForName(String name, int tone) throws NumberFormatException, IOException {
|
||||
InitPyName();
|
||||
InitPyIndex();
|
||||
|
||||
@@ -133,37 +133,37 @@ public class PinyinDict {
|
||||
Integer[] indexs;
|
||||
if (name.length() > 1) { // 检查复姓
|
||||
xing = name.substring(0, 2);
|
||||
if(_pyName.containsKey(xing)){
|
||||
indexs=_pyName.get(xing);
|
||||
if (_pyName.containsKey(xing)) {
|
||||
indexs = _pyName.get(xing);
|
||||
for (Integer index : indexs) {
|
||||
list.add(_pyShow[index + tone]);
|
||||
}
|
||||
if (name.length() > 2) {
|
||||
ming = name.substring(2);
|
||||
String[] pys=GetPinyinList(ming, tone);
|
||||
String[] pys = GetPinyinList(ming, tone);
|
||||
for (String py : pys) {
|
||||
list.add(py);
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
xing = name.substring(0, 1);
|
||||
if(_pyName.containsKey(xing)){
|
||||
indexs=_pyName.get(xing);
|
||||
if (_pyName.containsKey(xing)) {
|
||||
indexs = _pyName.get(xing);
|
||||
for (Integer index : indexs) {
|
||||
list.add(_pyShow[index + tone]);
|
||||
}
|
||||
if (name.length() > 1) {
|
||||
ming = name.substring(1);
|
||||
String[] pys=GetPinyinList(ming, tone);
|
||||
String[] pys = GetPinyinList(ming, tone);
|
||||
for (String py : pys) {
|
||||
list.add(py);
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
String[] pys=GetPinyinList(name, tone);
|
||||
String[] pys = GetPinyinList(name, tone);
|
||||
for (String py : pys) {
|
||||
list.add(py);
|
||||
}
|
||||
@@ -204,8 +204,7 @@ public class PinyinDict {
|
||||
}
|
||||
}
|
||||
|
||||
private static void InitPyName() throws NumberFormatException, IOException
|
||||
{
|
||||
private static void InitPyName() throws NumberFormatException, IOException {
|
||||
if (_pyName == null) {
|
||||
String resourceName = "pyName.txt";
|
||||
InputStream u1 = WordsSearch.class.getClassLoader().getResourceAsStream(resourceName);
|
||||
@@ -221,29 +220,30 @@ public class PinyinDict {
|
||||
int in = Integer.valueOf(idx, 16);
|
||||
index.add(in);
|
||||
}
|
||||
Integer[] temp=new Integer[index.size()];
|
||||
Integer[] temp = new Integer[index.size()];
|
||||
pyName.put(sp[0], index.toArray(temp));
|
||||
}
|
||||
br.close();
|
||||
|
||||
|
||||
_pyName = pyName;
|
||||
}
|
||||
}
|
||||
|
||||
private static void InitPyWords() throws NumberFormatException, IOException
|
||||
{
|
||||
private static void InitPyWords() throws NumberFormatException, IOException {
|
||||
if (_search == null) {
|
||||
String resourceName = "pyWords.txt";
|
||||
InputStream u1 = WordsSearch.class.getClassLoader().getResourceAsStream(resourceName);
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(u1));
|
||||
|
||||
Map<String, Integer> keywords = new HashMap<String, Integer>();
|
||||
List<String> keywords = new ArrayList<String>();
|
||||
List<Integer> wordPyIndex = new ArrayList<Integer>();
|
||||
List<Integer> wordPy = new ArrayList<Integer>();
|
||||
|
||||
String tStr = "";
|
||||
while ((tStr = br.readLine()) != null) {
|
||||
String[] sp = tStr.split(",");
|
||||
keywords.put(sp[0], wordPy.size());
|
||||
keywords.add(sp[0]);
|
||||
wordPyIndex.add(wordPy.size());
|
||||
for (int i = 1; i < sp.length; i++) {
|
||||
String idx = sp[i];
|
||||
int in = Integer.valueOf(idx, 16);
|
||||
@@ -253,8 +253,10 @@ public class PinyinDict {
|
||||
br.close();
|
||||
WordsSearch search = new WordsSearch();
|
||||
search.SetKeywords(keywords);
|
||||
Integer[] wp=new Integer[wordPy.size()];
|
||||
Integer[] wp = new Integer[wordPy.size()];
|
||||
_wordPy = wordPy.toArray(wp);
|
||||
Integer[] wpi = new Integer[wordPyIndex.size()];
|
||||
_wordPyIndex = wordPyIndex.toArray(wpi);
|
||||
_search = search;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user