问题
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