天天看點

spacy進行命名實體識别

英文文本: 

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)
           

 運作結果:

spacy進行命名實體識别
spacy進行命名實體識别

 中文文本:

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))
           

運作結果:

spacy進行命名實體識别
spacy進行命名實體識别
spacy進行命名實體識别
spacy進行命名實體識别