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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
# -*- coding: utf-8 -*-
"""
Test parsing of strings that are phrases
"""
from __future__ import unicode_literals
import sys
import time
import datetime
import parsedatetime as pdt
from . import utils
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
class test(unittest.TestCase):
# a special compare function for nlp returned data
@utils.assertEqualWithComparator
def assertExpectedResult(self, result, check, dateOnly=False):
target = result
value = check
if target is None and value is None:
return True
if (target is None and value is not None) or \
(target is not None and value is None):
return False
if len(target) != len(value):
return False
for i in range(0, len(target)):
target_date = target[i][0]
value_date = value[i][0]
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:
return False
if target[i][1] != value[i][1]:
return False
if target[i][2] != value[i][2]:
return False
if target[i][3] != value[i][3]:
return False
if target[i][4] != value[i][4]:
return False
return True
def setUp(self):
self.cal = pdt.Calendar()
(self.yr, self.mth, self.dy, self.hr,
self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime()
def testLongPhrase(self):
# note: these tests do not need to be as dynamic as the others because
# this is still based on the parse() function, so all tests of
# the actual processing of the datetime value returned are
# applicable to this. Here we are concerned with ensuring the
# correct portions of text and their positions are extracted and
# processed.
start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple()
target = ((datetime.datetime(2013, 8, 5, 20, 0),
3, 17, 37, 'At 8PM on August 5th'),
(datetime.datetime(2013, 8, 9, 21, 0),
3, 72, 90, 'next Friday at 9PM'),
(datetime.datetime(2013, 8, 1, 21, 30, 0),
2, 120, 132, 'in 5 minutes'),
(datetime.datetime(2013, 8, 8, 9, 0),
1, 173, 182, 'next week'))
# positive testing
self.assertExpectedResult(self.cal.nlp(
"I'm so excited!! At 8PM on August 5th i'm going to fly to "
"Florida. Then next Friday at 9PM i'm going to Dog n Bone! "
"And in 5 minutes I'm going to eat some food! Talk to you "
"next week.", start), target)
target = datetime.datetime(
self.yr, self.mth, self.dy, 17, 0, 0).timetuple()
def testQuotes(self):
# quotes should not interfere with datetime language recognition
start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple()
target = self.cal.nlp(
"I'm so excited!! At '8PM on August 5th' i'm going to fly to "
"Florida. Then 'next Friday at 9PM' i'm going to Dog n Bone! "
"And in '5 minutes' I'm going to eat some food! Talk to you "
'"next week"', start)
self.assertEqual(target[0][4], "At '8PM on August 5th")
self.assertEqual(target[1][4], "next Friday at 9PM")
self.assertEqual(target[2][4], "in '5 minutes")
self.assertEqual(target[3][4], "next week")
def testPrefixes(self):
# nlp has special handling for on/in/at prefixes
start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple()
target = self.cal.nlp("Buy a balloon on Monday", start)
self.assertEqual(target[0][4], "on Monday")
target = self.cal.nlp("Buy a balloon at noon", start)
self.assertEqual(target[0][4], "at noon")
target = self.cal.nlp("Buy a balloon in a month", start)
self.assertEqual(target[0][4], "in a month")
# Should no longer pull "on" off the end of balloon
target = self.cal.nlp("Buy a balloon Monday", start)
self.assertEqual(target[0][4], "Monday")
def testFalsePositives(self):
# negative testing - no matches should return None
start = datetime.datetime(2013, 8, 1, 21, 25, 0).timetuple()
self.assertExpectedResult(self.cal.nlp(
"Next, I'm so excited!! So many things that are going to "
"happen every week!!", start), None)
self.assertExpectedResult(self.cal.nlp("$300", start), None)
self.assertExpectedResult(self.cal.nlp("300ml", start), None)
self.assertExpectedResult(self.cal.nlp("nice ass", start), None)
|