2017-12-30 10 views
0

インテントは、私が以下のリンクの助けを借りて達成することができるPOSタグを基にしています。スペイシーを使用して同様の結果を達成しようとしTruecasing - SpaCy

How can I best determine the correct capitalization for a word?

def truecase(doc): 
    truecased_sents = [] # list of truecased sentences 
    tagged_sent = token.tag_([word.lower() for token in doc]) 
    normalized_sent = [w.capitalize() if t in ["NN","NNS"] else w for (w,t) in tagged_sent] 
    normalized_sent[0] = normalized_sent[0].capitalize() 
    string = re.sub(" (?=[\.,'!?:;])", "", ' '.join(normalized_sent)) 
    return string 

それはトークンとしてグローバルに宣言し、この問題を解決する方法

tagged_sent = token.tag_([word.lower() for token in doc]) 
NameError: global name 'token' is not defined 

このエラーをスローします。私のアプローチは正しいのですか?

答えて

0
import spacy, re 
nlp = spacy.load('en_core_web_sm') 
doc = nlp(u'autonomous cars shift insurance liability toward manufacturers.') 
tagged_sent = [(w.text, w.tag_) for w in doc] 
normalized_sent = [w.capitalize() if t in ["NN","NNS"] else w for (w,t) in tagged_sent] 
normalized_sent[0] = normalized_sent[0].capitalize() 
string = re.sub(" (?=[\.,'!?:;])", "", ' '.join(normalized_sent)) 
print string 

出力:メーカーに向けて ロボットカーシフト保険負債。

関連する問題