Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The parser just recognizes case sensitive strings #1291

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions chatterbot/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
lambda m, base_date: date_from_relative_week_year(
base_date,
m.group('time').lower(),
m.group('dmy'),
m.group('dmy').lower(),
m.group('number')
) + timedelta(**convert_time_to_hour_minute(
m.group('hour'),
Expand Down Expand Up @@ -488,7 +488,7 @@ def convert_time_to_hour_minute(hour, minute, convention):
hour = int(hour)
minute = int(minute)

if convention == 'pm':
if convention.lower() == 'pm':
hour += 12

return {'hours': hour, 'minutes': minute}
Expand Down
93 changes: 93 additions & 0 deletions tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ def test_captured_pattern_is_next_three_weeks(self):
)
self.assertEqual(len(parser), 1)

def test_captured_pattern_is_next_x_weeks_case_insensitive(self):
input_text = 'next 2 Weeks'
parser = parsing.datetime_parsing(input_text)
self.assertIn(input_text, parser[0])
self.assertEqual(
parser[0][1].strftime('%d-%m-%Y'),
(datetime.today() + timedelta(weeks=2)).strftime('%d-%m-%Y')
)
self.assertEqual(len(parser), 1)

def test_captured_pattern_is_next_eight_days(self):
input_text = 'Next 8 days'
parser = parsing.datetime_parsing(input_text)
Expand All @@ -199,6 +209,16 @@ def test_captured_pattern_is_next_eight_days(self):
)
self.assertEqual(len(parser), 1)

def test_captured_pattern_is_next_x_days_case_insensitive(self):
input_text = 'next 14 Days'
parser = parsing.datetime_parsing(input_text)
self.assertIn(input_text, parser[0])
self.assertEqual(
parser[0][1].strftime('%d-%m-%Y'),
(datetime.today() + timedelta(days=14)).strftime('%d-%m-%Y')
)
self.assertEqual(len(parser), 1)

def test_captured_pattern_is_next_ten_years(self):
input_text = 'Next 10 years'
parser = parsing.datetime_parsing(input_text)
Expand All @@ -209,6 +229,16 @@ def test_captured_pattern_is_next_ten_years(self):
)
self.assertEqual(len(parser), 1)

def test_captured_pattern_is_next_x_years_case_insensitive(self):
input_text = 'next 43 Years'
parser = parsing.datetime_parsing(input_text)
self.assertIn('next 43 Year', parser[0])
self.assertEqual(
parser[0][1].strftime('%d-%m-%Y'),
(datetime.today() + timedelta(43 * 365)).strftime('%d-%m-%Y')
)
self.assertEqual(len(parser), 1)

def test_captured_pattern_is_next_eleven_months(self):
import calendar
input_text = 'Next 11 months'
Expand All @@ -224,6 +254,21 @@ def test_captured_pattern_is_next_eleven_months(self):
)
self.assertEqual(len(parser), 1)

def test_captured_pattern_is_next_x_months_case_insensitive(self):
import calendar
input_text = 'next 55 Months'
parser = parsing.datetime_parsing(input_text)
relative_date = datetime.today()
month = relative_date.month - 1 + 55
year = relative_date.year + month // 12
month = month % 12 + 1
day = min(relative_date.day, calendar.monthrange(year, month)[1])
self.assertIn('next 55 Month', parser[0])
self.assertEqual(
parser[0][1].strftime('%d-%m-%Y'), datetime(year, month, day).strftime('%d-%m-%Y')
)
self.assertEqual(len(parser), 1)

def test_captured_pattern_is_on_day(self):
input_text = 'My birthday is on January 2nd.'
parser = parsing.datetime_parsing(input_text)
Expand Down Expand Up @@ -254,6 +299,30 @@ def test_captured_pattern_has_am(self):
self.assertEqual(parser[0][1].strftime('%H'), '05')
self.assertEqual(len(parser), 1)

def test_captured_pattern_has_am_case_insensitive_1(self):
input_text = '7 AM'
parser = parsing.datetime_parsing(input_text)
self.assertIn('7 AM', parser[0])
self.assertEqual(parser[0][1].strftime('%d'), datetime.today().strftime('%d'))
self.assertEqual(parser[0][1].strftime('%H'), '07')
self.assertEqual(len(parser), 1)

def test_captured_pattern_has_am_case_insensitive_2(self):
input_text = '1 Am'
parser = parsing.datetime_parsing(input_text)
self.assertIn('1 Am', parser[0])
self.assertEqual(parser[0][1].strftime('%d'), datetime.today().strftime('%d'))
self.assertEqual(parser[0][1].strftime('%H'), '01')
self.assertEqual(len(parser), 1)

def test_captured_pattern_has_am_case_insensitive_3(self):
input_text = '9aM'
parser = parsing.datetime_parsing(input_text)
self.assertIn('9aM', parser[0])
self.assertEqual(parser[0][1].strftime('%d'), datetime.today().strftime('%d'))
self.assertEqual(parser[0][1].strftime('%H'), '09')
self.assertEqual(len(parser), 1)

def test_captured_pattern_has_pm(self):
input_text = 'Your dental appointment at 4 pm in the evening.'
parser = parsing.datetime_parsing(input_text)
Expand All @@ -262,6 +331,30 @@ def test_captured_pattern_has_pm(self):
self.assertEqual(parser[0][1].strftime('%H'), '16')
self.assertEqual(len(parser), 1)

def test_captured_pattern_has_pm_case_insensitive_1(self):
input_text = '8 PM'
parser = parsing.datetime_parsing(input_text)
self.assertIn('8 PM', parser[0])
self.assertEqual(parser[0][1].strftime('%d'), datetime.today().strftime('%d'))
self.assertEqual(parser[0][1].strftime('%H'), '20')
self.assertEqual(len(parser), 1)

def test_captured_pattern_has_pm_case_insensitive_2(self):
input_text = '11 pM'
parser = parsing.datetime_parsing(input_text)
self.assertIn('11 pM', parser[0])
self.assertEqual(parser[0][1].strftime('%d'), datetime.today().strftime('%d'))
self.assertEqual(parser[0][1].strftime('%H'), '23')
self.assertEqual(len(parser), 1)

def test_captured_pattern_has_pm_case_insensitive_3(self):
input_text = '3Pm'
parser = parsing.datetime_parsing(input_text)
self.assertIn('3Pm', parser[0])
self.assertEqual(parser[0][1].strftime('%d'), datetime.today().strftime('%d'))
self.assertEqual(parser[0][1].strftime('%H'), '15')
self.assertEqual(len(parser), 1)


class DateTimeParsingTestCases(TestCase):
"""
Expand Down