平均 POS-TAG 频率

Average POS-TAG Frequency

我想获取这个标记文本(格式如此)并找出每个句子中后标记 DT 的平均频率。前任。 DT 在 sentence1 中出现 1/3 词,在 sentence2 中出现 1/3 词。然后我想将这些加起来并除以文本中的句子数(在本例中为 2)。这将给我每个句子 DT 的平均外观。

 from collections import Counter
 import nltk

 tagged_text = [('A', 'DT'), ('hairy', 'NNS'), ('dog', 'NN')]
 [('The', 'DT'), ('mischevious', 'NNS'), ('elephant', 'NN')]

 for eachSentence in tagged_text:
     Counter(tag for word,tag in tagged)/len(eachsentence.split())

 total = sum(counts.values())

 float(average) = sum(counts.values())/len(tagged_text.sents())
 print(float(average))

对我来说最大的问题是 eachSentence 部分,我不知道如何解决(我不知道如何定义它是什么)。我希望这段代码能够应用于数百个具有相同格式的句子。我知道代码有很多问题,所以如果有人可以纠正它们,我将不胜感激。

我(也)不确定您在寻找什么。在尝试将其放入代码之前,也许您应该尝试更多地构建您的 idea/requirements(在您的 head/on 论文中)。 根据你的描述和代码,我可以想到你想要的两个可能的数字,可以通过以下方式获得:

from collections import defaultdict

tagged_text = [[('A', 'DT'), ('hairy', 'NNS'), ('dog', 'NN')], [('The', 'DT'), ('mischevious', 'NNS'), ('elephant', 'NN')]]

d = defaultdict(int)
t = 0
for sentence in tagged_text:
    for tupl in sentence:
        tag = tupl[1]
        d[tag] += 1
        t += 1

for tag in d:
    print("Likelihood that %s appears in a sentence: %s" % (tag, str(float(d[tag] / len(tagged_text)))))
    print("Likelihood of %s appearing in complete corpus: %s" % (tag, str(float(d[tag] / t))))

导致

Likelihood that NN appears in a sentence: 1.0
Likelihood of NN in complete corpus: 0.3333333333333333
Likelihood that NNS appears in a sentence: 1.0
Likelihood of NNS in complete corpus: 0.3333333333333333
Likelihood that DT appears in a sentence: 1.0
Likelihood of DT in complete corpus: 0.3333333333333333

所有三个标签都出现在两个句子中,因此它出现在一个句子中的可能性为 1。所有三个标签都出现两次(总共六个),因此它们出现的可能性为 1/3(与句子分布无关)。 不过话又说回来,不确定这是不是你想要的。