【问题标题】:Tokenizer expanding extractionsTokenizer 扩展提取
【发布时间】:2020-01-14 05:46:05
【问题描述】:

我正在寻找一个扩展收缩的标记器。

使用 nltk 将短语拆分为标记,收缩不展开。

nltk.word_tokenize("she's")
-> ['she', "'s"]

但是,如果只使用带有收缩映射的字典,因此不考虑周围单词提供的任何信息,则无法决定“she's”应该映射到“she is”还是“she has” .

是否有提供收缩扩展的分词器?

【问题讨论】:

  • 变成“be”(引理)适合你吗?

标签: python nlp nltk tokenize


【解决方案1】:

您可以使用 Spacy 执行 rule based matching 以考虑周围单词提供的信息。 我在下面写了一些演示代码,您可以扩展它以涵盖更多情况:

import spacy
from spacy.pipeline import EntityRuler
from spacy import displacy
from spacy.matcher import Matcher

sentences = ["now she's a software engineer" , "she's got a cat", "he's a tennis player", "He thinks that she's 30 years old"]

nlp = spacy.load('en_core_web_sm')

def normalize(sentence):
    ans = []
    doc = nlp(sentence)


    #print([(t.text, t.pos_ , t.dep_) for t in doc])
    matcher = Matcher(nlp.vocab)
    pattern = [{"POS": "PRON"}, {"LOWER": "'s"}, {"LOWER": "got"}]
    matcher.add("case_has", None, pattern)
    pattern = [{"POS": "PRON"}, {"LOWER": "'s"}, {"LOWER": "been"}]
    matcher.add("case_has", None, pattern)
    pattern = [{"POS": "PRON"}, {"LOWER": "'s"}, {"POS": "DET"}]
    matcher.add("case_is", None, pattern)
    pattern = [{"POS": "PRON"}, {"LOWER": "'s"}, {"IS_DIGIT": True}]
    matcher.add("case_is", None, pattern)
    # .. add more cases

    matches = matcher(doc)
    for match_id, start, end in matches:
        string_id = nlp.vocab.strings[match_id]  
        for idx, t in enumerate(doc):
            if string_id == 'case_has' and t.text == "'s" and idx >= start and idx < end:
                ans.append("has")
                continue
            if string_id == 'case_is' and t.text == "'s" and idx >= start and idx < end:
                ans.append("is")
                continue
            else:
                ans.append(t.text)
    return(' '.join(ans))

for s in sentences:
    print(s)
    print(normalize(s))
    print()

输出:

现在她是一名软件工程师
现在她是一名软件工程师

她有一只猫
她养了一只猫

他是一名网球运动员
他是网球运动员

他认为她已经 30 岁了
他认为她已经30岁了

【讨论】:

  • 不错。我试过pycontractions,但即使是大模型也经常失败。
  • 好主意,完美解决了我的问题,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2017-11-29
  • 2012-04-05
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多