Skip to content

Commit

Permalink
raise used for error instead of print, closes #12. Also fixed #13
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaynagpal committed Jun 2, 2017
1 parent 99e1414 commit 0853221
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 46 deletions.
27 changes: 16 additions & 11 deletions unit_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,23 @@ def test_positives(self):
self.assertEqual(w2n.word_to_num('point'), 0)
self.assertEqual(w2n.word_to_num('point nineteen'), 0)
self.assertEqual(w2n.word_to_num('one hundred thirty-five'), 135)
self.assertEqual(w2n.word_to_num('hundred'), 100)
self.assertEqual(w2n.word_to_num('thousand'), 1000)
self.assertEqual(w2n.word_to_num('million'), 1000000)
self.assertEqual(w2n.word_to_num('billion'), 1000000000)
self.assertEqual(w2n.word_to_num('nine point nine nine nine'), 9.999)

def test_negatives(self):
self.assertEqual(w2n.word_to_num('112-'), None)
self.assertEqual(w2n.word_to_num('-'), None)
self.assertEqual(w2n.word_to_num('on'), None)
self.assertEqual(w2n.word_to_num('million million'), None)
self.assertEqual(w2n.word_to_num('three million million'), None)
self.assertEqual(w2n.word_to_num('million four million'), None)
self.assertEqual(w2n.word_to_num('thousand million'), None)
self.assertEqual(w2n.word_to_num('one billion point two million twenty three thousand and forty nine point two three six nine'), None)
self.assertEqual(w2n.word_to_num('seventh point nineteen'), 0)
self.assertEqual(w2n.word_to_num(112), None)

with self.assertRaises(ValueError):
w2n.word_to_num('112-')
w2n.word_to_num('-')
w2n.word_to_num('on')
w2n.word_to_num('million million')
w2n.word_to_num('three million million')
w2n.word_to_num('million four million')
w2n.word_to_num('thousand million')
w2n.word_to_num('one billion point two million twenty three thousand and forty nine point two three six nine')
w2n.word_to_num('seventh point nineteen')
w2n.word_to_num(112)
if __name__ == '__main__':
unittest.main()
73 changes: 38 additions & 35 deletions word2number/w2n.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ def get_decimal_sum(decimal_digit_words):

def word_to_num(number_sentence):
if type(number_sentence) is not str:
print("Error: Type of input is not string! Please enter a valid number word (eg. \'two million twenty three thousand and forty nine\')")
return None
raise ValueError("Type of input is not string! Please enter a valid number word (eg. \'two million twenty three thousand and forty nine\')")

number_sentence = number_sentence.replace('-', ' ')
number_sentence = number_sentence.lower() # converting input to lowercase

if(number_sentence.isdigit()): # return the number if user enters a number string
return int(number_sentence)

split_words = number_sentence.strip().split() # split sentence into words
split_words = number_sentence.strip().split() # strip extra spaces and split sentence into words

clean_numbers = []
clean_decimal_numbers = []
Expand All @@ -151,12 +151,11 @@ def word_to_num(number_sentence):

# Error message if the user enters invalid input!
if len(clean_numbers) == 0:
print("Error: No valid number words found! Please enter a valid number word (eg. two million twenty three thousand and forty nine)")
return None
raise ValueError("No valid number words found! Please enter a valid number word (eg. two million twenty three thousand and forty nine)")

# Error if user enters million,billion, thousand or decimal point twice
if clean_numbers.count('thousand') > 1 or clean_numbers.count('million') > 1 or clean_numbers.count('billion') > 1 or clean_numbers.count('point')> 1:
print("Error: Redundant number! Please enter a valid number word (eg. two million twenty three thousand and forty nine)")
return None
raise ValueError("Redundant number word! Please enter a valid number word (eg. two million twenty three thousand and forty nine)")

# separate decimal part of number (if exists)
if clean_numbers.count('point') == 1:
Expand All @@ -168,43 +167,47 @@ def word_to_num(number_sentence):
thousand_index = clean_numbers.index('thousand') if 'thousand' in clean_numbers else -1

if (thousand_index > -1 and (thousand_index < million_index or thousand_index < billion_index)) or (million_index>-1 and million_index < billion_index):
print("Error: Malformed number! Please enter a valid number word (eg. two million twenty three thousand and forty nine)")
return None
raise ValueError("Malformed number! Please enter a valid number word (eg. two million twenty three thousand and forty nine)")

total_sum = 0 # storing the number to be returned

if len(clean_numbers) > 0:
if billion_index > -1:
billion_multiplier = number_formation(clean_numbers[0:billion_index])
total_sum += billion_multiplier * 1000000000
# hack for now, better way TODO
if len(clean_numbers) == 1:
total_sum += american_number_system[clean_numbers[0]]

if million_index > -1:
else:
if billion_index > -1:
million_multiplier = number_formation(clean_numbers[billion_index+1:million_index])
else:
million_multiplier = number_formation(clean_numbers[0:million_index])
total_sum += million_multiplier * 1000000
billion_multiplier = number_formation(clean_numbers[0:billion_index])
total_sum += billion_multiplier * 1000000000

if thousand_index > -1:
if million_index > -1:
thousand_multiplier = number_formation(clean_numbers[million_index+1:thousand_index])
elif billion_index > -1 and million_index == -1:
thousand_multiplier = number_formation(clean_numbers[billion_index+1:thousand_index])
if billion_index > -1:
million_multiplier = number_formation(clean_numbers[billion_index+1:million_index])
else:
million_multiplier = number_formation(clean_numbers[0:million_index])
total_sum += million_multiplier * 1000000

if thousand_index > -1:
if million_index > -1:
thousand_multiplier = number_formation(clean_numbers[million_index+1:thousand_index])
elif billion_index > -1 and million_index == -1:
thousand_multiplier = number_formation(clean_numbers[billion_index+1:thousand_index])
else:
thousand_multiplier = number_formation(clean_numbers[0:thousand_index])
total_sum += thousand_multiplier * 1000

if thousand_index > -1 and thousand_index != len(clean_numbers)-1:
hundreds = number_formation(clean_numbers[thousand_index+1:])
elif million_index > -1 and million_index != len(clean_numbers)-1:
hundreds = number_formation(clean_numbers[million_index+1:])
elif billion_index > -1 and billion_index != len(clean_numbers)-1:
hundreds = number_formation(clean_numbers[billion_index+1:])
elif thousand_index == -1 and million_index == -1 and billion_index == -1:
hundreds = number_formation(clean_numbers)
else:
thousand_multiplier = number_formation(clean_numbers[0:thousand_index])
total_sum += thousand_multiplier * 1000

if thousand_index > -1 and thousand_index != len(clean_numbers)-1:
hundreds = number_formation(clean_numbers[thousand_index+1:])
elif million_index > -1 and million_index != len(clean_numbers)-1:
hundreds = number_formation(clean_numbers[million_index+1:])
elif billion_index > -1 and billion_index != len(clean_numbers)-1:
hundreds = number_formation(clean_numbers[billion_index+1:])
elif thousand_index == -1 and million_index == -1 and billion_index == -1:
hundreds = number_formation(clean_numbers)
else:
hundreds = 0
total_sum += hundreds
hundreds = 0
total_sum += hundreds

# adding decimal part to total_sum (if exists)
if len(clean_decimal_numbers) > 0:
Expand Down

0 comments on commit 0853221

Please sign in to comment.