Giter Site home page Giter Site logo

daidepp's Introduction

DAIDE++

Feature Tools
Languages Python 3.7 Markdown
Git Conventional Commits
Formatting Black docformatter numpy Imports: isort
Testing Pytest

DAIDE parser using parsimonious. Parsimonious is a python package that uses "a simplified sort of EBNF notation" to define a grammar. The parser currently supports all 130 levels of DAIDE.

  • The original DAIDE specification is here
  • The working markdown document that will included DAIDE enhancements is here
  • The machine-parsable grammar can be found in this .py file

Use

Basic usage

Using the grammar and grammar utils in grammar, you can create a parse tree from a DAIDE press message or reply. The nodes of the parse tree can be visited to return something more useful. The visiting rules for each of the nodes of the parse tree are defined in daide_visitor.py.

Example:

>>> from daidepp import create_daide_grammar, daide_visitor
>>> grammar = create_daide_grammar(level=130)
>>> message = 'PRP (AND (SLO (ENG)) (SLO (GER)) (SLO (RUS)) (AND (SLO (ENG)) (SLO (GER)) (SLO (RUS))))'
>>> parse_tree = grammar.parse(message)
>>> output = daide_visitor.visit(parse_tree) # object composed of dataclass objects in keywords.py
>>> print(output)
PRP ( AND ( SLO ( ENG ) ) ( SLO ( GER ) ) ( SLO ( RUS ) ) ( AND ( SLO ( ENG ) ) ( SLO ( GER ) ) ( SLO ( RUS ) ) ) )

If the DAIDE token is not in the grammar or if the message is malformed, the parser will just thrown an exception. We're currently working on returning a list of unrecognized tokens instead of just erroring out.

DAIDE string construction with keyword classes

In addition, DAIDE strings can be constructed using the classes in base_keywords.py and press_keywords. Each class has type hints that indicate the parameters that should be used.

Example:

>>> from daidepp import AND, PRP, PCE
>>> str(AND(PRP(PCE("AUS")), PRP(PCE("AUS", "ENG"))))
`AND ( PRP ( PCE ( AUS ) ) ) ( PRP ( PCE ( AUS ENG ) ) ) ( PRP ( PCE ( AUS ENG FRA ) ) )`

Each keyword class uses different parameters for instantiation, so it is recommended to carefully follow the type hints or checkout tests/keywords, which provides examples for each class.

Grammar construction with press keywords

Grammar can also be created using a subset of press keywords. The list of press keywords can be found in constants.py under PressKeywords.

Example:

>>> from daidepp.grammar.grammar_utils import create_grammar_from_press_keywords
>>> grammar = create_grammar_from_press_keywords(["PRP", "XDO", "ALY_VSS"]
>>> grammar.parse("PRP (ALY (ITA TUR) VSS (ENG RUS))")
>>> grammar.parse("PRP(XDO((ENG FLT EDI) SUP (ENG AMY LVP) MTO CLY))")
>>> grammar.parse("PRP(PCE (AUS ENG))") # this would fail

Note: Because of the way Parsimonious simplifies grammars, there may be some edge cases where the given list of press keywords does not result in a grammar object with the correct order of keywords (i.e. it may fail to parse even when the message is valid). If this happens, try providing the function with a few more keywords.

Pull Requests

Three files should be updated whenever making a PR:

daidepp's People

Contributors

mjspeck avatar byung-o avatar delaschwein avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.