python - 在 Python 中查找单词的所有变体(或时态)

标签 python machine-learning nlp artificial-intelligence nltk

我想知道如何在Python中找到一个单词的所有变体,或者与原始单词相关或非常相似的单词。

我正在寻找的事情的一个例子是这样的:

word = "summary" # any word

word_variations = find_variations_of_word(word) # a function that finds all the variations of a word, What i want to know how to make

print(word_variations)

# What is should print out: ["summaries", "summarize", "summarizing", "summarized"]

这只是代码应该做什么的一个例子,我见过关于同一主题的其他类似问题,但它们都不够准确,我找到了一些代码并将其更改为我自己的,这有点工作,但是现在按照我想要的方式进行。

import nltk
from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer

lemmatizer = WordNetLemmatizer()

def find_inflections(word):
    inflections = []
    for synset in wordnet.synsets(word):  # Find all synsets for the word
        for lemma in synset.lemmas():  # Find all lemmas for each synset
            inflected_form = lemma.name().replace("_", " ")  # Get the inflected form of the lemma
            if inflected_form != word:  # Only add the inflected form if it's different from the original word
                inflections.append(inflected_form)
    return inflections

word = "summary"
inflections = find_inflections(word)
print(inflections)  
# Output: ['sum-up', 'drumhead', 'compendious', 'compact', 'succinct']
# What the Output should be: ["summaries", "summarize", "summarizing", "summarized"]

最佳答案

这可能对您没有任何用处,但可能会帮助其他通过搜索找到此内容的人 -

如果目的只是找到单词,而不是专门使用机器学习方法来解决问题,您可以尝试使用正则表达式 (regex)。

w3学校似乎覆盖了足够的内容来获得你想要的结果here或者 python.org 有更多技术概述

要不区分大小写地搜索您列出的特定单词,可以使用以下方法:

import re

string =    "A SUMMARY ON SUMMATION:" \
            "We use summaries to summarize. This action is summarizing. " \
            "Once the action is complete things have been summarized."


occurrences = re.findall("summ[a-zA-Z]*", string, re.IGNORECASE)
    
print(occurrences)

但是,根据您的具体需求,您可能需要修改正则表达式,因为这也会找到“summer”和“summon”等词。

我不太擅长正则表达式,但如果您准确地知道自己在寻找什么,并花一点时间制作正确的表达式,那么它们可以成为一个强大的工具。

抱歉,这可能与您的情况无关,但祝您好运。

关于python - 在 Python 中查找单词的所有变体(或时态),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75024812/

相关文章:

Python mlpy 文本分类

hadoop - Mahout - 朴素贝叶斯模型非常慢

Java:如何使用TF-IDF计算两个文档的相似度?

python - 在 for 循环中

python - 从 Arduino Yun 执行 MySQL 脚本(无需 Python)

python - 为什么在许多实现中变分自动编码器的损失与纸上的符号相反?

keras - 嵌入 Keras

machine-learning - 新闻文章在线聚类

python - 向量化/优化此 python 代码的最佳方法是什么?

python - 如何检查字符串是否包含两个括号之间的数字并返回位置?