Skip to main content

Core library for oewn

Project description

Open English Wordnet core Python toolkit

This project's purpose is to provide basic load/save utilities in Python for Open English Wordnet models.

The starting point is a number of Python scripts written by JohnMcCrae that used to come with OEWN source. These have been reworked.

Dataflows

  • YAML -> model
  • model -> YAML
  • pickle -> model
  • model -> pickle
  • XML -> model
  • model -> XML

Bare-Bones, No-Frills

Unused code has been trimmed.

In the model, IDs are not transformed — there is no mangling to make them valid XML IDs:

  • sense IDs are the sensekeys,
  • synset IDs are the synsetids,
  • entries have no ID — they use (lemma, pos, discriminant) for resolution where discriminant is the possible indexing appended to the part-of-speech, for example: (bass,a,None) (bass,n,1) (bass,n,2)

One-field classes have been replaced with this field.

No processing nor editing of model is performed: it's up to other tools to do it.

No validation of model is performed within core: it's up to other tools to do it. There is however and independent validation module (see below) that can be run.

If anything goes wrong, the language and libraries will raise their own exceptions. However, some exceptions are raised when a requested operation can't carry on.

It is not considered inherent to the model to be exported to XML. So the model is XML-agnostic. XML legacy has been ditched except when it comes to XML exporting / importing in the dedicated reader and writer.

Modular

Design is modular: modules do not depend on each other except on model. So modules are encapsulated. Chains however use a supplier and a consumer.

Typed

Variables are typed to make the code readable and document it.

No-deps

No deps but YAML (pip install PyYAML)

Late resolution

Internal cross-dependencies are resolved at a later stage. If resolution is not necessary, this stage may be ignored. This involves

  • the resolution of synsetids in senses to synsets
  • the resolution of members in synsets to entries
  • the resolution of targets in relations to senses or synsets

The resolved entities are stored as resolved_* class members in the object.

Late extension

Optional extension of relation sets with the addition of inverse relations (if inversable) is possible at a later stage, if needed.

Packages

Code comes in 3 packages:

  • oewn_core which contains model and YAML I/O and knows nothing of oewn_xml
  • oewn_xml optional package which contains XML I/O and depends on oewn_core
  • oewn_validate optional package which contains model validation and depends on oewn_core, but not on oewn_wml since it does not validate XML but only the model's semantics.

Modules

Model

Suppliers: YAML/XML/pickle

  • fromyaml : Supply model from YAML
  • fromxml : Supply model from (one-file) XML

Consumers: YAML/XML/pickle

  • toyaml : Consume model to YAML
  • toxml : Consume model to (one-file) XML

Supplier-consumer chains: YAML2YAML, YAML2XML, XML2YAML

  • yaml_to_yaml : Chain from YAML supplier to YAML consumer (side effect is normalization)
  • yaml_to_xml : Chain from YAML supplier to XML consumer (conversion from XML)
  • xml_to_yaml : Chain from XML supplier to YAML consumer (conversion to YAML)

XML extensions ##****

While still conforming to WN-LMF-1.1.dtd,

  • wikidata in synsets is exported in the dc:subject attribute
  • sent in synsets is exported as <Example>
  • dc:date in <Lexicon> receives the date of XML generation
  • dc:identifier in <Lexicon> receives a UID (and is thus unique)

Sample code

How to load a model:

def main() -> None:
    def get_model() -> WordnetModel:
        if args.pickle:
            from oewn_core.deserialize import load
            return load(args.in_dir, resolve=True)
        else:
            from oewn_core.wordnet_fromyaml import load
            return load(args.in_dir, resolve=True)

    arg_parser = argparse.ArgumentParser(description="browse")
    arg_parser.add_argument('--serialized', action='store_true', default='True', help='model to use')
    arg_parser.add_argument('in_dir', type=str, help='from-dir for yaml/pickle')
    arg_parser.add_argument('pickle', type=str, nargs='?', default='oewn.pickle', help='from-pickle')
    args = arg_parser.parse_args()

    wn = get_model()
    browse(wn)

Note that loading uses the resolve=True parameter so that reference resolution is done once and for all, at an early stage, and resolved*_ fields can be used, saving you the trouble of having to look up for ID reference.

How to browse:

def browse(wn: WordnetModel) -> None:
    entity_resolver = wn.entry_resolver
    e = entity_resolver[('force', 'n', None)]
    print(f'{e}')
    for s in e.senses:
        ss = s.resolved_synset
        print(f'\t{s} -> {ss.members} {ss.definitions}')
        for ssr in ss.relations:
            print(f'\t\t{ssr} {ssr.resolved_target.members} {ssr.resolved_target.definitions}')
        for sr in s.relations:
            print(f'\t\t{sr} {sr.resolved_target}')

Note that entry_resolver creates an entry resolver that does not come with the model. It is advisable to save it in a variable for later use to avoid recreating this dictionary every time a lookup is initiated.

Testing

  • yaml → model → yaml
  • yaml → model → xml → model → yaml
  • yaml → pickle → yaml

must produce identical input and output at the ends of the chains

XML single output file must validate against WN-LMF-1.1.dtd

The validate package tests the model's coherence (but does not test XML well-formedness).

Authorship

Original code was written by John McCrae john@mccr.ae.

Bernard Bou revised, trimmed, revamped it 1313ou@gmail.

Licence

Licence is CC-4 for original code.

License is GPL-3 for revisions.

Related

The oewntk project is a tool suite written in Kotlin for JVM, that has similar design principles. Most notably, it exports to SQL, WNDB and JSON formats.

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

oewn_core-1.2.1.tar.gz (323.8 kB view details)

Uploaded Source

Built Distribution

oewn_core-1.2.1-py3-none-any.whl (56.6 kB view details)

Uploaded Python 3

File details

Details for the file oewn_core-1.2.1.tar.gz.

File metadata

  • Download URL: oewn_core-1.2.1.tar.gz
  • Upload date:
  • Size: 323.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for oewn_core-1.2.1.tar.gz
Algorithm Hash digest
SHA256 9919950dcc21a35313e123d3f728ab1e759c4c9a77163028ea9a4e16227edc1e
MD5 6847f4c286b467ebdb2f01038e6b9ea9
BLAKE2b-256 de5cd8f4d053f9372773ce37869e8d42d8d11c7ca6cfd51162deb95c0b94d150

See more details on using hashes here.

File details

Details for the file oewn_core-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: oewn_core-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 56.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for oewn_core-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d9b6d9c69f0423c89a3de03017aad9d3adce5f25ecf3aa9b0006f9edd53b5416
MD5 58ba415c90134e54a23d93e74b8cff6d
BLAKE2b-256 6eb16e666ee028c24926c17d2aaf705cedbba5b5f919b1e33a0ea577e97768e1

See more details on using hashes here.

Supported by

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