2016-12-16 9 views
2

私は、テキストに出現する重要なマルチワード表現(MWE)を示す索引項目をテキストとともに表示します(例えば、生物学テキストの場合は「スポンジ骨」)。私は、テキスト内のMWEの出現を認識できるように、エントリをspaCyでカスタムマッチャーを構築するために使用したいと思います。追加の要件は、MWE構成単語の字句表現とPOSタグを保持するために一致の発生が必要であることです。Spacyの複数単語の式認識

類似のことをする既存のspaCyの例を見てきましたが、パターンを取得できないようです。

答えて

-1

Spacyドキュメントでは、複数のフレーズでMatcherクラスを使用するとあまり明確ではありませんが、Githubレポにはexampleと一致するマルチフレーズがあります。

私は最近同じ挑戦に直面していましたが、私は以下のように動作させました。私のテキストファイルには、フレーズとその説明が '::'で区切られた行ごとに1つのレコードが含まれています。

import spacy 
import io 
from spacy.matcher import PhraseMatcher 

nlp = spacy.load('en') 
text = nlp(u'Your text here') 
rules = list() 

# Create a list of tuple of phrase and description from the file 
with io.open('textfile','r',encoding='utf8') as doc: 
    rules = [tuple(line.rstrip('\n').split('::')) for line in doc] 

# convert the phrase string to a spacy doc object 
rules = [(nlp(item[0].lower()),item[-1]) for item in rules ] 

# create a dictionary for accessing value using the string as the index which is returned by matcher class 
rules_dict = dict() 
for key,val in rules: 
    rules_dict[key.text]=val 

# get just the phrases from rules list 
rules_phrases = [item[0] for item in rules] 

# match using the PhraseMatcher class 
matcher = PhraseMatcher(nlp.vocab,rules_phrases) 
matches = matcher(text) 
result = list() 

for start,end,tag,label,m in matches: 
    result.append({"start":start,"end":end,"phrase":label,"desc":rules_dict[label]}) 
print(result)