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) IfTrue
, 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ikml_doc-0.21.2.tar.gz
.
File metadata
- Download URL: ikml_doc-0.21.2.tar.gz
- Upload date:
- Size: 24.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
ac079e15ecef57f93dbf0d4fc5a738819e6d33ed49dd7a80c0f48dbe86f8b5ea
|
|
MD5 |
31c6558e29de7fbf6acf7f1caa31c050
|
|
BLAKE2b-256 |
26c06acd9a00b639c295d201839b947beeb37109475803e9c7754a38370481eb
|
File details
Details for the file ikml_doc-0.21.2-py3-none-any.whl
.
File metadata
- Download URL: ikml_doc-0.21.2-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
aa865f14c9c6b6cc81cc89dd047946f5337efbd16816a353a8a6287e9415f251
|
|
MD5 |
17cf5810f9550a249199a653277d0edc
|
|
BLAKE2b-256 |
8a2ca0fd48c4d3ae89e2db51bdf5b75c6d088097bf57fa867f76928534c3419d
|