Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用同义词替换句子中每个单词的Python程序_Python - Fatal编程技术网

用同义词替换句子中每个单词的Python程序

用同义词替换句子中每个单词的Python程序,python,Python,基本上,我想做的是创建一个程序,将一个句子/段落作为用户输入,查找每个单词的同义词,并用同义词替换该单词。到目前为止,我为实现这一点而创建的程序运行得很好,但存在一些纠结/人为错误/逻辑错误。这是我现在得到的: response=input("Enter what you want to thesaurize") orig=response #puts user input into a string num=orig.count(" ")+1 #finds number of words in

基本上,我想做的是创建一个程序,将一个句子/段落作为用户输入,查找每个单词的同义词,并用同义词替换该单词。到目前为止,我为实现这一点而创建的程序运行得很好,但存在一些纠结/人为错误/逻辑错误。这是我现在得到的:

response=input("Enter what you want to thesaurize")
orig=response #puts user input into a string
num=orig.count(" ")+1 #finds number of words in the sentence
orig=orig.split(" ") #turns the sentence into a list of its words
new=[] #creates a new list to put the new words in, in case I'd want to go back to the original sentence for any reason
for i in range (num):
        if orig[i] not in badWords: #makes sure that the word is not a no-synonym word like "the" or "be"
            new.insert(i, myFuncs.replace(orig[i])) #the replace function (which I put in a separate module for neatness purposes) looks up the word on thesaurus.com and replaces it with a synonym
        else:
            new.insert(i, orig[i]) #If the word is an excluded word, it simply leaves it alone and keeps it in the new sentence

final="" #creates an empty string to put the new words in
for j in range(0,num):
    final=final+new[j]+" "  #turns the list of new words into a string that can be printed
print(final)
同样,这运行得很好,但也存在一些问题。基本上,我已将其简化为发生的4个基本问题:

1) 该词没有同义词,但仍不在排除词列表中

2) 输入单词的错误含义,或返回在用户输入的上下文中没有意义的含义

3) 返回动词的错误时态,然后

4) 当输入一个名词时,该词的动词将返回,反之亦然(即,“我将烤鸡肉”变为“我将烤鸡肉”或类似的内容)

基本上,我可以手动修复所有这些问题,方法是让用户检查每个没有意义的单词,然后使用嵌套的if-else和其他控制结构来引导他们选择正确的单词,但我认为这对用户来说是乏味的,会毁了整个要点,尤其是当他们进入一个有很多单词的地方

所以基本上我在问,这些问题中哪一个可以自动化?也就是说,我有没有办法编写代码让计算机识别这些问题?修复它们是容易的部分,但实际上让程序识别逻辑错误而不是让用户处理是困难的部分。

您应该研究NLP(自然语言处理),尤其是词性标记()。词性标注将根据动词、名词等词类和单词的语法形式对文本语料库中的每个单词进行标注。一个值得研究的伟大Python库是自然语言工具包

下面是该项目网站上的一个小例子

>>> import nltk
>>> sentence = """At eight o'clock on Thursday morning
... Arthur didn't feel very good."""
>>> tokens = nltk.word_tokenize(sentence)
>>> tokens
['At', 'eight', "o'clock", 'on', 'Thursday', 'morning',
'Arthur', 'did', "n't", 'feel', 'very', 'good', '.']
>>> tagged = nltk.pos_tag(tokens)
>>> tagged[0:6]
[('At', 'IN'), ('eight', 'CD'), ("o'clock", 'JJ'), ('on', 'IN'),
('Thursday', 'NNP'), ('morning', 'NN')]
标记句子后,只提取要查找同义词的词类,将单词规范化为现在时,执行同义词查找,并将结果转换回正确的时态,然后替换所需的单词

解析单词的时态也可以通过NLP机制以及将单词从正常形式转换为特定时态来实现。

您应该研究NLP(自然语言处理),尤其是词性标记()。词性标注将根据动词、名词等词类和单词的语法形式对文本语料库中的每个单词进行标注。一个值得研究的伟大Python库是自然语言工具包

下面是该项目网站上的一个小例子

>>> import nltk
>>> sentence = """At eight o'clock on Thursday morning
... Arthur didn't feel very good."""
>>> tokens = nltk.word_tokenize(sentence)
>>> tokens
['At', 'eight', "o'clock", 'on', 'Thursday', 'morning',
'Arthur', 'did', "n't", 'feel', 'very', 'good', '.']
>>> tagged = nltk.pos_tag(tokens)
>>> tagged[0:6]
[('At', 'IN'), ('eight', 'CD'), ("o'clock", 'JJ'), ('on', 'IN'),
('Thursday', 'NNP'), ('morning', 'NN')]
标记句子后,只提取要查找同义词的词类,将单词规范化为现在时,执行同义词查找,并将结果转换回正确的时态,然后替换所需的单词


解析单词的时态也可以通过NLP机制以及将单词从正常形式转换为特定时态来实现。

这是语义解析领域中的一个难题。我们中的一些人是无法解决的。如果你不熟悉,可以看看自然语言处理和机器学习。为了检查重播的正确性,你可以使用任何软件将首字母和变化的句子翻译成其他语言。如果两个句子的翻译结果相同,那么您的替换是正确的。这是关于问题(2)和(4)的。你可以看看NLTK(Natural Language ToolKit)提供的支持查找同义词的工具。这是语义解析领域中的一个难题。我们中的一些人是无法解决的。如果你不熟悉,可以看看自然语言处理和机器学习。为了检查重播的正确性,你可以使用任何软件将首字母和变化的句子翻译成其他语言。如果两个句子的翻译结果相同,那么您的替换是正确的。这是关于问题(2)和(4)的。您可以看看NLTK(自然语言工具包),它提供了查找同义词的支持。