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, sys 
  7  import parsedatetime as pdt 
8 9 -class test(unittest.TestCase):
10 11 # a special compare function for nlp returned data 12 @pdt.tests.assertEqualWithComparator
13 - def assertExpectedResult(self, result, check, dateOnly=False):
14 target = result 15 value = check 16 17 if target is None and value is None: 18 return True 19 20 if (target is None and value is not None) or (target is not None and value is None): 21 return False 22 23 if len(target) != len(value): 24 return False 25 26 for i in range(0, len(target)): 27 target_date = target[i][0] 28 value_date = value[i][0] 29 30 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: 31 return False 32 if target[i][1] != value[i][1]: 33 return False 34 if target[i][2] != value[i][2]: 35 return False 36 if target[i][3] != value[i][3]: 37 return False 38 if target[i][4] != value[i][4]: 39 return False 40 41 return True
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 testLongPhrase(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), 3, 72, 90, 'next Friday at 9PM'), 55 (datetime.datetime(2013, 8, 1, 21, 30, 0), 2, 120, 132, 'in 5 minutes'), 56 (datetime.datetime(2013, 8, 8, 9, 0), 1, 173, 182, 'next week')) 57 58 # positive testing 59 self.assertExpectedResult(self.cal.nlp("I'm so excited!! At 8PM on August 5th i'm going to fly to Florida" 60 ". Then next Friday at 9PM i'm going to Dog n Bone! And in 5 " 61 "minutes I'm going to eat some food! Talk to you next week.", start), target) 62 63 target = datetime.datetime(self.yr, self.mth, self.dy, 17, 0, 0).timetuple()
64
65 - def testQuotes(self):
66 # quotes should not interfere with datetime language recognition 67 start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple() 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! Talk to you \"next week\"", start) 71 72 self.assertEqual(target[0][4], "At '8PM on August 5th") 73 self.assertEqual(target[1][4], "next Friday at 9PM") 74 self.assertEqual(target[2][4], "in '5 minutes") 75 self.assertEqual(target[3][4], "next week")
76
77 - def testPrefixes(self):
78 # nlp has special handling for on/in/at prefixes 79 start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple() 80 81 target = self.cal.nlp("Buy a balloon on Monday", start) 82 self.assertEqual(target[0][4], "on Monday") 83 84 target = self.cal.nlp("Buy a balloon at noon", start) 85 self.assertEqual(target[0][4], "at noon") 86 87 target = self.cal.nlp("Buy a balloon in a month", start) 88 self.assertEqual(target[0][4], "in a month") 89 90 # Should no longer pull "on" off the end of balloon 91 target = self.cal.nlp("Buy a balloon Monday", start) 92 self.assertEqual(target[0][4], "Monday")
93
94 - def testFalsePositives(self):
95 # negative testing - no matches should return None 96 start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple() 97 self.assertExpectedResult(self.cal.nlp("Next, I'm so excited!! So many things that are going to happen every week!!", start), None) 98 self.assertExpectedResult(self.cal.nlp("$300", start), None) 99 self.assertExpectedResult(self.cal.nlp("300ml", start), None)
100