word2number

repository·master·Indexed 41 days ago

https://github.com/akshaynagpal/w2n

A Python module that converts English number words into numeric digit representations. Using the w2n.word_to_num API, it supports positive numbers up to 999,999,999,999, including decimals, hyphenated numbers, and numeric strings.

Tokens
647
Snippets
3
Records
3
Agent score
2%

What's inside word2number

  1. Install word2number via pip

    master

    Install the word2number module using the Python Package Index. It is recommended to ensure your pip is updated to the latest version before installation. You should also install all dependencies listed in requirements.txt.

    pip install word2number
    pip install -r requirements.txt
  2. Use w2n.word_to_num API

    master

    The word_to_num function is the primary entry point for the word2number library, located in the w2n module.

    from word2number import w2n
    
    # Signature: w2n.word_to_num(number_word_string)
    # Returns: int, float, or None
    result = w2n.word_to_num("one hundred")
  3. Convert number words to numeric digits with word_to_num

    master

    Use the w2n.word_to_num method to convert natural language number words (e.g., "twenty one") into numeric digits (e.g., 21).

    Supported Range: Positive numbers up to 999,999,999,999 (billions).

    Capabilities:

    • Converts full number words to integers.
    • Supports decimal points (e.g., "two point three" $\rightarrow$ 2.3).
    • Supports leading decimal points (e.g., "point one" $\rightarrow$ 0.1).
    • Handles hyphenated numbers (e.g., "one hundred thirty-five" $\rightarrow$ 135).
    • Accepts numeric strings directly (e.g., "112" $\rightarrow$ 112).

    Error Handling: If the input is invalid, the method prints an error message to the console and returns None.

    • Redundant numbers: e.g., "million million" triggers Error: Redundant number! Please enter a valid number word...
    • Invalid words: e.g., "blah" triggers Error: No valid number words found! Please enter a valid number word...
    from word2number import w2n
    
    # Basic conversion
    print(w2n.word_to_num("two million three thousand nine hundred and eighty four")) # 2003984
    
    # Decimal conversion
    print(w2n.word_to_num('two point three')) # 2.3
    print(w2n.word_to_num('point one'))      # 0.1
    
    # Numeric string conversion
    print(w2n.word_to_num('112'))             # 112
    
    # Hyphenated numbers
    print(w2n.word_to_num('one hundred thirty-five')) # 135