我正在开发一个专注于持续学习的网络爬虫,以查找与世界各地发生的特定危机和悲剧事件相关的新闻文章。考虑到数据模型随着爬行的继续而不断增长,我目前正在努力使数据模型尽可能精简和高效。
我将数据模型存储在一个列表中(以便与被抓取的页面进行 TFIDF 比较)我想减少列表的大小但又不想丢失的相对计数列表中的每一项。
这是来自 2 个已抓取网页的示例模型:
[[u'remark', u'special', u'agent', u'richard', u'deslauri', u'press', u'investig', u'crime', u'terror', u'crime', u'inform', u'servic', u'inform', u'laboratori', u'servic', u'want', u'want', u'want', u'terror', u'crime', u'want', u'news', u'news', u'press', u'news', u'servic', u'crime', u'inform', u'servic', u'laboratori', u'servic', u'servic', u'crime', u'crime', u'crime', u'terror', u'boston', u'press', u'remark', u'special', u'agent', u'richard', u'deslauri', u'press', u'investig', u'remark', u'special', u'agent', u'richard', u'deslauri', u'press', u'investig', u'boston', u'special', u'agent', u'remark', u'richard', u'deslauri', u'boston', u'investig', u'time', u'time', u'investig', u'boston', u'terror', u'law', u'enforc', u'boston', u'polic', u'polic', u'alreadi', u'alreadi', u'law', u'enforc', u'around', u'evid', u'boston', u'polic', u'evid', u'laboratori', u'evid', u'laboratori', u'may', u'alreadi', u'laboratori', u'investig', u'boston', u'polic', u'law', u'enforc', u'investig', u'around', u'alreadi', u'around', u'investig', u'law', u'enforc', u'evid', u'may', u'time', u'may', u'may', u'investig', u'may', u'around', u'time', u'investig', u'investig', u'boston', u'boston', u'news', u'press', u'boston', u'want', u'boston', u'want', u'news', u'servic', u'inform'], [u'2011', u'request', u'inform', u'tamerlan', u'tsarnaev', u'foreign', u'govern', u'crime', u'crime', u'inform', u'servic', u'inform', u'servic', u'nation', u'want', u'ten', u'want', u'want', u'crime', u'want', u'news', u'news', u'press', u'releas', u'news', u'stori', u'servic', u'crime', u'inform', u'servic', u'servic', u'servic', u'crime', u'crime', u'crime', u'news', u'press', u'press', u'releas', u'2011', u'request', u'inform', u'tamerlan', u'tsarnaev', u'foreign', u'govern', u'2011', u'request', u'inform', u'tamerlan', u'tsarnaev', u'foreign', u'govern', u'2013', u'nation', u'press', u'tamerlan', u'tsarnaev', u'dzhokhar', u'tsarnaev', u'tamerlan', u'tsarnaev', u'dzhokhar', u'tsarnaev', u'dzhokhar', u'tsarnaev', u'tamerlan', u'tsarnaev', u'dzhokhar', u'tsarnaev', u'2011', u'foreign', u'govern', u'inform', u'tamerlan', u'tsarnaev', u'inform', u'2011', u'govern', u'inform', u'tamerlan', u'tsarnaev', u'foreign', u'foreign', u'govern', u'2011', u'inform', u'foreign', u'govern', u'nation', u'press', u'releas', u'crime', u'releas', u'ten', u'news', u'stori', u'2013', u'ten', u'news', u'stori', u'2013', u'ten', u'news', u'stori', u'2013', u'2011', u'request', u'inform', u'tamerlan', u'tsarnaev', u'foreign', u'govern', u'nation', u'press', u'releas', u'want', u'news', u'servic', u'inform', u'govern']]
我想维护单词列表而不是将计数嵌入到列表本身中。我希望列表来自:
[Boston, Boston,Boston,Bombings,Bombings,Tsarnaev,Tsarnaev,Time] 到 [Boston,Boston,Bombings,Tsarnaev]
基本上,如果我有一个列表 [a,a,a,b,b,c],我想将它缩减为 [a,a,b]
编辑: 很抱歉不清楚,但我会再试一次。 我不想要一套。出现次数非常重要,因为它是一个加权列表,因此“Boston”出现的次数应该多于“time”或其他类似术语。我想要完成的是最小化数据模型,同时从模型中删除无关紧要的项。所以在上面的例子中,我故意省略了 C,因为它给模型增加了很多“脂肪”。我想保持相对性,因为 A 比 B 多出现 1 次,比 C 多出现 2 次,但由于 C 在原始模型中只出现一次,因此它正在从精益模型中删除。 p>
请您参考如下方法:
from collections import defaultdict
d = defaultdict(int)
for w in words[0]:
d[w] += 1
mmin = min(d[p] for p in d)
然后你可以从每个单词中减去这个 mmin 并创建一个新列表。但也许 dict 足够紧凑。为了保持顺序,您可以使用字典中的信息并设计一些巧妙的方法来过滤您的初始单词列表。
例如,对于单词列表[a,a,a,b,b,c],字典将包含{a:3, b:2, c:1 } 和 mmin=1。您可以通过从所有项目中减去 1 得到 {a:2, b:1} 并因为 c 是 0 它被删除了。
完整代码:
from collections import defaultdict
d = defaultdict(int)
words = ['a','a','a','b','b','c']
for w in words:
d[w] += 1
mmin = min(d[p] for p in d)
slim=[]
for w in words:
if d[w] > mmin:
slim.append(w)
d[w] -= 1
print slim
