当前位置:   article > 正文

NLP-HanLP基础_hanlp.segment

hanlp.segment

1、导入词典

  1. from pyhanlp import *
  2. def load_dictionary():
  3. """
  4. 加载HanLP中的mini词库
  5. :return: 一个set形式的词库
  6. """
  7. IOUtil = JClass('com.hankcs.hanlp.corpus.io.IOUtil')
  8. path = "CoreNatureDictionary.mini.txt" #字典所在的路径
  9. dic = IOUtil.loadDictionary([path]) #参数为列表形式
  10. return set(dic.keySet()) #返回集合
  11. if __name__ == '__main__':
  12. dic = load_dictionary()
  13. print(len(dic))
  14. print(list(dic)[0])

2、使用字典进行分词

  1. from pyhanlp import *
  2. #在字典中加入‘路麟城 nr 1’保证分词正确,删除bin文件重新运行
  3. print(HanLP.segment('''路明非累了。其实在路麟城第二次带他去那充满水银蒸汽的炼金术矩阵见小魔鬼时,
  4. 他已经决定要接受切割了。'''))
  5. for term in HanLP.segment('''你好,欢迎在python中调用hanlp的api'''):
  6. print(f'{term.word}\t{term.nature}')
  7. ------------------------------------------------------------
  8. [路明非/nr, 累/a, 了/ule, 。/w, 其实/d, 在/p, 路麟城/nr, 第二/mq, 次/qv, 带/v, 他/rr, 去/vf, 那/rzv, 充满/v, 水银/n, 蒸汽/n, 的/ude1, 炼金术/n, 矩阵/n, 见/v, 小魔鬼/nz, 时/qt, ,/w,
  9. /w, 他/rr, 已经/d, 决定/v, 要/v, 接受/v, 切割/v, 了/ule, 。/w]
  10. 路明非 nr
  11. 累 a
  12. 了 ule
  13. 。 w
  1. from pyhanlp import *
  2. HanLP.Config.ShowTermNature = False #不显示词性
  3. #导入分词器
  4. segment = JClass('com.hankcs.hanlp.seg.Other.AhoCorasickDoubleArrayTrieSegment')()
  5. segment.enablePartOfSpeechTagging(True) #识别英文和数字
  6. print(segment.seg("江西鄱阳湖干枯,中国six最大淡水湖变成大草原,tree"))

3、加载语料库

  1. 'my_cws_corpus.txt'
  2. 商品 和 服务
  3. 商品 和服 物美价廉
  4. 服务 和 货币
  5. --------------------------------------
  6. from pyhanlp import *
  7. CorpusLoader = SafeJClass('com.hankcs.hanlp.corpus.document.CorpusLoader')
  8. sents = CorpusLoader.convert2SentenceList('my_cws_corpus.txt')
  9. for sent in sents:
  10. print(sent)
  11. -------------------
  12. [商品, 和, 服务]
  13. [商品, 和服, 物美价廉]
  14. [服务, 和, 货币]

4、语法统计

  1. from pyhanlp import *
  2. NatureDictionaryMaker = SafeJClass('com.hankcs.hanlp.corpus.dictionary.NatureDictionaryMaker')
  3. CorpusLoader = SafeJClass('com.hankcs.hanlp.corpus.document.CorpusLoader')
  4. def train_bigram(corpus_path,model_path):
  5. sents = CorpusLoader.convert2SentenceList(corpus_path)
  6. for sent in sents:
  7. #为兼容hanlp字典格式,为每个单词添加占位符
  8. for word in sent:
  9. word.setLabel('n')
  10. #创建maker对象
  11. maker = NatureDictionaryMaker()
  12. #进行一元、二元统计
  13. maker.compute(sents)
  14. #保存文件,会得到三个文件
  15. maker.saveTxtTo(model_path)
  16. if __name__ == '__main__':
  17. train_bigram('my_cws_corpus.txt','my_cws_model')

5、获取词频

  1. from pyhanlp import *
  2. #必须用双引号
  3. def load_bigram(model_path):
  4. #更改字典路径
  5. HanLP.Config.CoreDictionaryPath = model_path + ".txt"
  6. HanLP.Config.BiGramDictionaryPath = model_path + ".ngram.txt"
  7. CoreDictionary = SafeJClass('com.hankcs.hanlp.dictionary.CoreDictionary')
  8. CoreBiGramTableDictionary = SafeJClass('com.hankcs.hanlp.dictionary.CoreBiGramTableDictionary')
  9. print(CoreDictionary.getTermFrequency("商品"))
  10. print(CoreBiGramTableDictionary.getBiFrequency("商品","和"))
  11. if __name__ == '__main__':
  12. load_bigram('my_cws_model')

6、构建词网

  1. from jpype import JString
  2. from pyhanlp import *
  3. WordNet = JClass('com.hankcs.hanlp.seg.common.WordNet')
  4. Vertex = JClass('com.hankcs.hanlp.seg.common.Vertex')
  5. #更改配置中字典路径
  6. HanLP.Config.CoreDictionaryPath = "my_cws_model.txt"
  7. CoreDictionary = LazyLoadingJClass('com.hankcs.hanlp.dictionary.CoreDictionary')
  8. def generate_wordnet(sent, trie):
  9. searcher = trie.getSearcher(JString(sent), 0)
  10. wordnet = WordNet(sent)
  11. while searcher.next():
  12. wordnet.add(searcher.begin + 1,
  13. Vertex(sent[searcher.begin:searcher.begin + searcher.length], searcher.value, searcher.index))
  14. # 原子分词,保证图连通
  15. vertexes = wordnet.getVertexes()
  16. i = 0
  17. while i < len(vertexes):
  18. if len(vertexes[i]) == 0
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/812565
推荐阅读
相关标签
  

闽ICP备14008679号