首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >信托- SpaCy

信托- SpaCy
EN

Stack Overflow用户
提问于 2017-12-30 01:48:31
回答 1查看 2.9K关注 0票数 2

意图是基于POS标签大写,我可以通过下面的链接来实现。

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

试着用spacy来达到类似的效果?

代码语言:javascript
复制
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

它抛出了这个错误

代码语言:javascript
复制
  tagged_sent = token.tag_([word.lower() for token in doc])
NameError: global name 'token' is not defined

如何将令牌声明为全局标记并解决此问题。我的方法正确吗?

EN

回答 1

Stack Overflow用户

发布于 2017-12-30 04:29:41

代码语言:javascript
复制
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

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48030217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档