【问题标题】:find and remove a word from sentence(in between the word matching) python从句子中查找并删除一个单词(在单词匹配之间)python
【发布时间】:2019-09-20 11:40:54
【问题描述】:

我有下面这样的句子

mainsentence="My words aren't available give didn't give apple and did happening me"

stopwords=['are','did','word', 'able','give','happen']

如果任何单词与中间的单词匹配,则要删除(例如:“word”应匹配“words”并将其删除,“did”应匹配“did't”并将其删除,“able”应删除“available” " 因为 'able' 词在 'available' 中

finalsentence="My apple and me"

用下面的代码试过了,但是

querywords = mainsentence.split()
resultwords  = [word for word in querywords if word.lower() not in stopwords]
result = ' '.join(resultwords)
print(result)

但它只适用于完全匹配。

请帮帮我。

【问题讨论】:

  • 这里的问题是你想要部分匹配,但a 将是你的大部分单词的部分匹配。
  • 另外,happen 应该在 finalsentence
  • @tituszban:更正了问题。
  • 听起来您需要检查的不是您的单词列表,而是您的单词列表的同义词列表。有几种方法可以做到这一点,一种是使用 PyDictionary:pypi.org/project/PyDictionary

标签: python python-3.7


【解决方案1】:

你可以做以下事情:

>>> ' '.join([word for word in mainsentence.split() if not any([stopword in word for stopword in stopwords])])
'My apple and me'

编辑:这不需要是双向检查,只需查看单词是否包含停用词
EDIT2:使用更新的问题参数更新结果

不区分大小写的版本:

' '.join([word for word in mainsentence.split() if not any([stopword.lower() in word.lower() for stopword in stopwords])])

【讨论】:

  • 注意区分大小写。
  • 您也不需要在any() 中创建中间list
【解决方案2】:

以下代码将满足您在问题中所述的要求,但结果可能不是您想要的。 代码的一般基础结构应该是正确的,但您可能需要更改部分匹配的条件(stopword in testword):

def filter_out_stopwords(text, stopwords):
    result = []
    for word in text.split():
        testword = word.lower()
        flag = True
        for stopword in stopwords:
            if stopword in testword:
                flag = False
                break
        if flag:
            result.append(word)
    return result


' '.join(filter_out_stopwords("My words aren't available give didn't give apple and did happening me", ['are', 'did', 'word', 'able', 'give', 'happen']))
# "My apple and me"

或者,使用列表理解和all()any() 可以等效使用):

def filter_out_stopwords(text, stopwords):                                                                                                   
    return [
        word for word in text.split()
        if all(stopword not in word.lower() for stopword in stopwords)]


' '.join(filter_out_stopwords("My words aren't available give didn't give apple and did happening me", ['are', 'did', 'word', 'able', 'give', 'happen']))
# "My apple and me"

【讨论】:

    【解决方案3】:

    您可以使用正则表达式的力量来解决这些问题。

    import re
    

    你可以像这样得到所有的数学单词:

    words = re.findall(r'[a-z]*did[a-z]*', mainsentence)
    
    

    您也可以替换它们:

    re.sub(r'[a-z]*able[a-z]* ', '', mainsentence)
    

    所以最后的答案:

    mainsentence="My words aren't available give didn't give apple and did happening me"
    
    stopwords=['are','did','word', 'able','give','happen']
    
    for word in stopwords:
        mainsentence = re.sub(fr'[a-z\']*{word}[a-z\']* ', '', mainsentence)
    # My apple and me
    

    【讨论】:

      【解决方案4】:

      您遇到的问题可以通过以下步骤获得可持续的解决方案。

      1. 像 Ive -> I have, didn't -> did not 一样展开单词。查看pycontractions
      2. 使用词的引理来获取每个词的基本形式,即将词的形式更改为其根形式。例子:playing,plays,played变成play。让我们将语料库的当前状态称为干净的语料库。查看lemmatization
      3. 现在从干净的语料库中删除所有停用词。

      您可能还会发现我写的一个有趣的text cleaning module,它还包括拼写校正,可用于制作文本清理管道。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-27
        • 2021-08-20
        • 1970-01-01
        • 2022-06-10
        • 1970-01-01
        • 1970-01-01
        • 2017-06-20
        相关资源
        最近更新 更多