Search in sources :

Example 1 with GetWord

use of org.nlpcn.commons.lang.tire.GetWord in project ansj_seg by NLPchina.

the class Analysis method analysisStr.

/**
	 * 一整句话分词,用户设置的歧异优先
	 * 
	 * @param temp
	 * @return
	 */
private List<Term> analysisStr(String temp) {
    Graph gp = new Graph(temp);
    int startOffe = 0;
    if (this.ambiguityForest != null) {
        GetWord gw = new GetWord(this.ambiguityForest, gp.chars);
        String[] params = null;
        while ((gw.getFrontWords()) != null) {
            if (gw.offe > startOffe) {
                analysis(gp, startOffe, gw.offe);
            }
            params = gw.getParams();
            startOffe = gw.offe;
            for (int i = 0; i < params.length; i += 2) {
                gp.addTerm(new Term(params[i], startOffe, new TermNatures(new TermNature(params[i + 1], 1))));
                startOffe += params[i].length();
            }
        }
    }
    if (startOffe < gp.chars.length) {
        analysis(gp, startOffe, gp.chars.length);
    }
    List<Term> result = this.getResult(gp);
    return result;
}
Also used : Graph(org.ansj.util.Graph) TermNatures(org.ansj.domain.TermNatures) Term(org.ansj.domain.Term) TermNature(org.ansj.domain.TermNature) GetWord(org.nlpcn.commons.lang.tire.GetWord)

Example 2 with GetWord

use of org.nlpcn.commons.lang.tire.GetWord in project ansj_seg by NLPchina.

the class DicAnalysis method getResult.

@Override
protected List<Term> getResult(final Graph graph) {
    Merger merger = new Merger() {

        @Override
        public List<Term> merger() {
            // 用户自定义词典的识别
            userDefineRecognition(graph, forests);
            graph.walkPath();
            // 数字发现
            if (isNumRecognition && graph.hasNum) {
                new NumRecognition().recognition(graph.terms);
            }
            // 姓名识别
            if (graph.hasPerson && isNameRecognition) {
                // 亚洲人名识别
                new AsianPersonRecognition().recognition(graph.terms);
                graph.walkPathByScore();
                NameFix.nameAmbiguity(graph.terms);
                // 外国人名识别
                new ForeignPersonRecognition().recognition(graph.terms);
                graph.walkPathByScore();
            }
            return getResult();
        }

        private void userDefineRecognition(final Graph graph, Forest... forests) {
            if (forests == null) {
                return;
            }
            int beginOff = graph.terms[0].getOffe();
            Forest forest = null;
            for (int i = forests.length - 1; i >= 0; i--) {
                forest = forests[i];
                if (forest == null) {
                    continue;
                }
                GetWord word = forest.getWord(graph.chars);
                String temp = null;
                int tempFreq = 50;
                while ((temp = word.getAllWords()) != null) {
                    if (graph.terms[word.offe] == null) {
                        continue;
                    }
                    tempFreq = getInt(word.getParam()[1], 50);
                    Term term = new Term(temp, beginOff + word.offe, word.getParam()[0], tempFreq);
                    term.selfScore(-1 * Math.pow(Math.log(tempFreq), temp.length()));
                    TermUtil.insertTerm(graph.terms, term, InsertTermType.REPLACE);
                }
            }
            graph.rmLittlePath();
            graph.walkPathByScore();
            graph.rmLittlePath();
        }

        private int getInt(String str, int def) {
            try {
                return Integer.parseInt(str);
            } catch (NumberFormatException e) {
                return def;
            }
        }

        private List<Term> getResult() {
            List<Term> result = new ArrayList<Term>();
            int length = graph.terms.length - 1;
            for (int i = 0; i < length; i++) {
                if (graph.terms[i] != null) {
                    result.add(graph.terms[i]);
                }
            }
            setRealName(graph, result);
            return result;
        }
    };
    return merger.merger();
}
Also used : ArrayList(java.util.ArrayList) Term(org.ansj.domain.Term) AsianPersonRecognition(org.ansj.recognition.arrimpl.AsianPersonRecognition) NumRecognition(org.ansj.recognition.arrimpl.NumRecognition) Graph(org.ansj.util.Graph) ForeignPersonRecognition(org.ansj.recognition.arrimpl.ForeignPersonRecognition) Forest(org.nlpcn.commons.lang.tire.domain.Forest) GetWord(org.nlpcn.commons.lang.tire.GetWord)

Example 3 with GetWord

use of org.nlpcn.commons.lang.tire.GetWord in project ansj_seg by NLPchina.

the class IndexAnalysis method getResult.

@Override
protected List<Term> getResult(final Graph graph) {
    Merger merger = new Merger() {

        @Override
        public List<Term> merger() {
            graph.walkPath();
            // 数字发现
            if (isNumRecognition && graph.hasNum) {
                new NumRecognition().recognition(graph.terms);
            }
            // 姓名识别
            if (graph.hasPerson && isNameRecognition) {
                // 亚洲人名识别
                new AsianPersonRecognition().recognition(graph.terms);
                graph.walkPathByScore();
                NameFix.nameAmbiguity(graph.terms);
                // 外国人名识别
                new ForeignPersonRecognition().recognition(graph.terms);
                graph.walkPathByScore();
            }
            // 用户自定义词典的识别
            userDefineRecognition(graph, forests);
            return result();
        }

        private void userDefineRecognition(final Graph graph, Forest... forests) {
            new UserDefineRecognition(InsertTermType.SKIP, forests).recognition(graph.terms);
            graph.rmLittlePath();
            graph.walkPathByScore();
        }

        /**
			 * 检索的分词
			 * 
			 * @return
			 */
        private List<Term> result() {
            String temp = null;
            Set<String> set = new HashSet<String>();
            List<Term> result = new LinkedList<Term>();
            int length = graph.terms.length - 1;
            for (int i = 0; i < length; i++) {
                if (graph.terms[i] != null) {
                    result.add(graph.terms[i]);
                    set.add(graph.terms[i].getName() + graph.terms[i].getOffe());
                }
            }
            LinkedList<Term> last = new LinkedList<Term>();
            char[] chars = graph.chars;
            if (forests != null) {
                for (Forest forest : forests) {
                    if (forest == null) {
                        continue;
                    }
                    GetWord word = forest.getWord(chars);
                    while ((temp = word.getAllWords()) != null) {
                        if (!set.contains(temp + word.offe)) {
                            set.add(temp + word.offe);
                            last.add(new Term(temp, word.offe, word.getParam(0), ObjConver.getIntValue(word.getParam(1))));
                        }
                    }
                }
            }
            result.addAll(last);
            Collections.sort(result, new Comparator<Term>() {

                @Override
                public int compare(Term o1, Term o2) {
                    if (o1.getOffe() == o2.getOffe()) {
                        return o2.getName().length() - o1.getName().length();
                    } else {
                        return o1.getOffe() - o2.getOffe();
                    }
                }
            });
            setRealName(graph, result);
            return result;
        }
    };
    return merger.merger();
}
Also used : Term(org.ansj.domain.Term) AsianPersonRecognition(org.ansj.recognition.arrimpl.AsianPersonRecognition) LinkedList(java.util.LinkedList) NumRecognition(org.ansj.recognition.arrimpl.NumRecognition) Graph(org.ansj.util.Graph) ForeignPersonRecognition(org.ansj.recognition.arrimpl.ForeignPersonRecognition) UserDefineRecognition(org.ansj.recognition.arrimpl.UserDefineRecognition) Forest(org.nlpcn.commons.lang.tire.domain.Forest) HashSet(java.util.HashSet) GetWord(org.nlpcn.commons.lang.tire.GetWord)

Aggregations

Term (org.ansj.domain.Term)3 Graph (org.ansj.util.Graph)3 GetWord (org.nlpcn.commons.lang.tire.GetWord)3 AsianPersonRecognition (org.ansj.recognition.arrimpl.AsianPersonRecognition)2 ForeignPersonRecognition (org.ansj.recognition.arrimpl.ForeignPersonRecognition)2 NumRecognition (org.ansj.recognition.arrimpl.NumRecognition)2 Forest (org.nlpcn.commons.lang.tire.domain.Forest)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 TermNature (org.ansj.domain.TermNature)1 TermNatures (org.ansj.domain.TermNatures)1 UserDefineRecognition (org.ansj.recognition.arrimpl.UserDefineRecognition)1