- How to Convert Numeric Words into Numbers using Python
- Examples:#
- Additional Notes:#
- Test cases to convert words into numbers#
- The solution in Python to convert words into numbers#
- Top Related Articles
- word2number 1.1
- Навигация
- Ссылки проекта
- Статистика
- Метаданные
- Сопровождающие
- Классификаторы
- Описание проекта
- Word to Number
- Installation
- Usage
- Bugs/Errors
- Contributors
- Welcome to word2number’s documentation!¶
- Installation¶
- Usage¶
- Bugs/Errors¶
- Contributors¶
- ru-word2number 1.1
- Навигация
- Ссылки проекта
- Статистика
- Метаданные
- Сопровождающие
- Описание проекта
How to Convert Numeric Words into Numbers using Python
Using Python, we want to convert words into numbers. In this challenge, we will explore how to convert a string into an integer.
The strings simply represent the numbers in words. Let’s convert these words into numbers.
Examples:#
- “one” => 1
- “twenty” => 20
- “two hundred forty-six” => 246
- “seven hundred eighty-three thousand nine hundred and nineteen” => 783919
Additional Notes:#
- The minimum number is “zero” (inclusively)
- The maximum number, which must be supported is 1 million (inclusively)
- The “and” in e.g. “one hundred and twenty-four” is optional, in some cases it’s present and in others, it’s not
- All tested numbers are valid, you don’t need to validate them
Test cases to convert words into numbers#
Test.assert_equals(parse_int('one'), 1) Test.assert_equals(parse_int('twenty'), 20) Test.assert_equals(parse_int('two hundred forty-six'), 246)
The solution in Python to convert words into numbers#
def parse_int(textnum, numwords=<>): # create our default word-lists if not numwords: # singles units = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", ] # tens tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] # larger scales scales = ["hundred", "thousand", "million", "billion", "trillion"] # divisors numwords["and"] = (1, 0) # perform our loops and start the swap for idx, word in enumerate(units): numwords[word] = (1, idx) for idx, word in enumerate(tens): numwords[word] = (1, idx * 10) for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0) # primary loop current = result = 0 # loop while splitting to break into individual words for word in textnum.replace("-"," ").split(): # if problem then fail-safe if word not in numwords: raise Exception("Illegal word: " + word) # use the index by the multiplier scale, increment = numwords[word] current = current * scale + increment # if larger than 100 then push for a round 2 if scale > 100: result += current current = 0 # return the result plus the current return result + current
Top Related Articles
- Solve The Triangle of Odd Numbers using Python
- “Who likes it” code Challenge in Python
- Python 4 New Features Planned
- Custom RGB To Hex Conversion with Python
- How to write a Chain Adding Function in Python
- Get the next biggest number with the same digits using Python
- Solving Tribonacci Sequence with Python
- The Casino Chips Problem Solved with Python
- Check if Isogram using Python
- Find the Longest Common Prefix using Python
word2number 1.1
Convert number words eg. three hundred and forty two to numbers (342).
Навигация
Ссылки проекта
Статистика
Метаданные
Лицензия: MIT License (The MIT License (MIT) )
Метки numbers, convert, words
Сопровождающие
Классификаторы
- Development Status
- 5 — Production/Stable
- Customer Service
- Developers
- Education
- Healthcare Industry
- Information Technology
- OSI Approved :: MIT License
- English
- OS Independent
- Python
- Python :: 2
- Python :: 2.7
- Python :: 3.6
- Documentation :: Sphinx
- Education
- Education :: Computer Aided Instruction (CAI)
- Education :: Testing
- Games/Entertainment :: Board Games
- Games/Entertainment :: Turn Based Strategy
- Scientific/Engineering :: Human Machine Interfaces
- Software Development :: Libraries :: Python Modules
- Software Development :: Version Control :: Git
- Utilities
Описание проекта
Word to Number
This is a Python module to convert number words (eg. twenty one) to numeric digits (21). It works for positive numbers upto the range of 999,999,999,999 (i.e. billions).
Installation
Please ensure that you have updated pip to the latest version before installing word2number.
You can install the module using Python Package Index using the below command.
Make sure you install all requirements given in requirements.txt
Usage
First you have to import the module using the below code. .. code-block:: python
Then you can use the word_to_num method to convert a number-word to numeric digits, as shown below.
Bugs/Errors
Please ensure that you have updated pip to the latest version before installing word2number.
If you find any bugs/errors in the usage of above code, please raise an issue through Github. If you don’t know how to use Github or raise an issue through it, I suggest that you should learn it. Else, send an email to akshay2626 @ gmail . com with a clear example that can reproduce the issue.
Contributors
Welcome to word2number’s documentation!¶
This is a Python module to convert number words (eg. twenty one) to numeric digits (21). It works for positive numbers upto the range of 999,999,999,999 (i.e. billions).
Installation¶
Please ensure that you have updated pip to the latest version before installing word2number.
You can install the module using Python Package Index using the below command.
Make sure you install all requirements given in requirements.txt
pip install -r requirements.txt
Usage¶
First you have to import the module using the below code.
from word2number import w2n
Then you can use the word_to_num method to convert a number-word to numeric digits, as shown below.
>>> print w2n.word_to_num("two million three thousand nine hundred and eighty four") 2003984 >>> print(w2n.word_to_num('two point three')) 2.3 >>> print(w2n.word_to_num('112')) 112 >>> print(w2n.word_to_num('point one')) 0.1 >>> print(w2n.word_to_num('one hundred thirty-five')) 135 >>> print(w2n.word_to_num('million million')) Error: Redundant number! Please enter a valid number word (eg. two million twenty three thousand and forty nine) None >>> print(w2n.word_to_num('blah')) Error: No valid number words found! Please enter a valid number word (eg. two million twenty three thousand and forty nine) None
Bugs/Errors¶
Please ensure that you have updated pip to the latest version before installing word2number.
If you find any bugs/errors in the usage of above code, please raise an issue through Github. If you don’t know how to use Github or raise an issue through it, I suggest that you should learn it. Else, send an email to akshay2626 @ gmail . com with a clear example that can reproduce the issue.
Contributors¶
ru-word2number 1.1
Convert russian number words eg. триста сорок два to numbers (342).
Навигация
Ссылки проекта
Статистика
Метаданные
Лицензия: The MIT License (MIT)
Сопровождающие
Описание проекта
Copyright (c) 2016 Akshay Nagpal (https://github.com/akshaynagpal)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the «Software»), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED «AS IS», WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.Download-URL: https://github.com/Oknolaz/Russian_w2n/tarball/1.1
Description: Convert russian number words to numbers.
Keywords: numbers,convert,words
Platform: UNKNOWN
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python