Truecasing - SpaCy

Pro*_*ltk 2 python nltk spacy

目的是根据 POS 标签进行大写,我可以通过以下链接来实现。

如何最好地确定单词的正确大小写?

尝试使用 spacy 获得类似的结果?

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
Run Code Online (Sandbox Code Playgroud)

它抛出这个错误

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

如何将 token 声明为全局并解决这个问题。我的做法正确吗?

Pro*_*ltk 5

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
Run Code Online (Sandbox Code Playgroud)

输出: 自动驾驶汽车将保险责任转移给制造商。