How to remove quotes after removing stopwords from nltk?

走远了吗. 提交于 2019-12-13 02:17:24

问题


I had captured headers from newspapers,also i removed stopwords from headres but after removing stopwords the word comes with single quote,so i dont want these quote,for this i tried below code:

from nltk.corpus import stopwords
blog_posts=[]
stop = stopwords.words('english')+[
    '.',
    ',',
    '--',
    '\'s',
    '?',
    ')',
    '(',
    ':',
    '\'',
    '\'re',
    '"',
    '-',
    '}',
    '{',
    u'—',
   'a', 'able', 'about', 'above', 'according', 'accordingly', 'across', 'actually', 'after', 'afterwards', 'again', 'against', 'all', 'allow', 'allows', 'almost', 'alone', 'along', 'already', 'also', 'although', 'always', 'am', 'among', 'amongst', 'an', 'and', 'another', 'any', 'anybody', 
]
file=open("resources/ch05-webpages/newspapers/TOI2232014.csv","r+")
t=[i for i in file.read().split() if i not in stop]
blog_posts.append((t,))
print blog_posts

so the output of this code is:

[(['"\'Duplicates\'', 'BJP,', 'Jaswant', 'Singh', 'says"', '"Flight'],)]

But i want output like this:

 [([Duplicates,BJP,Jaswant,Singh,ays,Flight])]   

so what can i do for this output?


回答1:


t=[i.replace("\\","").replace("\"","").replace("\'",").strip() 
   for i in file.read().split() if i not in stop]

would be a sloppy way to do it, if you knew the exhaustive list of characters you wanted to remove.

If you know you only want letter characters you can

import re

t=[re.findall([a-aA-Z]+, i) for i in file.read().split() 
   if i not in stop]



回答2:


yahoo finally i got the answer for this question.

t=[i.replace("\'","").replace("?","").replace(":","").replace("\"","").replace("#","").strip() 
  for i in file.read().split() if i not in stop]
#blog_posts.append((t,))
p=' '.join(t)
blog_posts.append((p,))
print blog_posts


来源:https://stackoverflow.com/questions/22576781/how-to-remove-quotes-after-removing-stopwords-from-nltk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!