import itertools
import nltk
from nltk.corpus import wordnet
Define the input sentence
sentence = "I love playing tennis."
Tokenize the sentence into individual words
words = nltk.word_tokenize(sentence)
Define a list to store the new sentence with verb tenses replaced
new_sentence = []
Iterate through each word in the sentence
for word in words:
Get the synsets for the word
synsets = wordnet.synsets(word, pos=wordnet.VERB)
if synsets:
If the word has synsets, get the infinitive form of the verb
infinitive = synsets[0].lemmas()[0].name()
Use the WordNet built-in method to get the present, past, and present participle forms of the verb
present = wordnet._morphy(infinitive, wordnet.VERB)[0]
past = wordnet._morphy(infinitive, wordnet.VERB, 'past')[0]
present_participle = wordnet._morphy(infinitive, wordnet.VERB, 'present_participle')[0]
Generate all possible combinations of the verb forms using itertools
forms = (present, past, "loved", present_participle)
verb_combinations = itertools.product(forms, repeat=1)
Join the verb forms together into sentences
for verb_tuple in verb_combinations:
new_sentence.append(" ".join([word if verb_tuple[i] is None else verb_tuple[i] for i in range(len(verb_tuple))]))
else:
If the word doesn't have synsets, append it as is to the new sentence list
new_sentence.append(word)
Print all possible combinations of verb forms on separate lines
for combination in new_sentence:
print(combination)
Get all the verbs in the sentence
verbs = [word for word in words if wordnet.synsets(word, pos=wordnet.VERB)]
Generate all possible combinations of the verb forms using itertools
forms = [('love', 'loved'), ('play', 'played', 'playing')]
verb_combinations = itertools.product(*forms)
Iterate through each combination and replace the verbs in the new sentence
for verb_tuple in verb_combinations:
new_sentence = []
i = 0
for word in words:
if word in verbs:
new_sentence.append(verb_tuple[i])
i += 1
else:
new_sentence.append(word)
Join the words in the new sentence together
print(" ".join(new_sentence))
output for this code