ben*_*ron 10 nlp text-parsing nltk spacy
我在nltk中有这个简单的分块示例.
我的数据:
data = 'The little yellow dog will then walk to the Starbucks, where he will introduce them to Michael.'
Run Code Online (Sandbox Code Playgroud)
......预处理......
data_tok = nltk.word_tokenize(data) #tokenisation
data_pos = nltk.pos_tag(data_tok) #POS tagging
Run Code Online (Sandbox Code Playgroud)
CHUNKING:
cfg_1 = "CUSTOMCHUNK: {<VB><.*>*?<NNP>}" #should return `walk to the Starbucks`, etc.
chunker = nltk.RegexpParser(cfg_1)
data_chunked = chunker.parse(data_pos)
Run Code Online (Sandbox Code Playgroud)
这返回(除了其他东西):(CUSTOMCHUNK walk/VB to/TO the/DT Starbucks/NNP)所以它做了我想要它做的事情.
现在我的问题是:我想切换到我的项目spacy.如何在spacy中执行此操作?
我来标记它(更粗糙的.pos方法将为我做):
from spacy.en import English
parser = English()
parsed_sent = parser(u'The little yellow dog will then walk to the Starbucks, where')
def print_coarse_pos(token):
print(token, token.pos_)
for sentence in parsed_sent.sents:
for token in sentence:
print_coarse_pos(token)
Run Code Online (Sandbox Code Playgroud)
...返回标签和标记
The DET
little ADJ
yellow ADJ
dog NOUN
will VERB
then ADV
walk VERB
...
我怎么能用自己的语法提取块?
从https://github.com/spacy-io/spaCy/issues/342逐字复制
有几种方法可以解决这个问题.RegexpParser该类最接近的功能是spaCy Matcher.但对于语法分块,我通常会使用依赖关系解析.例如,对于NPs分块,你有doc.noun_chunks迭代器:
doc = nlp(text)
for np in doc.noun_chunks:
print(np.text)
Run Code Online (Sandbox Code Playgroud)
这种工作的基本方式是这样的:
for token in doc:
if is_head_of_chunk(token)
chunk_start = token.left_edge.i
chunk_end = token.right_edge.i + 1
yield doc[chunk_start : chunk_end]
Run Code Online (Sandbox Code Playgroud)
您可以根据需要定义假设is_head_of功能.您可以使用依赖解析可视化工具来查看语法注释方案,并找出要使用的标签:http://spacy.io/demos/displacy