Skip to content
Open
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
26 changes: 25 additions & 1 deletion chatterbot/preprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from chatterbot.conversation import Statement
from unicodedata import normalize
from re import sub as re_sub
from re import sub as re_sub, compile as re_compile
from html import unescape


Expand Down Expand Up @@ -45,3 +45,27 @@ def convert_to_ascii(statement: Statement) -> Statement:

statement.text = str(text)
return statement

# Matches a single letter that is immediately repeated three or more times.
# Digits, punctuation, and whitespace are intentionally excluded so that
# values such as "1000000" or "!!!" are left unchanged.
_REPEATING_CHARACTER_PATTERN = re_compile(r'([^\W\d_])\1{2,}')


def normalize_repeating_characters(statement: Statement) -> Statement:
"""
Reduce runs of three or more repeated letters down to two.

Elongated words are common in conversational text (for example
"I am sooooo happy"). Collapsing the repeated characters maps these
variations to a single, consistent form ("I am soo happy") which helps
the chat bot match input against statements it has been trained on.

Letter pairs that occur naturally (such as the "oo" in "cool") are
preserved, and repeated digits or punctuation are left unchanged.
"""
statement.text = _REPEATING_CHARACTER_PATTERN.sub(
lambda match: match.group(1) * 2, statement.text

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking a look at some related sample data, it appears that this regex would group a statement such as "I am sooooo happy" with "I am soo happy" (x2 characters), which still doesn't quite reach the intended token of "so".

I'm not certain this works as intended in some of the cases the pull request was expecting. Perhaps there is another approach that might work better? (If not, a project-specific preprocessor is always an alternative option to including one in the main codebase.

)

return statement
2 changes: 2 additions & 0 deletions docs/preprocessors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ ChatterBot comes with several built-in preprocessors.

.. autofunction:: chatterbot.preprocessors.convert_to_ascii

.. autofunction:: chatterbot.preprocessors.normalize_repeating_characters


Creating new preprocessors
==========================
Expand Down
30 changes: 30 additions & 0 deletions tests/test_preprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,33 @@ def test_convert_to_ascii(self):
normal_text = 'Kluft skrams infor pa federal electoral groe'

self.assertEqual(cleaned.text, normal_text)

class NormalizeRepeatingCharactersPreprocessorTestCase(ChatBotTestCase):
"""
Make sure that ChatterBot's repeating-character preprocessor works as expected.
"""

def test_elongated_word_is_reduced(self):
statement = Statement(text='I am sooooo happy')
cleaned = preprocessors.normalize_repeating_characters(statement)
self.assertEqual(cleaned.text, 'I am soo happy')

def test_multiple_elongated_words(self):
statement = Statement(text='Yesss that was greaaaat')
cleaned = preprocessors.normalize_repeating_characters(statement)
self.assertEqual(cleaned.text, 'Yess that was greaat')

def test_natural_double_letters_preserved(self):
statement = Statement(text='That book looks really cool')
cleaned = preprocessors.normalize_repeating_characters(statement)
self.assertEqual(cleaned.text, 'That book looks really cool')

def test_repeating_digits_preserved(self):
statement = Statement(text='I have 1000000 dollars')
cleaned = preprocessors.normalize_repeating_characters(statement)
self.assertEqual(cleaned.text, 'I have 1000000 dollars')

def test_repeating_punctuation_preserved(self):
statement = Statement(text='Wow!!!')
cleaned = preprocessors.normalize_repeating_characters(statement)
self.assertEqual(cleaned.text, 'Wow!!!')