基本用法

例1

当包含单词和适当标记的列表作为

跟随

:>>> 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'}]}

例2

当包含带有适当标记和语法的单词的列表

提供:

>>> import treetojson
>>> sentence = [('Everyone', 'NN'), ('knows', 'VBZ'), ('an', 'DT'), ('Elephant', 'NN'), ('is', 'VBZ'), ('larger', 'JJR'),
('than', 'IN'), ('a', 'DT'), ('Dog', 'NN')]
>>> grammar = """
NP: {?*+}
CP: {}
VERB: {}
THAN: {}
COMP: {
????} 
 
"""
>>> 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'}]}]}]}

例3

当单独提供文字和标签或标签时:

>>> 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'}]}

例4

当单词和标签或标签与语法分开时

提供:

>>> import treetojson
>>> words = ['Everyone', 'knows', 'an', 'Elephant', 'is', 'larger', 'than', 'a', 'Dog']
>>> labels = ['NN', 'VBZ', 'DT', 'NN', 'VBZ', 'JJR', 'IN', 'DT', 'NN']
>>> grammar = """
NP: {?*+}
CP: {}
VERB: {}
THAN: {}
COMP: {
????} 
 
"""
>>> 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'}]}]}]}