我正在编写一个程序来计算单词的单次出现次数,但是首先我需要从文本中消除某些元素。我已经设法小写文本,更改负收缩(不是->不是)并删除所有格结尾(汤姆->汤姆)。 现在最终的输出是标记的文件。
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)
当我打印(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'), ('.', '.')]
如何将其更改为以下输出?
['i', 'do', 'not', 'like', 'it', '.', 'she', 'did', 'not', 'like','it', 'at', 'all', '.', 'i', 'went', 'to', 'susie', '.', 'she', 'is', 'playing', '.']
如何将其更改为以下输出?
[i do not like it. she did not like it at all. i went to susie. she is playing.]
提前谢谢
答案 0 :(得分:1)
这是一种方法。
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.
答案 1 :(得分:0)
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 .