Skip to content
This repository has been archived by the owner on Mar 12, 2022. It is now read-only.

Commit

Permalink
fixed w2n issue akshaynagpal#54 - floating point conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Sͬeͥbͭaͭsͤtͬian committed Jan 23, 2021
1 parent c465526 commit e44bfe9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .settings/org.eclipse.ltk.core.refactoring.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The MIT License (MIT)

Copyright (c) 2016 Akshay Nagpal
Copyright (c) 2020 Sebastian Ritter
Copyright (c) 2020-2021 Sebastian Ritter

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion LICENSES/MIT.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The MIT License (MIT)

Copyright (c) 2016 Akshay Nagpal
Copyright (c) 2020 Sebastian Ritter
Copyright (c) 2020-2021 Sebastian Ritter

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ Below is the installation, usage and other details of this module.

## Installation

Please ensure that you have **updated pip** to the latest version before installing word2number.
Please ensure that you have **updated pip** to the latest version before installing word2number-i18n.

You can install the module using Python Package Index using the below command.

pip install word2number
pip install word2number-i18n
pip3 install word2number-i18n

Make sure you install all requirements given in requirements.txt
```
pip install -r requirements.txt
pip install -r requirements.txt
pip3 install -r requirements.txt
```
## Usage

Expand Down Expand Up @@ -77,6 +79,8 @@ Do follow steps
You want to tranfer NLP CARD to numeric value for Lower Sorbian. German (de) isn't it.
You do not found an ISO-639-1 code, you do not found an ISO-639-1 file for ```dsb``` extension.
You create a new file ```number_system_dsb.txt``` with utf-8 encoding

```
null 0
jaden 1
dwa 2
Expand All @@ -90,6 +94,7 @@ You create a new file ```number_system_dsb.txt``` with utf-8 encoding
źaseś 10
[...]
point ,
```

## Bugs/Errors
- german language need more specific algorithm
Expand All @@ -111,7 +116,7 @@ The MIT License (MIT)

Copyright (c) 2016 Akshay Nagpal

Copyright (c) 2020 Sebastian Ritter
Copyright (c) 2020-2021 Sebastian Ritter

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def read_file(fname):

setuptools.setup(
name = "word2number-i18n",
version = "0.1.2",
version = "0.1.3",
description = "Convert i18n number words eg. three hundred and forty two to numbers (342).",
long_description=read_file("README.md"),
long_description_content_type='text/markdown',
Expand Down
10 changes: 10 additions & 0 deletions unit_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ def test_positives_en(self):
self.assertEqual(w2n.word_to_num('nine point nine nine nine'), 9.999)
self.assertEqual(w2n.word_to_num('seventh point nineteen'), 0)
self.assertEqual(w2n.word_to_num('seven million, eight hundred, and sixty three thousand, two hundred, and fifty four'), 7863254)

# test cases https://github.com/akshaynagpal/w2n/issues/54
self.assertEqual(w2n.word_to_num('three point nine seven'), 3.97)
self.assertEqual(w2n.word_to_num('two point seven eight'),2.78)
self.assertEqual(w2n.word_to_num('one point eight six'), 1.86)
self.assertEqual(w2n.word_to_num('two point seven two'), 2.72)
self.assertEqual(w2n.word_to_num('one point eight four'), 1.84)
self.assertEqual(w2n.word_to_num('two point two eight'), 2.28)
self.assertEqual(w2n.word_to_num('two point four seven'), 2.47)
self.assertEqual(w2n.word_to_num('one point five nine'), 1.59)

def test_negatives_en(self):
self.assertRaises(ValueError, w2n.word_to_num, '112-')
Expand Down
16 changes: 10 additions & 6 deletions word2numberi18n/w2n.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import codecs
import re

# the fallback number system is using of no files can be loaded
number_system_fallback = {
'zero': 0,
'one': 1,
Expand Down Expand Up @@ -43,6 +44,7 @@
'point': '.'
}

#
lang = locale.getlocale()[0]
if "w2n.lang" in os.environ :
lang = os.environ ["w2n.lang"]
Expand Down Expand Up @@ -101,20 +103,21 @@ def number_formation(number_words):

"""
function to convert post decimal digit words to numerial digits
it returns a string to prevert from floating point conversation problem
input: list of strings
output: double
output: string
"""


def get_decimal_sum(decimal_digit_words):
def get_decimal_string(decimal_digit_words):
decimal_number_str = []
for dec_word in decimal_digit_words:
if(dec_word not in decimal_words):
return 0
else:
decimal_number_str.append(number_system[dec_word])
final_decimal_string = '0.' + ''.join(map(str,decimal_number_str))
return float(final_decimal_string)
final_decimal_string = ''.join(map(str,decimal_number_str))
return final_decimal_string


"""
Expand Down Expand Up @@ -206,7 +209,8 @@ def word_to_num(number_sentence):

# adding decimal part to total_sum (if exists)
if len(clean_decimal_numbers) > 0:
decimal_sum = get_decimal_sum(clean_decimal_numbers)
total_sum += decimal_sum
decimal_sum = get_decimal_string(clean_decimal_numbers)
decimal_sum = str(total_sum)+"."+str(decimal_sum)
total_sum = float(decimal_sum)

return total_sum

0 comments on commit e44bfe9

Please sign in to comment.