/
test_cfd_mutation.py
39 lines (32 loc) · 1.32 KB
/
test_cfd_mutation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import unittest
from nltk import ConditionalFreqDist, tokenize
class TestEmptyCondFreq(unittest.TestCase):
def test_tabulate(self):
empty = ConditionalFreqDist()
self.assertEqual(empty.conditions(),[])
try:
empty.tabulate(conditions="BUG") # nonexistent keys shouldn't be added
except:
pass
self.assertEqual(empty.conditions(), [])
def test_plot(self):
empty = ConditionalFreqDist()
self.assertEqual(empty.conditions(),[])
try:
empty.plot(conditions=["BUG"]) # nonexistent keys shouldn't be added
except:
pass
self.assertEqual(empty.conditions(),[])
def test_increment(self):
# make sure that we can still mutate cfd normally
text = "cow cat mouse cat tiger"
cfd = ConditionalFreqDist()
# create cfd with word length as condition
for word in tokenize.word_tokenize(text):
condition = len(word)
cfd[condition][word] += 1
self.assertEqual(cfd.conditions(), [3,5])
# incrementing previously unseen key is still possible
cfd[2]['hi'] += 1
self.assertEqual(set(cfd.conditions()),set([3,5,2])) # new condition added
self.assertEqual(cfd[2]['hi'], 1) # key's frequency incremented from 0 (unseen) to 1