org.nlpcn.commons.lang.tire.domain.Forest Java Examples

The following examples show how to use org.nlpcn.commons.lang.tire.domain.Forest. 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: LearnTool.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * 公司名称学习.
 * 
 * @param graph
 */
public void learn(Graph graph, SplitWord splitWord, Forest... forests) {

    this.splitWord = splitWord;

    this.forests = forests;

    // 亚洲人名识别
    if (isAsianName) {
        findAsianPerson(graph);
    }

    // 外国人名识别
    if (isForeignName) {
        findForeignPerson(graph);
    }

}
 
Example #2
Source File: AmbiguityLibrary.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * 根据key获取
 * 
 */
public static Forest get(String key) {

    KV<String, Forest> kv = AMBIGUITY.get(key);

    if (kv == null) {
        if (MyStaticValue.ENV.containsKey(key)) {
            putIfAbsent(key, MyStaticValue.ENV.get(key));
            return get(key);
        }

        LOG.warn("crf " + key + " not found in config ");
        return null;
    }

    Forest sw = kv.getV();
    if (sw == null) {
        try {
            sw = init(key, kv, false);
        } catch (Exception e) {
        }
    }
    return sw;
}
 
Example #3
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 #4
Source File: MyStaticValue.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * 增加一个词典
 * 
 * @param key
 * @param path
 * @param value
 */
public static void putLibrary(String key, String path, Object value) {
    if (key.startsWith(DicLibrary.DEFAULT)) {
        DicLibrary.put(key, path, (Forest) value);
    } else if (key.startsWith(StopLibrary.DEFAULT)) {
        StopLibrary.put(key, path, (StopRecognition) value);
    } else if (key.startsWith(SynonymsLibrary.DEFAULT)) {
        SynonymsLibrary.put(key, path, (SmartForest) value);
    } else if (key.startsWith(AmbiguityLibrary.DEFAULT)) {
        AmbiguityLibrary.put(key, path, (Forest) value);
    } else if (key.startsWith(CrfLibrary.DEFAULT)) {
        CrfLibrary.put(key, path, (SplitWord) value);
    } else {
        throw new LibraryException(key + " type err must start with dic,stop,ambiguity,synonyms");
    }
    ENV.put(key, path);
}
 
Example #5
Source File: Library.java    From nlp-lang with Apache License 2.0 6 votes vote down vote up
/**
 * 词典树的构造方法
 *
 * @param br
 * @param forest
 * @return
 * @throws Exception
 */
private static Forest makeLibrary(BufferedReader br, Forest forest) throws Exception {
	if (br == null) {
		return forest;
	}
	try {
		String temp = null;
		while ((temp = br.readLine()) != null) {
			insertWord(forest, temp);
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		br.close();
	}
	return forest;
}
 
Example #6
Source File: DicLibrary.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * 根据模型名称获取crf模型
 * 
 * @param modelName
 * @return
 */
public static Forest get(String key) {

    KV<String, Forest> kv = DIC.get(key);

    if (kv == null) {
        if (MyStaticValue.ENV.containsKey(key)) {
            putIfAbsent(key, MyStaticValue.ENV.get(key));
            return get(key);
        }
        LOG.warn("dic " + key + " not found in config ");
        return null;
    }
    Forest forest = kv.getV();
    if (forest == null) {
        forest = init(key, kv, false);
    }
    return forest;

}
 
Example #7
Source File: DicManager.java    From nlp-lang with Apache License 2.0 5 votes vote down vote up
/**
 * 构建一个tire书辞典
 * 
 * @param dicName
 * @param dicName
 * @return
 * @throws Exception
 */
public synchronized static Forest makeForest(String dicName, BufferedReader br) throws Exception {
	Forest forest = null;
	if ((forest = forestMap.get(dicName)) != null) {
		return forest;
	}
	forest = Library.makeForest(br);

	if (dicName != null) {
		forestMap.put(dicName, forest);
	}

	return forest;
}
 
Example #8
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 #9
Source File: Analysis.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
protected Analysis() {
    this.forests = new Forest[] {DicLibrary.get()};
    this.isNameRecognition = MyStaticValue.isNameRecognition;
    this.isNumRecognition = MyStaticValue.isNumRecognition;
    this.isQuantifierRecognition = MyStaticValue.isQuantifierRecognition;
    this.isRealName = MyStaticValue.isRealName;
}
 
Example #10
Source File: DicRecognition.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void recognition(Result result) {
    for (Forest forest : forests) {
        if (forest == null) {
            continue;
        }
        recognition(result, forest);
    }
}
 
Example #11
Source File: NameFix.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * 人名消歧,比如.邓颖超生前->邓颖 超生 前 fix to 丁颖超 生 前! 规则的方式增加如果两个人名之间连接是- , ·,•则连接
 */
public static void nameAmbiguity(Term[] terms, Forest... forests) {
    Term from = null;
    Term term = null;
    Term next = null;
    for (int i = 0; i < terms.length - 1; i++) {
        term = terms[i];
        if (term != null && term.termNatures() == TermNatures.NR && term.getName().length() == 2) {
            next = terms[i + 2];
            if (next.termNatures().personAttr.split > 0) {
                term.setName(term.getName() + next.getName().charAt(0));
                terms[i + 2] = null;

                String name = next.getName().substring(1);
                terms[i + 3] = new Term(name, next.getOffe() + 1,
                                new NatureRecognition(forests).getTermNatures(name));
                TermUtil.termLink(term, terms[i + 3]);
                TermUtil.termLink(terms[i + 3], next.to());
            }
        }
    }

    // 外国人名修正
    for (int i = 0; i < terms.length; i++) {
        term = terms[i];
        if (term != null && term.getName().length() == 1 && i > 0
                        && WordAlert.CharCover(term.getName().charAt(0)) == '·') {
            from = term.from();
            next = term.to();

            if (from.natrue().natureStr.startsWith("nr") && next.natrue().natureStr.startsWith("nr")) {
                from.setName(from.getName() + term.getName() + next.getName());
                TermUtil.termLink(from, next.to());
                terms[i] = null;
                terms[i + 1] = null;
            }
        }
    }

}
 
Example #12
Source File: DicLibrary.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * 关键词增加
 *
 * @param keyword 所要增加的关键词
 * @param nature 关键词的词性
 * @param freq 关键词的词频
 */
public static void insert(String key, String keyword, String nature, int freq) {
    Forest dic = get(key);
    String[] paramers = new String[2];
    paramers[0] = nature;
    paramers[1] = String.valueOf(freq);
    Value value = new Value(keyword, paramers);
    Library.insertWord(dic, value);
}
 
Example #13
Source File: DicLibrary.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * 删除关键词
 */
public static void delete(String key, String word) {

    Forest dic = get(key);
    if (dic != null) {
        Library.removeWord(dic, word);
    }
}
 
Example #14
Source File: UserDefineRecognition.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public UserDefineRecognition(InsertTermType type, Forest... forests) {
    this.type = type;
    if (forests != null && forests.length > 0) {
        this.forests = forests;
    }

}
 
Example #15
Source File: GetWordTest.java    From nlp-lang with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	/**
	 * 词典的构造.一行一个词后面是参数.可以从文件读取.可以是read流.
	 */
	long start = System.currentTimeMillis();
	String dic = "android\t10\t孙健\nc\t100\nC++\t10\nc++\t5\nc#\t100\nVC++\t100".toLowerCase();
	System.out.println(dic);
	Forest forest = Library.makeForest(new BufferedReader(new StringReader(dic)));
	/**
	 * 删除一个单词
	 */
	Library.removeWord(forest, "中国");
	/**
	 * 增加一个新词
	 */
	Library.insertWord(forest, "中国人");
	String content = "Android--中国人";
	content = StringUtil.rmHtmlTag(content);

	for (int i = 0; i < 1; i++) {
		GetWord udg = forest.getWord(content.toLowerCase().toCharArray());

		String temp = null;
		while ((temp = udg.getFrontWords()) != null) {
			System.out.println(temp + "\t\t" + udg.getParam()[0] + "\t\t" + udg.getParam()[1]);
			System.out.println(udg.offe);
		}
	}
	System.out.println(System.currentTimeMillis() - start);
}
 
Example #16
Source File: Library.java    From nlp-lang with Apache License 2.0 5 votes vote down vote up
private static void insertWord(Forest forest, String temp, String... param) {
	SmartForest<String[]> branch = forest;
	char[] chars = temp.toCharArray();
	for (int i = 0; i < chars.length; i++) {
		if (chars.length == i + 1) {
			branch.add(new Forest(chars[i], 3, param));
		} else {
			branch.add(new Forest(chars[i], 1, null));
		}
		branch = branch.getBranch(chars[i]);
	}
}
 
Example #17
Source File: AmbiguityLibrary.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * 刷新一个,将值设置为null
 * 
 * @param key
 * @return
 */
public static void reload(String key) {
    if (!MyStaticValue.ENV.containsKey(key)) { //如果变量中不存在直接删掉这个key不解释了
        remove(key);
    }

    putIfAbsent(key, MyStaticValue.ENV.get(key));

    KV<String, Forest> kv = AMBIGUITY.get(key);

    init(key, kv, true);
}
 
Example #18
Source File: AmbiguityLibrary.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * 删除一个key
 * 
 * @param key
 * @return
 */
public static KV<String, Forest> remove(String key) {
    KV<String, Forest> kv = AMBIGUITY.get(key);
    if (kv != null && kv.getV() != null) {
        kv.getV().clear();
    }
    MyStaticValue.ENV.remove(key);
    return AMBIGUITY.remove(key);
}
 
Example #19
Source File: AmbiguityLibrary.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * 插入到树中呀
 * 
 * @param key
 * @param split
 * @return
 */
public static void insert(String key, String... split) {
    Forest forest = get(key);
    StringBuilder sb = new StringBuilder();
    if (split.length % 2 != 0) {
        LOG.error("init ambiguity  error in line :" + Arrays.toString(split) + " format err !");
        return;
    }
    for (int i = 0; i < split.length; i += 2) {
        sb.append(split[i]);
    }
    forest.addBranch(sb.toString(), split);
}
 
Example #20
Source File: AmbiguityLibrary.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * 获取系统默认词典
 * 
 * @return
 */
public static Forest get() {
    if (!AMBIGUITY.containsKey(DEFAULT)) {
        return null;
    }
    return get(DEFAULT);
}
 
Example #21
Source File: DicLibrary.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static void reload(String key) {
    if (!MyStaticValue.ENV.containsKey(key)) { //如果变量中不存在直接删掉这个key不解释了
        remove(key);
    }

    putIfAbsent(key, MyStaticValue.ENV.get(key));

    KV<String, Forest> kv = DIC.get(key);

    init(key, kv, true);
}
 
Example #22
Source File: DicLibrary.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static KV<String, Forest> remove(String key) {
    KV<String, Forest> kv = DIC.get(key);
    if (kv != null && kv.getV() != null) {
        kv.getV().clear();
    }
    MyStaticValue.ENV.remove(key);
    return DIC.remove(key);
}
 
Example #23
Source File: Library.java    From nlp-lang with Apache License 2.0 5 votes vote down vote up
/**
 * 传入value数组.构造树
 *
 * @param values
 * @param forest
 * @return
 */
public static Forest makeForest(List<Value> values) {
	Forest forest = new Forest();
	for (Value value : values) {
		insertWord(forest, value.toString());
	}
	return forest;
}
 
Example #24
Source File: DicLibrary.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * 根据keys获取词典集合
 * 
 * @param keys
 * @return
 */
public static Forest[] gets(String... keys) {
    Forest[] forests = new Forest[keys.length];
    for (int i = 0; i < forests.length; i++) {
        forests[i] = get(keys[i]);
    }
    return forests;
}
 
Example #25
Source File: Library.java    From nlp-lang with Apache License 2.0 5 votes vote down vote up
/**
 * 插入一个词
 *
 * @param forest
 * @param temp
 */
public static void insertWord(Forest forest, String temp) {
	String[] param = temp.split("\t");

	temp = param[0];

	String[] resultParams = new String[param.length - 1];
	for (int j = 1; j < param.length; j++) {
		resultParams[j - 1] = param[j];
	}

	insertWord(forest, temp, resultParams);
}
 
Example #26
Source File: Library.java    From nlp-lang with Apache License 2.0 5 votes vote down vote up
/**
 * 删除一个词
 *
 * @param forest
 * @param temp
 */
public static void removeWord(Forest forest, String word) {
	SmartForest<String[]> branch = forest;
	char[] chars = word.toCharArray();

	for (int i = 0; i < chars.length; i++) {
		if (branch == null) {
			return;
		}
		if (chars.length == i + 1) {
			branch.add(new Forest(chars[i], -1, null));
		}
		branch = branch.getBranch(chars[i]);
	}
}
 
Example #27
Source File: DicLibrary.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public static Forest get() {
    if (!DIC.containsKey(DEFAULT)) {
        return null;
    }
    return get(DEFAULT);
}
 
Example #28
Source File: Analysis.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public Analysis setForests(Forest... forests) {
    this.forests = forests;
    return this;
}
 
Example #29
Source File: AmbiguityLibrary.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public static void put(String key, String path, Forest value) {
    AMBIGUITY.put(key, KV.with(path, value));
    MyStaticValue.ENV.put(key, path);
}
 
Example #30
Source File: Analysis.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public Analysis setAmbiguityForest(Forest ambiguityForest) {
    this.ambiguityForest = ambiguityForest;
    return this;
}