Python 从给定的句子生成一个新句子,而不改变给定句子的含义
我想从一个给定的句子中生成一个新的句子,它的意思与给定的句子相同Python 从给定的句子生成一个新句子,而不改变给定句子的含义,python,python-3.x,tensorflow,nlp,spacy,Python,Python 3.x,Tensorflow,Nlp,Spacy,我想从一个给定的句子中生成一个新的句子,它的意思与给定的句子相同 from nltk.corpus import wordnet from nltk.tokenize import word_tokenize from random import randint from nltk.corpus import stopwords import nltk.data # Load a text file if required text = "How many holidays do I hav
from nltk.corpus import wordnet
from nltk.tokenize import word_tokenize
from random import randint
from nltk.corpus import stopwords
import nltk.data
# Load a text file if required
text = "How many holidays do I have?"
output = ""
Lisst=[]
a=10
while (len(Lisst)<a):
output = ""
# Load the pretrained neural net
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
# Tokenize the text
tokenized = tokenizer.tokenize(text)
#print('tokenized',tokenized,'\n')
# Get the list of words from the entire text
words = word_tokenize(text)
#print('words',words,'\n')
stop_words = set(stopwords.words('english'))
#print('stop_words',stop_words,'\n')
# Identify the parts of speech
tagged = nltk.pos_tag(words)
for i in range(0,len(words)):
replacements = []
# Only replace nouns with nouns, vowels with vowels etc.
for syn in wordnet.synsets(words[i]):
#print(syn)
# Do not attempt to replace proper nouns or determiners
if tagged[i][1] == 'NNP' or tagged[i][1] == 'DT':
#print('tag',tagged[i][1])
break
# The tokenizer returns strings like NNP, VBP etc
# but the wordnet synonyms has tags like .n.
# So we extract the first character from NNP ie n
# then we check if the dictionary word has a .n. or not
word_type = tagged[i][1][0].lower()
#print('type',word_type)
if syn.name().find("."+word_type+"."):
# extract the word only
#print('synname',syn.name().find("."+word_type+"."))
r = syn.name()[0:syn.name().find(".")]
#print(r)
replacements.append(r)
#print(replacements,'\n')
if len(replacements) > 0:
# Choose a random replacement
replacement = replacements[randint(0,len(replacements)-1)]
output = output + " " + replacement
#print(output)
else:
# If no replacement could be found, then just use the
# original word
output = output + " " + words[i]
#print('\n',output)
#print('Input:',text)
Lisst.append(output)
#print('Output:',output)
Lisst1=set(Lisst)
Lisst=list(Lisst1)
#print('Outputs',Lisst)
print('Input:',text)
print('Outputs',Lisst)
它改变了除DT限定词和NNP专有名词单数外的所有单词的同义词。大部分产出都没有意义。我希望输出是完整的。
如果我能写出一个全新的句子,意思也一样,那就太好了。例如:
Input: How many holidays do I have?
Outputs['Number of leaves do I have','Leaves that I can avail'.......,'']
推荐链接也受到欢迎
提前感谢:-) 我被困在一个类似的任务中。你找到解决办法了吗?或者如果你能指引一条路。提前感谢,我无法找到解决方案,但您可以查看对我有点帮助的链接。如果你不想改写句子,只想用最匹配的同义词替换句子中的单词,那么请查看wup_相似度。如果您提出了更好的方法,请分享详细信息。:-)谢谢你@Gnaneshwar Babu这真的很有帮助我被困在一个类似的任务中。你找到解决办法了吗?或者如果你能指引一条路。提前感谢,我无法找到解决方案,但您可以查看对我有点帮助的链接。如果你不想改写句子,只想用最匹配的同义词替换句子中的单词,那么请查看wup_相似度。如果您提出了更好的方法,请分享详细信息。:-)谢谢你@Gnaneshwar Babu这真的很有帮助
Input: How many holidays do I have?
Outputs['Number of leaves do I have','Leaves that I can avail'.......,'']