【问题标题】:Find multi-word terms in a tokenized text in Python在 Python 中的标记化文本中查找多字词
【发布时间】:2017-12-05 22:43:06
【问题描述】:

我有一个已标记化的文本,或者一般来说,一个单词列表也可以。例如:

   >>> from nltk.tokenize import word_tokenize
    >>> s = '''Good muffins cost $3.88\nin New York.  Please buy me
    ... two of them.\n\nThanks.'''
    >>> word_tokenize(s)
        ['Good', 'muffins', 'cost', '$', '3.88', 'in', 'New', 'York', '.',
        'Please', 'buy', 'me', 'two', 'of', 'them', '.', 'Thanks', '.']

如果我有一个包含单个单词和多单词键的 Python 字典,我如何才能有效且正确地检查它们在文本中的存在?理想的输出是 key:location_in_text 对,或者其他方便的东西。 提前致谢!

附:解释“正确” - 如果我的字典中有“租约”,我不希望请标记。此外,还需要识别复数。我想知道这是否可以在没有很多 if-else 子句的情况下优雅地解决。

【问题讨论】:

    标签: python nlp nltk


    【解决方案1】:

    如果您已有多词表达地名词典列表,您可以使用MWETokenizer,例如:

    >>> from nltk.tokenize import MWETokenizer
    >>> from nltk import sent_tokenize, word_tokenize
    
    >>> s = '''Good muffins cost $3.88\nin New York.  Please buy me
    ...     ... two of them.\n\nThanks.'''
    
    >>> mwe = MWETokenizer([('New', 'York'), ('Hong', 'Kong')], separator='_')
    
    
    >>> [mwe.tokenize(word_tokenize(sent)) for sent in sent_tokenize(s)]
    [['Good', 'muffins', 'cost', '$', '3.88', 'in', 'New_York', '.'], ['Please', 'buy', 'me', '...', 'two', 'of', 'them', '.'], ['Thanks', '.']]
    

    【讨论】:

    • 如果这如宣传的那样有效,这就是我正在寻找的解决方案。一会会报告。非常感谢!
    • 我很高兴这个答案有帮助。
    猜你喜欢
    • 2021-06-28
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    • 2016-12-19
    • 2022-01-03
    相关资源
    最近更新 更多