nltk新手,条件频率有问题

2024-09-30 02:19:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python和nltk非常陌生(我2小时前开始学习)。我要做的是:

Write a function GetAmbigousWords(corpus, N) that finds words in the corpus with more than N observed tags. This function should return a ConditionalFreqDist object where the conditions are the words and the frequency distribution indicates the tag frequencies for each word.

以下是我目前所做的:

def GetAmbiguousWords(corpus, number):
conditional_frequency = ConditionalFreqDist()
word_tag_dict = defaultdict(set)       # Creates a dictionary of sets
for (word, tag) in corpus:
    word_tag_dict[word].add(tag)

for taggedWord in word_tag_dict:
    if ( len(word_tag_dict[taggedWord]) >= number ):
        condition = taggedWord
        conditional_frequency[condition] # do something, I don't know what to do

return conditional_frequency

例如,以下是函数的行为方式:

^{pr2}$

我想知道我是在正确的轨道上还是完全偏离轨道?尤其是,我不太明白conditional frequency。在

提前谢谢。在


Tags: theinnumberforreturntagfunctioncorpus
1条回答
网友
1楼 · 发布于 2024-09-30 02:19:10

使用频率分布,可以收集文本中单词出现的频率:

text = "cow cat mouse cat tiger"

fDist = FreqDist(word_tokenize(text))

for word in fDist:
    print "Frequency of", word, fDist.freq(word)

这将导致:

^{pr2}$

现在,一个条件频率基本上是相同的,但是你添加了一个条件,在这个条件下你可以对频率进行分组。E、 g.按字长分组:

cfdist = ConditionalFreqDist()

for word in word_tokenize(text):
    condition = len(word)
    cfdist[condition][word] += 1

for condition in cfdist:
    for word in cfdist[condition]:
        print "Cond. frequency of", word, cfdist[condition].freq(word), "[condition is word length =", condition, "]"

这将打印:

Cond. frequency of cow 0.333333333333 [condition is word length = 3 ]
Cond. frequency of cat 0.666666666667 [condition is word length = 3 ]
Cond. frequency of tiger 0.5 [condition is word length = 5 ]
Cond. frequency of mouse 0.5 [condition is word length = 5 ]

希望有帮助。在

相关问题 更多 >

    热门问题