Skip to main content

An efficient incremental parser for multi-choices grammars.

Project description

Multi-choices Parser

Overview

Multi-choices Parser is an pure-Python efficient incremental parser for multi-choices grammars. These grammars are composed of lists of choices, where each choice is a literal string and can possibly be empty (grammar form below). This parser is optimized for scenarios where the size of the lists of choices is very large, such as representing entities preceded by a determiner.

Here is the type of grammar handled by this parser:

start: list1 list2 ... listn
list1: choice1_1 | choice1_2 | ... | choice1_k1
list2: choice2_1 | choice2_2 | ... | choice2_k2
...
listm: choicem_1 | choicem_2 | ... | choicem_km

Installation

pip install multi-choices-parser

Features

  • Handles large lists of choices efficiently (up to millions of choices).
  • Incremental parsing.

Usage

To use the MultiChoicesParser, follow these steps:

  1. Initialize the parser with a list of choices.
  2. Use the step method to feed characters to the parser.
  3. Check the success flag to determine if the parsed string is correct after feeding the End symbol.
  4. Reset the parser state using the reset method if needed.

Example

from multi_choices_parser.parser import MultiChoicesParser, end_symb

# Define your list of choices
l = [
    ['the', 'an', "a", ""],
    ['orange', 'apple', 'banana']
]

# Initialize the parser
p = MultiChoicesParser(l)

# Parse a string (don't forget to add the End symbol)
for i, c in enumerate(tuple("apple") + (end_symb, )):
    print('Step %s' % i)
    print("Authorized characters:", sorted(p.next()))
    print('Adding character:', c)
    p.step(c)
    print("State: Finished=%s, Success=%s" % (p.finished, p.success))
    print()
Example Output
Step 0
Authorized characters: ['a', 'b', 'o', 't']
Adding character: a
State: Finished=False, Success=False

Step 1
Authorized characters: ['a', 'b', 'n', 'o', 'p']
Adding character: p
State: Finished=False, Success=False

Step 2
Authorized characters: ['p']
Adding character: p
State: Finished=False, Success=False

Step 3
Authorized characters: ['l']
Adding character: l
State: Finished=False, Success=False

Step 4
Authorized characters: ['e']
Adding character: e
State: Finished=False, Success=False

Step 5
Authorized characters: [End]
Adding character: End
State: Finished=True, Success=True

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contact

For any queries or bug reports, please open an issue on the GitHub repository :)

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

multi_choices_parser-0.9.47.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

multi_choices_parser-0.9.47-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file multi_choices_parser-0.9.47.tar.gz.

File metadata

  • Download URL: multi_choices_parser-0.9.47.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for multi_choices_parser-0.9.47.tar.gz
Algorithm Hash digest
SHA256 c564c1768644a4420b7327a93a89a7f07faf5597dfa8189cd0b69af6c115172b
MD5 1f0c943e9d4bb976b12dc2968a68712f
BLAKE2b-256 c61a2ec3af0b2f3e1d44e9dae5ec2083602a2d12399b5b69aa2ab09e0378af6d

See more details on using hashes here.

File details

Details for the file multi_choices_parser-0.9.47-py3-none-any.whl.

File metadata

File hashes

Hashes for multi_choices_parser-0.9.47-py3-none-any.whl
Algorithm Hash digest
SHA256 cddaf44281f632c0eaea71445fc2043e8b3ddc5c7a2b232adddcb0a5b7545c63
MD5 4a0397d6f09f1ccf00f94f77fd83c1f0
BLAKE2b-256 016db630cf1bbe3fdf2edc81dfd0c58bb83f1018cf23ac0a6a42c6689ae32e20

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page