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
孙健
committed
Sep 7, 2016
1 parent
5ff0319
commit 6cf0dd0
Showing
5 changed files
with
77 additions
and
4 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
73 changes: 73 additions & 0 deletions
73
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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; | ||
|
||
public class AnsjTokenizerFactory extends TokenizerFactory { | ||
|
||
public final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
private String stopwordsDir; | ||
public Set<String> filter; | ||
private String type; | ||
|
||
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(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Tokenizer create(AttributeFactory factory) { | ||
return AnsjAnalyzer.getTokenizer(null, AnsjAnalyzer.TYPE.valueOf(type), filter); | ||
} | ||
|
||
} |
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
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