【问题标题】:How to remove quotes after removing stopwords from nltk?从 nltk 中删除停用词后如何删除引号?
【发布时间】:2014-04-29 21:13:11
【问题描述】:

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

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])]   

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

【问题讨论】:

    标签: python-2.7 nltk stop-words


    【解决方案1】:
    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]
    

    【讨论】:

    • 它工作正常,但它没有删除单引号,它只在单引号内删除(/,',//),例如:'prashant's'->'prashant',但我想要->普拉尚特
    【解决方案2】:

    yahoo 我终于得到了这个问题的答案。

    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
    

    【讨论】:

    • 你使用了我的答案的一个非常轻微修改的版本,发布它然后接受它?
    猜你喜欢
    • 2013-05-12
    • 1970-01-01
    • 2015-01-20
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 2019-10-01
    相关资源
    最近更新 更多