org.ansj.library.DicLibrary Java Examples

The following examples show how to use org.ansj.library.DicLibrary. 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: DicSegment.java    From youkefu with Apache License 2.0 5 votes vote down vote up
public synchronized static String[] segment(String content) throws IOException{
	List<Term> terms = null ;
	if (librarykeyList != null && librarykeyList.size()>0) {
		terms = NlpAnalysis.parse(content,DicLibrary.gets(librarykeyList)).getTerms();
	}else {
		terms = NlpAnalysis.parse(content).getTerms();
	}
	List<String> words = new ArrayList<String>() ;
	for(Term term : terms) {
		words.add(term.getName()) ;
	}
	
	return words.toArray(new String[words.size()]);
}
 
Example #2
Source File: DicSegment.java    From youkefu with Apache License 2.0 5 votes vote down vote up
public static void addWord(List<String> words , String librarykey){
	if (DicLibrary.get(DicLibrary.DEFAULT+librarykey) == null) {
		MyStaticValue.ENV.put(DicLibrary.DEFAULT+librarykey,new File(loadpath,"ukefu-"+librarykey+".dic").getAbsolutePath());
		librarykeyList.add(DicLibrary.DEFAULT+librarykey);
	}
	if(words!=null && words.size() > 0  ) {
		for(String word : words) {
			DicLibrary.insert(DicLibrary.DEFAULT +librarykey, word);
		}
	}
}
 
Example #3
Source File: DicSegment.java    From youkefu with Apache License 2.0 5 votes vote down vote up
public static String[] byNature(String content , Set<String> expectedNature){
	List<String> wordList = new ArrayList<String>();
	if (!StringUtils.isBlank(content) && expectedNature != null && expectedNature.size() > 0) {
		Result result = NlpAnalysis.parse(content,DicLibrary.gets(librarykeyList));//分词结果的一个封装,主要是一个List<Term>的terms
           List<Term> terms = result.getTerms(); //拿到terms
           for(int i=0; i<terms.size(); i++) {
               String word = terms.get(i).getName(); //拿到词
               String natureStr = terms.get(i).getNatureStr(); //拿到词性
               if(expectedNature.contains(natureStr)) {
                   wordList.add(word+"/"+natureStr);
               }
           }
	}
	return wordList.toArray(new String[wordList.size()]);
}
 
Example #4
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 #5
Source File: DicSegment.java    From youkefu with Apache License 2.0 4 votes vote down vote up
public static void loadDic(String path , List<Words> wordsList) throws IOException{
	loadpath = path ;
	
	File dicPath = new File(path) ;
	if(!dicPath.exists()){
		dicPath.mkdirs() ;
	}
	File ukefudicFile = new File(path ,"ukefu.dic") ;//ukefu词典
	if(!ukefudicFile.getParentFile().exists()){
		ukefudicFile.getParentFile().mkdirs();
	}
	if(!ukefudicFile.exists()){
		ukefudicFile.createNewFile();
	}
	File ambiguitydicFile = new File(path ,"ambiguity.dic") ;//歧义词典
	if(!ambiguitydicFile.getParentFile().exists()){
		ambiguitydicFile.getParentFile().mkdirs();
	}
	if(!ambiguitydicFile.exists()){
		ambiguitydicFile.createNewFile();
	}
	File stopdicFile = new File(path ,"stop.dic") ;//停词词典
	if(!stopdicFile.getParentFile().exists()){
		stopdicFile.getParentFile().mkdirs();
	}
	if(!stopdicFile.exists()){
		stopdicFile.createNewFile();
	}
	try {
		librarykeyList = new ArrayList<String>() ;
		File[] tempList = dicPath.listFiles();
		for(File file : tempList){
			String name = file.getName();
			if (file.isFile() && name.contains("ukefu")) {
				String dicname = DicLibrary.DEFAULT+name.replaceAll(".dic","").replaceAll("ukefu-", "");//格式:[dic]+[类型id]
				if (MyStaticValue.ENV.get(dicname)!=null && !StringUtils.isBlank(MyStaticValue.ENV.get(dicname))) {
					MyStaticValue.reloadLibrary(dicname);
				}else {
					MyStaticValue.ENV.put(dicname, new File(path,name).getAbsolutePath());
				}
				librarykeyList.add(dicname);
			}
		}
		MyStaticValue.ENV.put(DicLibrary.DEFAULT,ukefudicFile.getAbsolutePath());
		MyStaticValue.ENV.put(AmbiguityLibrary.DEFAULT,ambiguitydicFile.getAbsolutePath());//歧义词典
		MyStaticValue.ENV.put(StopLibrary.DEFAULT,stopdicFile.getAbsolutePath());//停词词典
	} catch (Exception e) {
		e.printStackTrace();
	}//加载字典文件
	if (wordsList != null && wordsList.size()>0) {//加载数据库词典表
		for(Words words : wordsList){
			if(!StringUtils.isBlank(words.getContent())){
	    		for(String word : words.getContent().split("[, ,:;;\\n\t ]")){
	    			if(!StringUtils.isBlank(word)){
	    				DicLibrary.insert(DicLibrary.DEFAULT+"ukefu", word);
	    			}
	    		}
	    	}
		}
	}
}
 
Example #6
Source File: DicSegment.java    From youkefu with Apache License 2.0 4 votes vote down vote up
public static void removeWord(String word , String librarykey){
	if (!StringUtils.isBlank(word)) {
		DicLibrary.delete(DicLibrary.DEFAULT +librarykey, word);
	}
}
 
Example #7
Source File: DicSegment.java    From youkefu with Apache License 2.0 4 votes vote down vote up
public static void removeLibrary(String librarykey){
	if (!StringUtils.isBlank(librarykey)) {
		MyStaticValue.removeLibrary(DicLibrary.DEFAULT +librarykey);
	}
}
 
Example #8
Source File: AnsjAnalyzer.java    From jstarcraft-nlp with Apache License 2.0 4 votes vote down vote up
public AnsjAnalyzer(TYPE type, String dics) {
    this.args = new HashMap<String, String>();
    args.put("type", type.name());
    args.put(DicLibrary.DEFAULT, dics);
}
 
Example #9
Source File: NatureRecognition.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public NatureRecognition() {
    forests = new Forest[] {DicLibrary.get()};
}
 
Example #10
Source File: DicRecognition.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public DicRecognition() {
    forests = DicLibrary.gets(DicLibrary.DEFAULT);
}
 
Example #11
Source File: DicRecognition.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public DicRecognition(String[] keys) {
    forests = DicLibrary.gets(keys);
}
 
Example #12
Source File: UserDicNatureRecognition.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public UserDicNatureRecognition() {
    forests = new Forest[] {DicLibrary.get()};
}