-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtfidf_guesser_test.py
47 lines (35 loc) · 1.63 KB
/
tfidf_guesser_test.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
import unittest
from math import log
class StubDatabase:
def __init__(self):
self.guess_train_questions = []
def add(self, question):
self.guess_train_questions.append(question)
class StubQuestion:
def __init__(self, question, answer):
self.text = question
self.page = answer
class TestGuesser(unittest.TestCase):
def setUp(self):
from tfidf_guesser import TfidfGuesser
self.guesser = TfidfGuesser()
self.data = StubDatabase()
self.num_duplicates = 5
for ii in range(self.num_duplicates):
self.data.add(StubQuestion("Who is buried in Grant's tomb?", "Ulysses_S._Grant"))
self.data.add(StubQuestion("Who is buried in the Mausoleum at Halicarnassus?", "Mausolus"))
self.guesser.train(self.data)
def testSame(self):
guess = self.guesser.guess(["Who is buried in Grant's tomb?"], max_n_guesses=1)
self.assertEqual(guess[0][0][0], "Ulysses_S._Grant")
guess = self.guesser.guess(["Who is buried in the Mausoleum at Halicarnassus?"], max_n_guesses=1)
self.assertEqual(guess[0][0][0], "Mausolus")
def testSimilar(self):
guess = self.guesser.guess(["tomb"], max_n_guesses=self.num_duplicates + 1)
for ii in range(self.num_duplicates):
self.assertEqual(guess[0][ii][0], "Ulysses_S._Grant")
self.assertEqual(guess[0][ii][0], guess[0][0][0])
self.assertEqual(guess[0][self.num_duplicates][0], "Mausolus")
self.assertGreater(guess[0][0][1], guess[0][self.num_duplicates][1])
if __name__ == '__main__':
unittest.main()