import spacy
nlp = spacy.load('en_core_web_sm')
doc_2 = nlp('Weather is good, very windy and sunny.We have no classes in afternoon')
for ent in doc_2.ents:
print('{}--{}'.format(ent,ent.label_))
from spacy import displacy
doc = nlp('Weather is good, very windy and sunny.We have no classes in afternoon')
displacy.render(doc,style='ent',jupyter=True)
运行结果:
中文文本:
import spacy
nlp2 = spacy.load('zh_core_web_sm') #加载中文包
def read_file(file_name): #打开要处理的文本
with open(file_name,'r',encoding='utf-8') as file:
return file.read()
text = read_file('./data/nba.txt') #读取文本
processed_text = nlp2(text)
processed_text
sentences = [s for s in processed_text.sents]
print(len(sentences)) #输出有多少句话
from spacy import displacy
doc = nlp2(text)
displacy.render(doc,style='ent',jupyter=True)
from collections import Counter
def find_person(doc):
c = Counter()
for ent in processed_text.ents:
print(ent.label_)
print(ent.lemma_)
if ent.label_ == 'DATE':
c[ent.lemma_]+=1
return c.most_common(1)
print(find_person(processed_text))