替换一个特定的词

n1c9

该脚本从许多新闻站点获取头条新闻,并统计头条新闻出现的次数。

我得到诸如“ to”,“ for”之类的词,以及那些我不打算在此脚本中抢夺的类似词。

我尝试编写一个str.translate(None,“ to”)来删除“ to”一词,但是却删除了“贪婪的”一词-当我要删除的只是“ to”一词时,将华盛顿的部分地抢购一空。

import pprint
import feedparser
from collections import Counter

def feedGrabber(feed):
    parsed = feedparser.parse(feed)
    feed1 = []
    feed1.append(parsed.entries[0].title)
    feed1.append(parsed.entries[1].title)
    feed1.append(parsed.entries[3].title)
    feed1.append(parsed.entries[4].title)
    feed1.append(parsed.entries[5].title)
    feed1.append(parsed.entries[6].title)
    feed1.append(parsed.entries[7].title)
    feed1.append(parsed.entries[8].title)
    feed1.append(parsed.entries[9].title)
    feed1 = str(feed1)
    feedsplit = feed1
    feedsplit = feedsplit.translate(None, '\'')
    feedsplit = feedsplit.translate(None, 'u')
    feedsplit = feedsplit.translate(None, '[')
    feedsplit = feedsplit.translate(None, ']')
    feedsplit = str.lower(feedsplit)
    feedsplit = str.split(feedsplit)
    return(feedsplit)

reddit = feedGrabber("https://www.reddit.com/r/news/.rss")
cnn = feedGrabber('http://rss.cnn.com/rss/cnn_topstories.rss')
nyt = feedGrabber('http://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml')

one = Counter(reddit)
two = Counter(cnn)
three = Counter(nyt)
pprint.pprint(one + two + three)
遗愿

这是一些常用单词的列表,您可以使用列表理解将其从文本中删除

text = [ x for x in  text if not isCommon(x)]


   def isCommon(word):

    commonWords = ["the", "be", "and", "of", "a", "in", "to", "have", "it",
        "i", "that", "for", "you", "he", "with", "on", "do", "say", "this",
        "they", "is", "an", "at", "but","we", "his", "from", "that", "not",
        "by", "she", "or", "as", "what", "go", "their","can", "who", "get",
        "if", "would", "her", "all", "my", "make", "about", "know", "will",
        "as", "up", "one", "time", "has", "been", "there", "year", "so",
        "think", "when", "which", "them", "some", "me", "people", "take",
        "out", "into", "just", "see", "him", "your", "come", "could", "now",
        "than", "like", "other", "how", "then", "its", "our", "two", "more",
        "these", "want", "way", "look", "first", "also", "new", "because",
        "day", "more", "use", "no", "man", "find", "here", "thing", "give",
        "many", "well"]

    if word in commonWords:
        return True
    return False

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我想替换一个故事中的一个词

如何在另一个搜索词中搜索和替换一个词

如何替换一个词,除非它紧跟在另一个词之后

sed查找并替换一个特定的数字

替换一个特定对象的类方法

用另一个词替换一个词的每个实例,而不破坏包含该词的其他词

用另一个词替换一个词,而不替换另一个在 Javascript 中的同一个句子中包含相似子串的词

根据另一个因素替换一个特定因素

用文件中的另一个单词替换一个特定的单词

替换一个JavaScript变量

在字典中定义规则后,在pandas数据帧(词频)中替换一个词

一旦执行OnPressed,就用一个特定的Widget替换一个特定的Widget

查询MongoDB,用正则表达式替换一个特定字段

AWK-仅在特定列中替换一个字符

根据另一个df中的特定索引替换一个df中的值

我想用一个特定的数字替换一个字符,为此输入什么公式?

Python-多次替换一个单词

替换一个对象内的钩子 useState

用sed替换一个以上特殊字符

Java replaceAll()仅替换一个实例

删除片段并替换一个新片段

从日期起替换一个额外的数字

我想搜索并替换一个模式

jquery - 从克隆的 html 中替换一个元素

在变量中替换一个 href 地址

为什么可以替换一个新的片段,而不替换一个初始化的片段?

想要在Linux文件的2个字符串之间替换一个特定的单词

如何在字符串的特定索引处替换一个字符

想要在VB 6.0中的特定位置替换一个字符