【问题标题】:How to filter out dictionary keys based on their frequencies in a corpus?如何根据语料库中的频率过滤掉字典键?
【发布时间】:2019-04-17 14:53:33
【问题描述】:

所以,我正在做一个任务,我被困在这部分。我有一个字典,其中包含一个字符串元组作为键和相应的值。现在我必须通过使用 paras 方法删除布朗语料库文档中出现次数少于 8 次的键来过滤字典

我到处寻找它,但找不到任何关于如何做到这一点的伪代码。

[{('love', 'sex'): '6.77',
  ('tiger', 'cat'): '7.35',
  ('tiger', 'tiger'): '10.00',
  ('book', 'paper'): '7.46',
  ('computer', 'keyboard'): '7.62',
  ('computer', 'internet'): '7.58',
  ('plane', 'car'): '5.77',
  ('train', 'car'): '6.31',
  ('telephone', 'communication'): '7.50',
  ('television', 'radio'): '6.77',
  ('media', 'radio'): '7.42',
  ('drug', 'abuse'): '6.85',
  .
  . 
  .


所以我对这本字典要做的是,我应该删除键,其标记(单词对)不是按字母顺序排列的,以及至少一个单词有文档的单词对(键)棕色语料库中低于8的频率

【问题讨论】:

  • 我可以为您提供帮助,但您能否提供一个(或几个)示例输入和示例输出,也涵盖边缘情况?我宁愿不必深入研究“棕色语料库”和“文档频率”的定义......
  • @Error-SyntacticalRemorse 我不明白边缘情况下的输入/输出是什么意思。
  • 不要介意边缘情况。提供足够详细的示例输入,以满足您的需求,并为该输入提供您想要的输出。
  • @Error-SyntacticalRemorse 输入是您在问题中看到的字典。它有153个整体。所以,我要做的是删除单词对(键)不是按字母顺序排列的条目。然后,我必须计算文档集合(布朗语料库)中每个单词(不是单词对,单个单词)的文档频率,并删除单词对中至少一个单词具有文档频率的键小于 8

标签: python-3.x nltk corpus


【解决方案1】:

我不知道document 在这种情况下是什么,所以这个答案可能有缺陷。

输入:

mylist = [{('love', 'sex'): '6.77',
  ('tiger', 'cat'): '7.35',
  ('tiger', 'tiger'): '10.00',
  ('book', 'paper'): '7.46',
  ('computer', 'keyboard'): '7.62',
  ('computer', 'internet'): '7.58',
  ('computer', 'car'): '7.58',
  ('computer', 'plane'): '7.58',
  ('computer', 'train'): '7.58',
  ('computer', 'television'): '7.58',
  ('computer', 'radio'): '7.58',
  ('computer', 'tiger'): '7.58',
  ('computer', 'test1'): '7.58',
  ('computer', 'test2'): '7.58',
  ('tiger', 'tz1'): '7.58',
  ('tiger', 'tz2'): '7.58',
  ('tiger', 'tz3'): '7.58',
  ('tiger', 'tz4'): '7.58',
  ('tiger', 'tz5'): '7.58',
  ('tiger', 'tz6'): '7.58',
  ('tiger', 'tz7'): '7.58',
  ('tiger', 'tz8'): '7.58',
  ('plane', 'car'): '5.77',
  ('train', 'car'): '6.31',
  ('telephone', 'communication'): '7.50',
  ('television', 'radio'): '6.77',
  ('media', 'radio'): '7.42',
  ('drug', 'abuse'): '6.85'}]

解决方案: 请注意,该解决方案必须遍历字典两次(尽管第二次循环通常只会遍历字典的一部分)。我还将列表中的每个字典作为自己的东西处理,因此您可能需要移动一些语句。

# This will be the keys we want to remove
removable_keys = set()

# This will be the number of times we see a key part (left or right)
occurences = dict()

# For each dictionary in our list
for dic in mylist:
    # For each key in that dictionary
    for key in dic:
        # If the key is not in alphabetical order
        if list(key) != sorted(list(key)):
            # We will remove that key
            removable_keys.add(key)
        # Else this is a valid key
        else:
            # Increment the number of times we have seen this key
            left, right = key
            occurences[left] = 1 if left not in occurences else occurences[left] + 1
            occurences[right] = 1 if right not in occurences else occurences[right] + 1
    # No we need to look for keys that had less than 8 occurences.
    for key in dic.keys() - removable_keys:
        left, right = key
        if occurences[left] < 8 or occurences[right] < 8:
            removable_keys.add(key)
    # Finally remove all those keys from our dict
    for key in removable_keys:
        del dic[key]
    print(dic)

输出:

{('tiger', 'tiger'): '10.00', ('computer', 'tiger'): '7.58'}

【讨论】:

  • 感谢您的解决方案。这部分是我想要的,文档频率部分我解释得不好,所以让我说清楚。语料库是文档的集合。在这种情况下,语料库是一个布朗语料库,它是来自各种已发表文章的文本集合(en.wikipedia.org/wiki/Brown_Corpus)因此,该集合中的每个段落都被视为一个文档。我想要的是计算这些文档在键中包含左右单词的频率,我们必须删除左右单词的文档频率低于 8 的键
  • 我们应该计算对中单个单词的文档频率。我们不需要将频率计算为一对
  • 我不会将它们计算为一对。我单独做。哪个方面不正常?如果您提供您想要的输入和您期望的输出,这将使我们的生活更轻松。
  • 好的。我们应该计算这个词出现的段落数的频率。这些段落在这个名为布朗语料库的大集合中。 list1 = brown.paras() 此命令从集合中获取所有段落并将其存储在列表 1 变量中。当您查看列表 1 时,存储的数据有点像这样。 [[['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an', 'investigation', 'of', "Atlanta's", 'recent', 'primary', 'election', 'produced', '``', 'no', 'evidence', "''", 'that', 'any', 'irregularities', 'took', 'place', '.']],...]
  • 所以,我想要的是单词love 或字典中的任何单词出现的段落数。而如果这个词的段落频率小于8,那么key就是要去掉
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-28
  • 2014-09-01
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多