Package: treetojson
treetojsonSimple Library to map a tree structure to a valid JSON object.This treetojson module ispublished on the Python Package
IndexSummarytreetojson 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 ofNLTK. This can
also be used along withNLTK
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 specific tags of it
using the NLTK module, the tree structure was not able to map
accordingly to a JSON object using the existing libraries. The problem
occurred 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.InstallationThe treetojson module ispublished on the Python Package
Index, so you can install
it usingpiporeasy_install.pip install treetojsonoreasy_install treetojsonThat should be all you need to go.Basic UsageExample1When 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)
{u'SENTENCE': [{u'NN': u'Everyone'}, {u'VBZ': u'knows'}, {u'DT': u'an'}, {u'NN': u'Elephant'}, {u'VBZ': u'is'},
{u'JJR': u'larger'}, {u'IN': u'than'}, {u'DT': u'a'}, {u'NN': u'Dog'}]}Example2When 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)
{u'SENTENCE': [{u'NP': [{u'NN': u'Everyone'}]}, {u'VERB': [{u'VBZ': u'knows'}]}, {u'COMP': [{u'DT': u'an'},
{u'NP': [{u'NN': u'Elephant'}]}, {u'VERB': [{u'VBZ': u'is'}]}, {u'CP': [{u'JJR': u'larger'}]},
{u'THAN': [{u'IN': u'than'}]}, {u'DT': u'a'}, {u'NP': [{u'NN': u'Dog'}]}]}]}Example3When 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)
{u'SENTENCE': [{u'NN': u'Everyone'}, {u'VBZ': u'knows'}, {u'DT': u'an'}, {u'NN': u'Elephant'}, {u'VBZ': u'is'},
{u'JJR': u'larger'}, {u'IN': u'than'}, {u'DT': u'a'}, {u'NN': u'Dog'}]}Example4When 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)
{u'SENTENCE': [{u'NP': [{u'NN': u'Everyone'}]}, {u'VERB': [{u'VBZ': u'knows'}]}, {u'COMP': [{u'DT': u'an'},
{u'NP': [{u'NN': u'Elephant'}]}, {u'VERB': [{u'VBZ': u'is'}]}, {u'CP': [{u'JJR': u'larger'}]},
{u'THAN': [{u'IN': u'than'}]}, {u'DT': u'a'}, {u'NP': [{u'NN': u'Dog'}]}]}]}DebuggingYou can enable debugging information.>>> import treetojson
>>> treetojson.set_debug()
Debug mode is on. Events are logged at: treetojson.logTo turn debug mode off, callset_debugwith an argument ofFalse:>>> 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/issuesContributing guideUse guidelines provided inCONTRIBUTING.mdLicenseMITAuthorAuthor: Saad SahibjanEmail:saadsahibjan@gmail.comRepository:https://github.com/saadsahibjan/treetojson