Java Code Examples for org.nlpcn.commons.lang.tire.domain.SmartForest#getParam()

The following examples show how to use org.nlpcn.commons.lang.tire.domain.SmartForest#getParam() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NatureRecognition.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * 获取一个词语的参数
 * 
 * @param word
 * @return
 */
public String[] getParams(String word) {
    for (Forest forest : forests) {
        if (forest == null) {
            continue;
        }
        SmartForest<String[]> sf = forest;
        for (int i = 0; i < word.length(); i++) {
            sf = sf.get(word.charAt(i));
            if (sf == null) {
                return null;
            }
        }
        if (sf.getStatus() > 1) {
            return sf.getParam();
        } else {
            return null;
        }
    }
    return null;
}
 
Example 2
Source File: LearnTool.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * 增加一个新词到树中
 * 
 * @param newWord
 */
public void addTerm(NewWord newWord) {
    NewWord temp = null;
    SmartForest<NewWord> smartForest = null;
    if ((smartForest = sf.getBranch(newWord.getName())) != null && smartForest.getParam() != null) {
        temp = smartForest.getParam();
        temp.update(newWord.getNature(), newWord.getAllFreq());
    } else {
        count++;
        if (splitWord == null) {
            newWord.setScore(-1);
        } else {
            newWord.setScore(-splitWord.cohesion(newWord.getName()));
        }

        synchronized (sf) {
            sf.add(newWord.getName(), newWord);
        }
    }
}
 
Example 3
Source File: SynonymsLibrary.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private static Set<String> findAllWords(String key, String[] words) {

        SmartForest<List<String>> synonyms = get(key);

        Set<String> set = new HashSet<>();
        for (String word : words) {
            SmartForest<List<String>> branch = synonyms.getBranch(word);
            if (branch != null) {
                List<String> params = branch.getParam();
                if (params != null) {
                    set.addAll(params);
                }
            }
        }
        return set;
    }
 
Example 4
Source File: SynonymsLibrary.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * 从同义词组中删除掉一个词 [中国, 中华, 我国] -> remove(我国) -> [中国, 中华]
 * 
 * @param words
 */
public static void remove(String key, String word) {

    SmartForest<List<String>> synonyms = get(key);

    SmartForest<List<String>> branch = synonyms.getBranch(word);

    if (branch == null || branch.getStatus() < 2) {
        return;
    }

    List<String> params = branch.getParam();

    synonyms.remove(word);
    branch.setParam(null);
    params.remove(word);

    if (params.size() == 1) { //如果是1 个也删除
        synonyms.remove(params.get(0));
        params.remove(0);
    } else {
        params.remove(word);
    }
}
 
Example 5
Source File: NatureRecognition.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * 通过规则 猜测词性
 * 
 * @param word
 * @return
 */
public static TermNatures guessNature(String word) {
    String nature = null;
    SmartForest<String[]> smartForest = SUFFIX_FOREST;
    int len = 0;
    for (int i = word.length() - 1; i >= 0; i--) {
        smartForest = smartForest.get(word.charAt(i));
        if (smartForest == null) {
            break;
        }
        len++;
        if (smartForest.getStatus() == 2) {
            nature = smartForest.getParam()[0];
        } else if (smartForest.getStatus() == 3) {
            nature = smartForest.getParam()[0];
            break;
        }
    }

    if ("nt".equals(nature) && (len > 1 || word.length() > 3)) {
        return TermNatures.NT;
    } else if ("ns".equals(nature)) {
        return TermNatures.NS;
    } else if (word.length() < 5) {
        Result parse = ToAnalysis.parse(word);
        for (Term term : parse.getTerms()) {
            if ("nr".equals(term.getNatureStr())) {
                return TermNatures.NR;
            }
        }
    } else if (ForeignPersonRecognition.isFName(word)) {
        return TermNatures.NRF;
    }

    return TermNatures.NW;
}
 
Example 6
Source File: SynonymsRecgnition.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void recognition(Result result) {
    for (Term term : result) {
        SmartForest<List<String>> branch = synonyms.getBranch(term.getName());
        if (branch != null && branch.getStatus() > 1) {
            List<String> syns = branch.getParam();
            if (syns != null) {
                term.setSynonyms(syns);
            }
        }
    }
}
 
Example 7
Source File: UserDicNatureRecognition.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static String[] getParams(Forest forest, String word) {
    SmartForest<String[]> temp = forest;
    for (int i = 0; i < word.length(); i++) {
        temp = temp.get(word.charAt(i));
        if (temp == null) {
            return null;
        }
    }
    if (temp.getStatus() > 1) {
        return temp.getParam();
    } else {
        return null;
    }
}
 
Example 8
Source File: LearnTool.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * 尝试激活,新词
 * 
 * @param name
 */
public void active(String name) {
    SmartForest<NewWord> branch = sf.getBranch(name);
    if (branch != null && branch.getParam() != null) {
        branch.getParam().setActive(true);
    }
}
 
Example 9
Source File: Model.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * 获得特征所在权重数组
 * 
 * @param featureStr
 * @return
 */
public float[] getFeature(char... chars) {
    if (chars == null) {
        return null;
    }
    SmartForest<float[]> sf = featureTree;
    sf = sf.getBranch(chars);
    if (sf == null || sf.getParam() == null) {
        return null;
    }
    return sf.getParam();
}