Skip to main content

A parser for IKML (Indic Knowledge Markup Language) Documents

Project description

ikml-doc

IKML Document Parser

Description

A parser for IKML (Indic Knowledge Markup Language)

Installation

To install the IKML parser, use the following command:

pip install ikml_doc

Usage

The IKML parser can be used to load IKML data from a URL or a local file, and then convert it into various formats such as JSON, XML, and plain text.

Loading IKML Data

You can load IKML data from a URL or a local file using the IKML_Document class.

from ikml_doc import IKML_Document
ikml_url = "https://api.siddhantakosha.org/ikmldata?expand_inline=1&gpath=libraries/smap-granthas/Tattvabodha/all-ikml.txt&fmt=txt"
# Load from URL
doc = IKML_Document(url=ikml_url)

# Load from raw data (string or dict)
doc = IKML_Document(data="path_to_ikml.txt")

Saving Data

def save(self, exclude_root=False, filename="out_ikml.txt")
  • Parameters:
    • exclude_root: (bool) If True, the root node is excluded from the saved output.
    • filename: (string) The name of the file to save the IKML data.

Example:

doc.save(filename="output_ikml.txt")

Converting Data

You can convert the loaded data into different formats.

  • To Dictionary:
doc_dict = doc.to_dict()
  • To JSON:
doc_json = doc.to_json()
  • To XML:
doc_xml = doc.to_xml(put_attrs_inside=True)
  • To Plain Text:
doc_txt = doc.to_txt(exclude_root=False, quoted_attr=False)

Iterating Over Nodes

You can iterate through the nodes using a generator method.

for node in doc.iter():
    print("  "*node.depth, node)

Iterating Over Immediate Children

Use node.children to iterate over all child nodes. Use node.node_children to iterate over all child nodes that aren't attributes where node.is_attribute is False.

for node in doc.iter():
    print("  "*node.depth, node)
    for child in node.children:
        print("  "*child.depth, child)

Finding Nodes

You can find nodes based on specific criteria.

  • Find Children by Tag Name:
for child in doc.find_children(tag_name="va"):
    print(child)
  • Get a Node by ID:
node = doc.get(tag_id="l.smaps.TatvB.v-1")

Accessing Attributes of Nodes

You can access attributes of the nodes directly.

  • Get Node Attributes:
attributes = node.keys()  # Get list of keys
value = node.get("id")  # Get value of a specific property

Validating Schema

You can validate the structure of the IKML document against a schema.

schema_doc = IKML_Document(data=schema_data)
is_valid = doc.validate_schema(schema_doc)

Example Usage

from ikml_doc import IKML_Document

# Loading IKML data from a URL
ikml_url = "https://api.siddhantakosha.org/ikmldata?expand_inline=1&gpath=libraries/smap-granthas/Tattvabodha/all-ikml.txt&fmt=txt"
doc = IKML_Document(url=ikml_url)

# Iterating over all nodes
for i, node in enumerate(doc.iter()):
    node["new_attr_count"] = i

# Saving the loaded document
doc.save(filename="Tattvabodha_output.txt", exclude_root=True)

# Accessing a specific node and its properties
node = doc.get("l.smaps.TatvB.v-10")
print(node.keys())
print(node.get("new_attr_count"))

Contributing

Contributions are welcome. Please open an issue to discuss any changes before submitting a pull request.

License

This project is licensed under the LGPL License.

Acknowledgments

Special thanks to the contributors and maintainers of the IKML project.

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

ikml_doc-0.22.0.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

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

ikml_doc-0.22.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file ikml_doc-0.22.0.tar.gz.

File metadata

  • Download URL: ikml_doc-0.22.0.tar.gz
  • Upload date:
  • Size: 25.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for ikml_doc-0.22.0.tar.gz
Algorithm Hash digest
SHA256 a7af9771bc0a2122e1ac6d463cc8ffbc0e65df1b14302ea6336645828cfa386d
MD5 d99e8a068a710c69f79fa7f84ef841f3
BLAKE2b-256 2ee3a8d7f8eeff6adc4abae982f6492a55f3f291bb6e69c2cb747063f071bb9f

See more details on using hashes here.

File details

Details for the file ikml_doc-0.22.0-py3-none-any.whl.

File metadata

  • Download URL: ikml_doc-0.22.0-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for ikml_doc-0.22.0-py3-none-any.whl
Algorithm Hash digest
SHA256 312844fdd0c1a13ca9e3b1588bbeb128ac459c8cd69a5bc0b78f697417444bd0
MD5 e78fbf3488179106fd3f99c75af269d1
BLAKE2b-256 e9a885dc0a95473e5c235f089ebd3b3ef66a87242670c254daa591ff4b9a9d7b

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