Skip to main content

Numbers from String

This Python module provides functions that get the numbers or the numeric string tokens in an input string.

To capture the numerals in a piece of text is a common preprocess for retrieving numerical information from documents. However, due to the various representations of these numerals, it's somewhat tricky to capture them all using simple rules. We packed several regex rules with comprehensive coverage in this library and hope it can be a useful tool for NLP researchers.

Installation

pip install nums_from_string

Usage

  1. Extract numbers from a string
>>> string1 = "U.S. goods and services trade with China totaled an estimated $710.4 billion in 2017. "
>>> nums_from_string.get_nums(string1)
[710.4, 2017]

>>> string2 = "David spent .25 billion dollars buying a building and 600,000.5 dollars getting himself a car."
>>> nums_from_string.get_nums(string2)
[0.25, 600000.5]
  1. Extract numeric strings from a string
>>> string1 = "U.S. goods and services trade with China totaled an estimated $710.4 billion in 2017. "
>>> nums_from_string.get_numeric_string_tokens(string1)
['710.4', '2017']

>>> string2 = "David spent .25 billion dollars buying a building and 600,000.5 dollars getting himself a car."
>>> nums_from_string.get_numeric_string_tokens(string2)
['.25', '600,000.5']

>>> string3 = "Find the product of 4 and -5?"
>>> nums_from_string.get_numeric_string_tokens(string3)
['4', '-5']

>>> string4 = "The flight number is Airbus A330-300"
>>> nums_from_string.get_numeric_string_tokens(string4, no_minus=True)
['330', '300']
  1. Convert strings to numbers
>>> s0 = "255"
>>> nums_from_string.to_num(s0)
255

>>> s1 = "-255,000.0"
>>> nums_from_string.to_num(s1)
-255000.0

>>> s2 = "87/25"
>>> nums_from_string.to_num(s2)
Fraction(87, 25)

>>> s3 = "a1b2"
>>> nums_from_string.to_num(s3)
Traceback (most recent call last):
    ...
ValueError: Invalid numerical string!

Todo

  • Capture the pattern of fractions in a string
  • Capture the patterns like this "-3.5/11"

Reference

License

This project is licensed under the terms of the MIT license.

Metadata

Release files for nums-from-string 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for nums-from-string 0.1.2
File Interpreter ABI Platform Actions
nums_from_string-0.1.2-py3-none-any.whl Python 3 none any Details for nums_from_string-0.1.2-py3-none-any.whl

Release files / nums_from_string-0.1.2-py3-none-any.whl

Download URL nums_from_string-0.1.2-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f36bb38c6be5259290d445912f96760800b6dad2ea9d6b9046e708c8213c1b1
BLAKE2b-256 checksum
How to use checksums
77c64ffad07baaef7b00360c8b16d253c288c8170dd41d4b65a071abf41d4083
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

Security for nums-from-string 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page