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

Implement function to tie percent sign to number #201

Closed
wants to merge 3 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
23 changes: 23 additions & 0 deletions lingua_franca/lang/parse_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,28 @@ def replace_words(self, utterance):
utterance = " ".join(words)
return utterance

@staticmethod
def _is_number(string):
try:
number = float(string)
except:
return False
return True

def tying_percentage(self, utterance):
words = self.tokenize(utterance)
output = []
for idx, w in enumerate(words):
if w.startswith('%') and idx > 0:
if self._is_number(output[-1]) is False:
output.append(w)
else:
output[-1] += w
else:
output.append(w)
utterance = " ".join(output)
return utterance

def normalize(self, utterance="", remove_articles=None):
# mutations
if self.should_lowercase:
Expand All @@ -186,6 +208,7 @@ def normalize(self, utterance="", remove_articles=None):
utterance = self.remove_stopwords(utterance)
# remove extra spaces
utterance = " ".join([w for w in utterance.split(" ") if w])
utterance = self.tying_percentage(utterance)
return utterance


Expand Down
23 changes: 22 additions & 1 deletion test/test_parse_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import unittest

from lingua_franca.lang.parse_common import tokenize, Token
from lingua_franca.lang.parse_common import Normalizer, tokenize, Token


class TestParseCommon(unittest.TestCase):
Expand All @@ -33,3 +33,24 @@ def test_tokenize(self):

self.assertEqual(tokenize('hashtag #1world'),
[Token('hashtag', 0), Token('#1world', 1)])

def test_normalize(self):

normalizer = Normalizer()
self.assertEqual(normalizer.normalize('Set volume to 50%.'),
'Set volume to 50%.')

self.assertEqual(normalizer.normalize('I am 100% sure.'),
'I am 100% sure.')

self.assertEqual(normalizer.normalize('Is current volume 42%?'),
'Is current volume 42%?')

self.assertEqual(normalizer.normalize('42% is current volume!'),
'42% is current volume!')

self.assertEqual(normalizer.normalize('It takes 12.34% of revenue.'),
'It takes 12.34% of revenue.')

self.assertEqual(normalizer.normalize('Return on asset is 56.78%.'),
'Return on asset is 56.78%.')