Skip to main content

Converts a tree structure in to a valid JSON

Project description

Summary
=======

treetojson is a helpful in converting a given tree structure into a
valid JSON. Using this a tree strcuture can be easily interpreted to a
valid JSON. Further explaination on how the input should be given and
the output are explained under Basic Usage. This is pure Python code
with a single dependency of `NLTK`_. This can also be used along with
`NLTK RegexpParser`_.

This was a mainly developed due to a problem faced during a project on
Part-of-Speech (POS) tagger which is a part of Natural Language
Processing. When a tree structure is created with specfic tags of it
using the NLTK module, the tree strcuture was not able to map
accordingly to a JSON object using the existing libraries. The problem
occured due to the existence of repetitive ‘key’ since tags are fixed
for languages. Because of such a problem this was initially developed to
come out of this problem and was later decided to publish as an open
source Python module.

Further treetojson maintains the order provided. It does’nt mix up the
order. The output given will be a readable sentence as its given in to
treetojson. The output given by treetojson can be a respond to a HTTP
request, and can be used, manipulated and display appropriately in the
front-end.

Installation
============

The treetojson module is `published on the Python Package Index`_, so
you can install it using ``pip`` or ``easy_install``.

::

pip install treetojson

or

::

easy_install treetojson

That should be all you need to go.

Basic Usage
===========

Example1
--------

When a list containing words and it’s appropriate tags are provided as
follow:

::

>>> import treetojson
>>> sentence = [('Everyone', 'NN'), ('knows', 'VBZ'), ('an', 'DT'), ('Elephant', 'NN'), ('is', 'VBZ'), ('larger', 'JJR'), ('than', 'IN'), ('a', 'DT'), ('Dog', 'NN')]
>>> print treetojson.get_json(data=sentence)
{"SENTENCE":[{"NN":"Everyone"},{"VBZ":"knows"},{"DT":"an"},{"NN":"Elephant"},{"VBZ":"is"}, {"JJR":"larger"},{"IN":"than"},{"DT":"a"},{"NN":"Dog"}]}

Example2
--------

When a list containing words with appropriate tags along with a grammar
is provided:

::

>>> import treetojson
>>> sentence = [('Everyone', 'NN'), ('knows', 'VBZ'), ('an', 'DT'), ('Elephant', 'NN'), ('is', 'VBZ'), ('larger', 'JJR'), ('than', 'IN'), ('a', 'DT'), ('Dog', 'NN')]
>>> grammar = """
NP: {<PRP>?<JJ.*>*<NN.*>+}
CP: {<JJR|JJS>}
VERB: {<VB.*>}
THAN: {<IN>}
COMP: {<DT>?<NP><RB>?<VERB><DT>?<CP><THAN><DT>?<NP>}
"""
>>> print treetojson.get_json(data=sentence, grammar=grammar)
{"SENTENCE":[{"NP":[{"NN":"Everyone"}]},{"VERB":[{"VBZ":"knows"}]},{"COMP": [{"DT":"an"},{"NP":[{"NN":"Elephant"}]},{"VERB":[{"VBZ":"is"}]},{"CP":[{"JJR":"larger"}]},{"THAN":[{"IN":"than"}]},{"DT":"a"},{"NP":[{"NN":"Dog"}]}]}]}

.. _NLTK: http://www.nltk.org/
.. _NLTK RegexpParser: http://www.nltk.org/_modules/nltk/chunk/regexp.html
.. _published on the Python Package Index: https://pypi.python.org/pypi/treetojson
pandoc 1.19.1

Example3
--------

When words and labels or tags are seperately provided:

::

>>> import treetojson
>>> words = ['Everyone', 'knows', 'an', 'Elephant', 'is', 'larger', 'than', 'a', 'Dog']
>>> labels = ['NN', 'VBZ', 'DT', 'NN', 'VBZ', 'JJR', 'IN', 'DT', 'NN']
>>> print treetojson.get_json(words=words, label=labels)
{"SENTENCE":[{"NN":"Everyone"},{"VBZ":"knows"},{"DT":"an"},{"NN":"Elephant"},{"VBZ":"is"},{"JJR":"larger"},{"IN":"than"},{"DT":"a"},{"NN":"Dog"}]}

Example4
--------

When words and labels or tags seperately along with a grammar is
provided:

::

>>> import treetojson
>>> words = ['Everyone', 'knows', 'an', 'Elephant', 'is', 'larger', 'than', 'a', 'Dog']
>>> labels = ['NN', 'VBZ', 'DT', 'NN', 'VBZ', 'JJR', 'IN', 'DT', 'NN']
>>> grammar = """
NP: {<PRP>?<JJ.*>*<NN.*>+}
CP: {<JJR|JJS>}
VERB: {<VB.*>}
THAN: {<IN>}
COMP: {<DT>?<NP><RB>?<VERB><DT>?<CP><THAN><DT>?<NP>}
"""
>>> print treetojson.get_json(words=words, label=labels, grammar=grammar)
{"SENTENCE":[{"NP":[{"NN":"Everyone"}]},{"VERB":[{"VBZ":"knows"}]},{"COMP": [{"DT":"an"},{"NP":[{"NN":"Elephant"}]},{"VERB":[{"VBZ":"is"}]},{"CP":[{"JJR":"larger"}]},{"THAN":[{"IN":"than"}]},{"DT":"a"},{"NP":[{"NN":"Dog"}]}]}]}

Debugging
=========

You can enable debugging information.

::

>>> import treetojson
>>> treetojson.set_debug()
Debug mode is on. Events are logged at: treetojson.log

To turn debug mode off, call ``set_debug`` with an argument of
``False``:

::

>>> treetojson.set_debug(False)
Debug mode is off.

| If you encounter any errors in the code, please file an issue on
github:
| https://github.com/saadsahibjan/treetojson/issues

Author
======

- Author: Saad Sahibjan
- Email: saadsahibjan@gmail.com
- Repository: https://github.com/saadsahibjan/treetojson

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

treetojson-0.0.9.tar.gz (4.6 kB view details)

Uploaded Source

File details

Details for the file treetojson-0.0.9.tar.gz.

File metadata

  • Download URL: treetojson-0.0.9.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for treetojson-0.0.9.tar.gz
Algorithm Hash digest
SHA256 adf1bfa8d10badac90be1d5d6f95cf9ca645c9192fbbe5e0e751cb4ff30e1bee
MD5 3990df8950fa49563a75d7f7a5797a11
BLAKE2b-256 94fb71561078ac8257623fcb1541d269186ba213920b628f10111bfe19561ff1

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