Com base no HANLP, qualquer sistema baseado em Lucene (7.x), incluindo Solr (7.x), é suportado.
< dependency >
< groupId >com.hankcs.nlp</ groupId >
< artifactId >hanlp-lucene-plugin</ artifactId >
< version >1.1.7</ version >
</ dependency >${webapp}/WEB-INF/lib . (Ou use mvn package para empacotar o código-fonte e copiar target/hanlp-lucene-plugin-xxxjar para ${webapp}/WEB-INF/lib )${core}/conf/schema.xml do Solr Core: < fieldType name = " text_cn " class = " solr.TextField " >
< analyzer type = " index " >
< tokenizer class = " com.hankcs.lucene.HanLPTokenizerFactory " enableIndexMode = " true " />
</ analyzer >
< analyzer type = " query " >
<!-- 切记不要在query中开启index模式 -->
< tokenizer class = " com.hankcs.lucene.HanLPTokenizerFactory " enableIndexMode = " false " />
</ analyzer >
</ fieldType >
<!-- 业务系统中需要分词的字段都需要指定type为text_cn -->
< field name = " my_field1 " type = " text_cn " indexed = " true " stored = " true " />
< field name = " my_field2 " type = " text_cn " indexed = " true " stored = " true " /> Atualmente, este plug-in suporta as seguintes configurações baseadas em schema.xml :
| Nome do item de configuração | Função | valor padrão |
|---|---|---|
| algoritmo | Algoritmo de particípio da palavra | Viterbi |
| EnableIndexMode | Defina como o modo de índice (não ligue para consulta) | verdadeiro |
| EnableCustomdictionary | Se deve habilitar o dicionário de usuário | verdadeiro |
| CustomDictionaryPath | Caminho do dicionário de usuário (caminho absoluto ou caminho relativo que pode ser lido pelo programa, separado por vários dicionários por espaços) | nulo |
| EnableCustomdictionário forçando | Dicionário de usuário alta prioridade | falso |
| StopWordDictionaryPath | Stop Word Dictionary Path | nulo |
| enableNumberQuantifierRecognize | Se deve habilitar o reconhecimento de palavras numéricas e quantitativas | verdadeiro |
| EnablenameRecognize | Ligue o reconhecimento de nome da pessoa | verdadeiro |
| EnableTranslatedNameRecognize | Se deve habilitar o reconhecimento de nome da pessoa transliterada | falso |
| Ativar Japanesenamerecognize | Se deve permitir o reconhecimento de nomes japoneses | falso |
| enableorganizationRecognize | Ligue o reconhecimento do nome da organização | falso |
| EnablePlacEroCognize | Ligue o reconhecimento do nome do local | falso |
| Enablenormalização | Se deve executar a regularização de caracteres (tradicional-> simplificado, largura total-> meia largura, superior-> casos inferiores) | falso |
| EnableTraditionalChinesEmode | Ligue a segmentação de palavras tradicional chinesa tradicional precisa | falso |
| Enabledebug | Ligue o modo de depuração | falso |
Configurações mais avançadas são configuradas principalmente por meio de hanlp.properties sob o caminho da classe. Leia a documentação do pacote de processamento de linguagem natural HANLP para configurações mais relacionadas, como:
Recomenda-se usar a implementação do filtro do Lucene ou Solr, este plug-in não interferirá. Um exemplo de configuração é o seguinte:
<!-- text_cn字段类型: 指定使用HanLP分词器,同时开启索引模式。通过solr自带的停用词过滤器,使用"stopwords.txt"(默认空白)过滤。
在搜索的时候,还支持solr自带的同义词词典。 -->
< fieldType name = " text_cn " class = " solr.TextField " positionIncrementGap = " 100 " >
< analyzer type = " index " >
< tokenizer class = " com.hankcs.lucene.HanLPTokenizerFactory " enableIndexMode = " true " />
< filter class = " solr.StopFilterFactory " ignoreCase = " true " words = " stopwords.txt " />
<!-- 取消注释可以启用索引期间的同义词词典
<filter class="solr.SynonymFilterFactory" synonyms="index_synonyms.txt" ignoreCase="true" expand="false"/>
-->
< filter class = " solr.LowerCaseFilterFactory " />
</ analyzer >
< analyzer type = " query " >
< tokenizer class = " com.hankcs.lucene.HanLPTokenizerFactory " enableIndexMode = " false " />
< filter class = " solr.StopFilterFactory " ignoreCase = " true " words = " stopwords.txt " />
< filter class = " solr.SynonymFilterFactory " synonyms = " synonyms.txt " ignoreCase = " true " expand = " true " />
< filter class = " solr.LowerCaseFilterFactory " />
</ analyzer >
</ fieldType >
<!-- 业务系统中需要分词的字段都需要指定type为text_cn -->
< field name = " my_field1 " type = " text_cn " indexed = " true " stored = " true " />
< field name = " my_field2 " type = " text_cn " indexed = " true " stored = " true " />Ao reescrever a consulta, você pode usar a parte da fala e outros atributos no resultado do particípio de Hanlpanalyzer, como
String text = "中华人民共和国很辽阔" ;
for ( int i = 0 ; i < text . length (); ++ i )
{
System . out . print ( text . charAt ( i ) + "" + i + " " );
}
System . out . println ();
Analyzer analyzer = new HanLPAnalyzer ();
TokenStream tokenStream = analyzer . tokenStream ( "field" , text );
tokenStream . reset ();
while ( tokenStream . incrementToken ())
{
CharTermAttribute attribute = tokenStream . getAttribute ( CharTermAttribute . class );
// 偏移量
OffsetAttribute offsetAtt = tokenStream . getAttribute ( OffsetAttribute . class );
// 距离
PositionIncrementAttribute positionAttr = tokenStream . getAttribute ( PositionIncrementAttribute . class );
// 词性
TypeAttribute typeAttr = tokenStream . getAttribute ( TypeAttribute . class );
System . out . printf ( "[%d:%d %d] %s/%s n " , offsetAtt . startOffset (), offsetAtt . endOffset (), positionAttr . getPositionIncrement (), attribute , typeAttr . type ());
}Em outros cenários, o Hanlptokenizer é suportado pela segmentação de palavras personalizada (como a segmentação de palavras que permite o reconhecimento de entidade nomeado, a segmentação tradicional de palavras chinesas, a segmentação de palavras CRF, etc.), como:
tokenizer = new HanLPTokenizer ( HanLP . newSegment ()
. enableJapaneseNameRecognize ( true )
. enableIndexMode ( true ), null , false );
tokenizer . setReader ( new StringReader ( "林志玲亮相网友:确定不是波多野结衣?" ));Licença Apache versão 2.0