Andrea
Andrea

Reputation: 47

How to change POS list into normal string

I am writing a program to count the number of single occurrences of words but first I need to eliminate certain elements from the text. I have already managed to lowercase the text, to change negative contractions (n't -> not)and delete possessive endings (Tom's -> Tom). Now the final output is the tagged file.

import nltk 
import re
from nltk import pos_tag
from nltk.tokenize import word_tokenize
from string import punctuation

txt = "I don't like it. She didn't like it at all. I went to Susie's. She is playing."

y=txt.lower()#I lowercase the text
word_tokens = word_tokenize(y)

def decontracted(phrase):#how to change negative contractions
phrase = re.sub(r"n\'t", " not", phrase)
return phrase

d=(decontracted(y))
print(d)

x=pos_tag(word_tokenize(d))#POS tagging
y=[s for s in x if s[1] != 'POS']#I delete POS possessive ending
print(y)

When I print(y) the result is:

[('i', 'NNS'), ('do', 'VBP'), ('not', 'RB'), ('like', 'IN'), ('it', 'PRP'), ('.', '.'), ('she', 'PRP'), ('did', 'VBD'), ('not', 'RB'), ('like', 'IN'), ('it', 'PRP'), ('at', 'IN'), ('all', 'DT'), ('.','.'), ('i', 'VB'), ('went', 'VBD'), ('to', 'TO'), ('susie', 'VB'),('.', '.'), ('she', 'PRP'), ('is', 'VBZ'), ('playing', 'VBG'), ('.', '.')]

How can I change it to the following output?

['i', 'do', 'not', 'like', 'it', '.', 'she', 'did', 'not', 'like','it', 'at', 'all', '.', 'i', 'went', 'to', 'susie', '.', 'she', 'is', 'playing', '.']

How can I change it to the following output?

[i do not like it. she did not like it at all. i went to susie. she is playing.]

Thank you in advance

Upvotes: 1

Views: 198

Answers (2)

AnkushRasgon
AnkushRasgon

Reputation: 820

y=[('i', 'NNS'), ('do', 'VBP'), ('not', 'RB'), ('like', 'IN'), ('it', 'PRP'), ('.', '.'), ('she', 'PRP'), ('did', 'VBD'), ('not', 'RB'), ('like', 'IN'), ('it', 'PRP'), ('at', 'IN'), ('all', 'DT'), ('.','.'), ('i', 'VB'), ('went', 'VBD'), ('to', 'TO'), ('susie', 'VB'),('.', '.'), ('she', 'PRP'), ('is', 'VBZ'), ('playing', 'VBG'), ('.', '.')]

result=[x[0] for x in y] //to get the first word of a tuple in a list

print(result)

OUTPUT:
['i', 'do', 'not', 'like', 'it', '.', 'she', 'did', 'not', 'like', 'it', 'at', 'all', '.', 'i', 'went', 'to', 'susie', '.', 'she', 'is', 'playing', '.']

print(" ".join(result)) //join the words

OUTPUT:
i do not like it . she did not like it at all . i went to susie . she is playing .

Upvotes: 0

marc
marc

Reputation: 914

Here is a way to do it.

y = [('i', 'NNS'), ('do', 'VBP'), ('not', 'RB'), ('like', 'IN'), ('it', 'PRP'), ('.', '.'), ('she', 'PRP'), ('did', 'VBD'), ('not', 'RB'), ('like', 'IN'), ('it', 'PRP'), ('at', 'IN'), ('all', 'DT'), ('.','.'), ('i', 'VB'), ('went', 'VBD'), ('to', 'TO'), ('susie', 'VB'),('.', '.'), ('she', 'PRP'), ('is', 'VBZ'), ('playing', 'VBG'), ('.', '.')]

w = [r[0] for r in y]
print(w)
# ['i', 'do', 'not', 'like', 'it', '.', 'she', 'did', 'not', 'like', 'it', 'at', 'all', '.', 'i', 'went', 'to', 'susie', '.', 'she', 'is', 'playing', '.']

wStr = " ".join(w)
print(wStr)
# i do not like it . she did not like it at all . i went to susie . she is playing .

string = wStr.replace(' .', '.')
print(string)

# i do not like it. she did not like it at all. i went to susie. she is playing.

Upvotes: 2

Related Questions