A simple API to scan and tokenize text for the purpose of structured language processing.
Project description
nr.parsing.core
The nr.parsing.core
package provides a simple API to scan and tokenize text for the purpose of
structured langauge processing.
Example
from nr.parsing.core import RuleSet, Tokenizer, rules
ruleset = RuleSet()
ruleset.rule('number', rules.regex_extract(r'\-?(0|[1-9]\d*)', 0))
ruleset.rule('operator', rules.regex_extract(r'[\-\+]', 0))
ruleset.rule('whitespace', rules.regex(r'\s+'), skip=True)
def calculate(expr: str) -> int:
tokenizer = Tokenizer(ruleset, expr)
result = 0
sign: t.Optional[int] = 1
while tokenizer:
if tokenizer.current.type != 'number':
raise ValueError(f'unexpected token {tokenizer.current}')
assert sign is not None
result += sign * int(tokenizer.current.value)
tokenizer.next()
if tokenizer.current.type == 'operator':
sign = -1 if tokenizer.current.value == '-' else 1
tokenizer.next()
else:
sign = None
if sign is not None:
raise ValueError(f'unexpected trailing operator')
return result
assert calculate('3 + 5 - 1') == 7
Copyright © 2020 Niklas Rosenstein
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
nr.parsing.core-2.0.3.tar.gz
(11.7 kB
view details)
Built Distribution
File details
Details for the file nr.parsing.core-2.0.3.tar.gz
.
File metadata
- Download URL: nr.parsing.core-2.0.3.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e351949682abc3a02008f670f64f49bd8acd4dc39289fd98e5985cf186a6463 |
|
MD5 | 512e811340740f4fe3820b9af8199506 |
|
BLAKE2b-256 | e45eef7b19dbcd23c7a84df2b4df2357873d5e0fa2d88d75f29a6bb6e135390c |
File details
Details for the file nr.parsing.core-2.0.3-py3-none-any.whl
.
File metadata
- Download URL: nr.parsing.core-2.0.3-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3485e3b571a780e46659741c5aa73fb15042b3f9bc71681b66cd8e6ff49979ac |
|
MD5 | 73e94fb578a262e2e3fc0afb466e7747 |
|
BLAKE2b-256 | 06af26c828ea61ec23ab997adba067f06b3cd1d13fe848a9860fb3bee328cde5 |