forked from sbenthall/bluestocking
/
tests.py
96 lines (72 loc) · 2.81 KB
/
tests.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import doxament
import parse
import chunkerator
import random
import unittest
import nltk
class TestParserMethods(unittest.TestCase):
parser = None
def setUp(self):
text2 = "Ice cream is good. Spinach is bad. Today was not bad."
doc2 = parse.Document(text2)
self.parser = parse.Parser(doc2)
def test_make_relation(self):
r1 = self.parser.make_relation(("neg_foo","bar"))
self.assertTrue(not r1.co)
def test_strip_neg(self):
self.assertTrue(self.parser.strip_neg("neg_foo") == "foo")
def test_is_neg(self):
self.assertTrue(self.parser.is_neg('neg_foo'))
class GeneralTests(unittest.TestCase):
def setUp(self):
self.text1 = "Today was a good day. Yesterday was a bad day."
self.text2 = "Ice cream is good. Spinach is bad. Today was not bad."
doc1 = parse.Document(self.text1)
doc2 = parse.Document(self.text2)
self.dox1 = doc1.to_dox()
self.dox2 = doc2.to_dox()
self.kb = doxament.merge(self.dox1, self.dox2)
def test_queries(self):
print "Knowledge base documents"
print self.text1
print self.text2
print "Knowledge base relations"
pp(self.kb.relations)
self.counter = 1
def query(text,kb):
print "Query document %d" % (self.counter)
self.counter += 1
dox = parse.Document(text).to_dox()
print text
q = kb.query(dox)
print "Query result:"
print q
return q
self.assertTrue(query("Today was a good day because I ate ice cream.", self.kb)[0] > 0)
self.assertTrue(query("Yesterday was good because I ate spinach. Today was bad.",self.kb)[0] < 0)
self.assertTrue(doxament.Relation(False,'Today','good') in query("Today was not good. Ice cream is not good. Spinach is bad. I hate everything.",self.kb)[1])
self.assertTrue(query("Yesterday was good. Spinach is good.",self.kb)[0] < 0)
class TestChunkerator(unittest.TestCase):
def test_find_chunks(self):
rules = """
NP: {<DT>?<JJ>*<NN.*>}
NP: {<PRP>}
"""
c = Chunkerator(rules, True)
textChunk = 'I hate yellow snow because I know the black dogs are the ones who make the stuff.'
textChunk = textChunk.split()
outsent = c.chunk_sent(textChunk)
print c.chunksSeen
self.assertTrue(len(c.chunksSeen) == 5)
def test_chunked_in_output(self):
rules = """
NP: {<DT>?<JJ>*<NN.*>}
NP: {<PRP>}
"""
c = Chunkerator(rules,True)
textChunk = 'I hate yellow snow because I know the dogs are the ones who make the nasty stuff.'
textChunk = textChunk.split()
outsent = c.chunk_sent(textChunk)
self.assertTrue('dogs' in outsent)
if __name__ == '__main__':
unittest.main()