【问题标题】:Finding total count for word form when many possible POS tags当有许多可能的 POS 标签时,查找单词形式的总数
【发布时间】:2019-10-29 21:56:59
【问题描述】:

我觉得我有一个愚蠢的问题,但不管怎样…… 我正在尝试从看起来像这样的数据:

a word form     lemma    POS                count of occurrance
same word form  lemma    Not the same POS   another count
same word form  lemma    Yet another POS    another count

到如下所示的结果:

the word form    total count    all possible POS and their individual counts 

所以例如我可以:

ring     total count = 100        noun = 40, verb = 60

我的数据保存在 CSV 文件中。我想做这样的事情:

for row in all_rows:
    if row[0] is the same as row[0] in the next row, add the values from row[3] together to get the total count

buuut 我似乎不知道该怎么做。帮助?

【问题讨论】:

  • 你不是说if column[0] is the same as column[0] in next row... 吗?
  • 嗯。我的想法是逐行进行,因为我的数据中有多个单词,并且我希望保持看起来相同但具有不同 POS 标签的单词形式的总数(敲钟,戴戒指)。因此,如果第 1 行的第 0 个元素(即单词形式)与第 2 行的第 0 个元素相同,则将来自这些行的第 3 个元素的值相加得到单词的总数形式。
  • .. 是的,从技术上讲是专栏。
  • 你是对的,实际上 row[0] 是一列,所以我不知道我为什么问这个问题,但最初我很困惑。谢谢

标签: python python-3.x nlp linguistics


【解决方案1】:

如果我理解正确,实现您需要的最简单方法是:

# Mocked CSV data
data = [
 ['a', 'lemma', 'pos', 1],
 ['a', 'lemma', 'pos1', 2],
 ['a', 'lemma', 'pos2', 3],
 ['b', 'lemma', 'pos', 5],
]

result = {}

for row in data:
  key = row[0]
  count = row[3]
  if key in result:
    result[key] += count
  else:
    result[key] = count

print(result)

结果:

{
  'a': 6,
  'b': 5
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-19
    • 2021-09-23
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多