Search in sources :

Example 6 with NewWord

use of org.ansj.domain.NewWord in project ansj_seg by NLPchina.

the class AsianPersonRecognition method getNewWords.

public List<NewWord> getNewWords(Term[] terms) {
    this.terms = terms;
    List<NewWord> all = new ArrayList<NewWord>();
    List<Term> termList = recogntion_();
    for (Term term2 : termList) {
        all.add(new NewWord(term2.getName(), Nature.NR));
    }
    return all;
}
Also used : ArrayList(java.util.ArrayList) Term(org.ansj.domain.Term) NewWord(org.ansj.domain.NewWord)

Example 7 with NewWord

use of org.ansj.domain.NewWord in project ansj_seg by NLPchina.

the class LearnToolDemo method main.

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    // 构建一个新词学习的工具类。这个对象。保存了所有分词中出现的新词。出现次数越多。相对权重越大。
    LearnTool learnTool = new LearnTool();
    NlpAnalysis nlpAnalysis = new NlpAnalysis().setLearnTool(learnTool);
    // 进行词语分词。也就是nlp方式分词,这里可以分多篇文章
    nlpAnalysis.parseStr("说过,社交软件也是打着沟通的平台,让无数寂寞男女有了肉体与精神的寄托。");
    nlpAnalysis.parseStr("其实可以打着这个需求点去运作的互联网公司不应只是社交类软件与可穿戴设备,还有携程网,去哪儿网等等,订房订酒店多好的寓意");
    nlpAnalysis.parseStr("张艺谋的卡宴,马明哲的戏");
    // 取得学习到的topn新词,返回前10个。这里如果设置为0则返回全部
    System.out.println(learnTool.getTopTree(10));
    // 只取得词性为Nature.NR的新词
    System.out.println(learnTool.getTopTree(10, Nature.NR));
    /**
		 * 将训练结果序列写入到硬盘中
		 */
    List<Entry<String, Double>> topTree = learnTool.getTopTree(0);
    StringBuilder sb = new StringBuilder();
    for (Entry<String, Double> entry : topTree) {
        sb.append(entry.getKey() + "\t" + entry.getValue() + "\n");
    }
    IOUtil.Writer("learnTool.snap", IOUtil.UTF8, sb.toString());
    sb = null;
    /**
		 * reload训练结果
		 */
    learnTool = new LearnTool();
    HashMap<String, Double> loadMap = IOUtil.loadMap("learnTool.snap", IOUtil.UTF8, String.class, Double.class);
    for (Entry<String, Double> entry : loadMap.entrySet()) {
        learnTool.addTerm(new NewWord(entry.getKey(), Nature.NW, entry.getValue()));
        learnTool.active(entry.getKey());
    }
    System.out.println(learnTool.getTopTree(10));
    new File("learnTool.snap").delete();
}
Also used : Entry(java.util.Map.Entry) NlpAnalysis(org.ansj.splitWord.analysis.NlpAnalysis) File(java.io.File) LearnTool(org.ansj.dic.LearnTool) NewWord(org.ansj.domain.NewWord)

Aggregations

NewWord (org.ansj.domain.NewWord)7 ArrayList (java.util.ArrayList)3 Term (org.ansj.domain.Term)3 LearnTool (org.ansj.dic.LearnTool)2 AsianPersonRecognition (org.ansj.recognition.arrimpl.AsianPersonRecognition)2 ForeignPersonRecognition (org.ansj.recognition.arrimpl.ForeignPersonRecognition)2 File (java.io.File)1 Entry (java.util.Map.Entry)1 TermNatures (org.ansj.domain.TermNatures)1 NewWordRecognition (org.ansj.recognition.arrimpl.NewWordRecognition)1 NumRecognition (org.ansj.recognition.arrimpl.NumRecognition)1 UserDefineRecognition (org.ansj.recognition.arrimpl.UserDefineRecognition)1 NatureRecognition (org.ansj.recognition.impl.NatureRecognition)1 NlpAnalysis (org.ansj.splitWord.analysis.NlpAnalysis)1 MapCount (org.nlpcn.commons.lang.util.MapCount)1