当前位置:   article > 正文

spacy进行命名实体识别_spacy命名实体识别

spacy命名实体识别

英文文本: 

  1. import spacy
  2. nlp = spacy.load('en_core_web_sm')
  3. doc_2 = nlp('Weather is good, very windy and sunny.We have no classes in afternoon')
  4. for ent in doc_2.ents:
  5. print('{}--{}'.format(ent,ent.label_))
  6. from spacy import displacy
  7. doc = nlp('Weather is good, very windy and sunny.We have no classes in afternoon')
  8. displacy.render(doc,style='ent',jupyter=True)

 运行结果:

 中文文本:

  1. import spacy
  2. nlp2 = spacy.load('zh_core_web_sm') #加载中文包
  3. def read_file(file_name): #打开要处理的文本
  4. with open(file_name,'r',encoding='utf-8') as file:
  5. return file.read()
  6. text = read_file('./data/nba.txt') #读取文本
  7. processed_text = nlp2(text)
  8. processed_text
  9. sentences = [s for s in processed_text.sents]
  10. print(len(sentences)) #输出有多少句话
  11. from spacy import displacy
  12. doc = nlp2(text)
  13. displacy.render(doc,style='ent',jupyter=True)
  14. from collections import Counter
  15. def find_person(doc):
  16. c = Counter()
  17. for ent in processed_text.ents:
  18. print(ent.label_)
  19. print(ent.lemma_)
  20. if ent.label_ == 'DATE':
  21. c[ent.lemma_]+=1
  22. return c.most_common(1)
  23. print(find_person(processed_text))

运行结果:

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/394462
推荐阅读
相关标签
  

闽ICP备14008679号