Package parsedatetime :: Package tests :: Module TestNlp
[hide private]
[frames] | no frames]

Source Code for Module parsedatetime.tests.TestNlp

 1   
 2  """ 
 3  Test parsing of strings that are phrases 
 4  """ 
 5   
 6  import unittest, time, datetime 
 7  import parsedatetime as pdt 
 8   
 9   
10  # a special compare function for nlp returned data 
11 -def _compareResults(result, check, dateOnly=False, debug=False):
12 target = result 13 value = check 14 15 if target is None and value is None: 16 return True 17 18 if (target is None and value is not None) or (target is not None and value is None): 19 return False 20 21 if len(target) != len(value): 22 return False 23 24 for i in range(0, len(target)): 25 target_date = target[i][0] 26 value_date = value[i][0] 27 28 if target_date.year != value_date.year or target_date.month != value_date.month or target_date.day != value_date.day or target_date.hour != value_date.hour or target_date.minute != value_date.minute: 29 return False 30 if target[i][1] != value[i][1]: 31 return False 32 if target[i][2] != value[i][2]: 33 return False 34 if target[i][3] != value[i][3]: 35 return False 36 if target[i][4] != value[i][4]: 37 return False 38 39 return True
40
41 -class test(unittest.TestCase):
42
43 - def setUp(self):
44 self.cal = pdt.Calendar() 45 self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime()
46
47 - def testNlp(self):
48 # note: these tests do not need to be as dynamic as the others because this is still based 49 # on the parse() function, so all tests of the actual processing of the datetime 50 # value returned are applicable to this. Here we are concerned with ensuring the 51 # correct portions of text and their positions are extracted and processed. 52 start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple() 53 target = ((datetime.datetime(2013, 8, 5, 20, 0), 3, 17, 37, 'At 8PM on August 5th'), 54 (datetime.datetime(2013, 8, 9, 21, 0), 2, 72, 90, 'next Friday at 9PM'), 55 (datetime.datetime(2013, 8, 1, 21, 30, 0), 2, 120, 132, 'in 5 minutes')) 56 57 # positive testing 58 self.assertTrue(_compareResults(self.cal.nlp("I'm so excited!! At 8PM on August 5th i'm going to fly to Florida" 59 ". Then next Friday at 9PM i'm going to Dog n Bone! And in 5 " 60 "minutes I'm going to eat some food!", start), target)) 61 62 target = datetime.datetime(self.yr, self.mth, self.dy, 17, 0, 0).timetuple() 63 64 # negative testing - no matches should return None 65 self.assertTrue(_compareResults(self.cal.nlp("I'm so excited!! So many things that are going to happen!!", start), None)) 66 67 # quotes should not interfere with datetime language recognition 68 target = self.cal.nlp("I'm so excited!! At '8PM on August 5th' i'm going to fly to Florida" 69 ". Then 'next Friday at 9PM' i'm going to Dog n Bone! And in '5 " 70 "minutes' I'm going to eat some food!", start) 71 72 self.assertTrue(target[0][4] == "At '8PM on August 5th") 73 self.assertTrue(target[1][4] == "next Friday at 9PM") 74 self.assertTrue(target[2][4] == "in '5 minutes")
75