'Replacing a word in a sentence based on POS Tag
I'd like to replace all the propernouns in a sentence I generate and then generate 2-3 sentences additionally with the replaced nouns.
text = "Who won the Australian Open?"
word_punct_token = WordPunctTokenizer().tokenize(text)
stop_words = stopwords.words('english')
tokens = [x for x in word_punct_token if x not in stop_words]
data_tagset = nltk.pos_tag(tokens)
tagged = nltk.pos_tag(sample.split())
propernouns = [word for word,tag in tagged if tag == 'NNP']
propern_list = ['US Open', 'Smithsonian']
repl = random.choice(propern_list)
for word,pos in tagged:
if(pos=='NNP'):
word=repl
How can I replace Australian Open with US open and generate the same sentence(s)? - "Who won the US Open?", 'Who won the Smithsonian?"
Thanks for the help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
