【问题标题】:Find different realization of a word in a sentence string - Python在句子字符串中查找单词的不同实现 - Python
【发布时间】:2012-11-05 17:48:30
【问题描述】:

(这个问题是关于一般的字符串检查而不是自然语言处理本身,但如果您将其视为 NLP 问题,请想象它不是当前分析器可以分析的语言,为简单起见,我将使用英文字符串,例如)

假设一个单词只有 6 种可能的形式

  1. 首字母大写
  2. 它带有“s”的复数形式
  3. 带有“es”的复数形式
  4. 大写+“es”
  5. 大写+“s”
  6. 没有复数或大写的基本形式

假设我想找到第一个实例的索引,任何形式的单词coach 出现在一个句子中,有没有更简单的方法来执行这两种方法:

long if 条件

sentence = "this is a sentence with the Coaches"
target = "coach"

print target.capitalize()

for j, i in enumerate(sentence.split(" ")):
  if i == target.capitalize() or i == target.capitalize()+"es" or \
     i == target.capitalize()+"s" or i == target+"es" or i==target+"s" or \
     i == target:
    print j

迭代 try-except

variations = [target, target+"es", target+"s", target.capitalize()+"es",
target.capitalize()+"s", target.capitalize()]

ind = 0
for i in variations:
  try:
    j == sentence.split(" ").index(i)
    print j
  except ValueError:
    continue

【问题讨论】:

  • #2 和#3 相同。一个应该是“s”,另一个应该是“es”吗?
  • 使用正则表达式会简单得多。

标签: python string nlp stemming


【解决方案1】:

我推荐看看 NLTK 的 stem 包:http://nltk.org/api/nltk.stem.html

使用它,您可以“从单词中删除形态词缀,只留下词干。词干算法旨在删除那些所需的词缀,例如语法角色、时态、派生形态,只留下词干。”

如果当前 NLTK 未涵盖您的语言,则应考虑扩展 NLTK。如果你真的需要一些简单的东西并且不关心 NLTK,那么你仍然应该将你的代码编写为一个小的、易于组合的实用函数的集合,例如:

import string 

def variation(stem, word):
    return word.lower() in [stem, stem + 'es', stem + 's']

def variations(sentence, stem):
    sentence = cleanPunctuation(sentence).split()
    return ( (i, w) for i, w in enumerate(sentence) if variation(stem, w) )

def cleanPunctuation(sentence):
    exclude = set(string.punctuation)
    return ''.join(ch for ch in sentence if ch not in exclude)

def firstVariation(sentence, stem):
    for i, w  in variations(sentence, stem):
        return i, w

sentence = "First coach, here another two coaches. Coaches are nice."

print firstVariation(sentence, 'coach')

# print all variations/forms of 'coach' found in the sentence:
print "\n".join([str(i) + ' ' + w for i,w in variations(sentence, 'coach')])

【讨论】:

  • 它还不是 NLTK 词干分析器支持的语言。
  • 我正在尝试构建一个有限规则库系统,该系统可以涵盖 NLP 中的高精度,因此如果没有注释数据,基于统计分类的词干分析器将无法实现。但是 NLTK 应该是查找任何 NLP 相关任务的第一件事 =)
【解决方案2】:

形态学通常是一种有限状态现象,因此正则表达式是处理它的完美工具。使用如下函数构建一个匹配所有案例的 RE:

def inflect(stem):
    """Returns an RE that matches all inflected forms of stem."""
    pat = "^[%s%s]%s(?:e?s)$" % (stem[0], stem[0].upper(), re.escape(stem[1:]))
    return re.compile(pat)

用法:

>>> sentence = "this is a sentence with the Coaches"
>>> target = inflect("coach")
>>> [(i, w) for i, w in enumerate(sentence.split()) if re.match(target, w)]
[(6, 'Coaches')]

如果变形规则比这更复杂,请考虑使用Python's verbose REs

【讨论】:

    猜你喜欢
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 2016-05-28
    相关资源
    最近更新 更多