如何从nltk中删除停用词后删除引号?

时间:2014-03-22 11:06:50

标签: python-2.7 nltk stop-words

我从报纸上抓下了标题,我也从头文中删除了停用词,但在删除了停用词之后,这个单词带有单引号,所以我不想要这些引用,为此我尝试下面的代码:

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

所以这段代码的输出是:

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

但我想要这样的输出:

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

那么我能为这个输出做些什么呢?

2 个答案:

答案 0 :(得分:2)

t=[i.replace("\\","").replace("\"","").replace("\'",").strip() 
   for i in file.read().split() if i not in stop]
如果您知道要删除的详尽字符列表,那么

将是一种草率的方式。

如果你知道你只想要字母字符

import re

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

答案 1 :(得分:0)

雅虎终于得到了这个问题的答案。

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