forked from NLPchina/ansj_seg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ansj
committed
Nov 30, 2016
1 parent
3976026
commit 572aeb3
Showing
33 changed files
with
1,080 additions
and
1,016 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 7 additions & 52 deletions
59
plugin/ansj_lucene5_plugin/src/main/java/org/ansj/lucene/util/AnsjTokenizerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,28 @@ | ||
package org.ansj.lucene.util; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.ansj.lucene5.AnsjAnalyzer; | ||
import org.apache.lucene.analysis.Tokenizer; | ||
import org.apache.lucene.analysis.util.TokenizerFactory; | ||
import org.apache.lucene.util.AttributeFactory; | ||
import org.nlpcn.commons.lang.util.IOUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.nlpcn.commons.lang.util.logging.Log; | ||
import org.nlpcn.commons.lang.util.logging.LogFactory; | ||
|
||
public class AnsjTokenizerFactory extends TokenizerFactory { | ||
|
||
public final Logger logger = LoggerFactory.getLogger(getClass()); | ||
public final Log logger = LogFactory.getLog(); | ||
|
||
private String stopwordsDir; | ||
public Set<String> filter; | ||
private String type; | ||
private Map<String, String> args; | ||
|
||
public AnsjTokenizerFactory(Map<String, String> args) { | ||
super(args); | ||
stopwordsDir = get(args, "words"); | ||
type = get(args, "type"); | ||
addStopwords(stopwordsDir); | ||
} | ||
|
||
/** | ||
* 添加停用词 | ||
* | ||
* @param dir | ||
*/ | ||
private void addStopwords(String dir) { | ||
if (dir == null) { | ||
logger.info("no stopwords dir"); | ||
return; | ||
} | ||
logger.info("stopwords: {}", dir); | ||
filter = new HashSet<String>(); | ||
BufferedReader br = null; | ||
try { | ||
br = IOUtil.getReader(dir, "uf-8"); | ||
String word = br.readLine(); | ||
while (word != null) { | ||
filter.add(word); | ||
word = br.readLine(); | ||
} | ||
} catch (FileNotFoundException e) { | ||
logger.info("No stopword file found"); | ||
} catch (IOException e) { | ||
logger.info("stopword file io exception"); | ||
} finally { | ||
if (br != null) { | ||
try { | ||
br.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
this.args = args ; | ||
} | ||
|
||
@Override | ||
public Tokenizer create(AttributeFactory factory) { | ||
return AnsjAnalyzer.getTokenizer(null, AnsjAnalyzer.TYPE.valueOf(type), filter); | ||
return AnsjAnalyzer.getTokenizer(null, args); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.