Json格式怎么做 [英] how to do Json format
本文介绍了Json格式怎么做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(S
(PERSON Rami/NNP Eid/NNP)
is/VBZ
studying/VBG
at/IN
(ORGANIZATION Stony/NNP Brook/NNP University/NNP)
in/IN
(LOCATION NY/NNP))
这是NLTK代码的输出,现在我想将其存储在json文件中
This is output of NLTK code now i want to store it in json file like
import json
data = {
'Rami Eid':{'ORGANIZATION': 'Stony Brook University', 'location':'NY'},
'GuruRaj Bagali':{'job': 'professor', 'location': 'NY'}
}
我想像上面的格式那样将块树存储到json文件中怎么办?
I want store chunk tree into json file like above format how to do?
推荐答案
nltk = """
(S
(PERSON Rami/NNP Eid/NNP)
is/VBZ
studying/VBG
at/IN
(ORGANIZATION Stony/NNP Brook/NNP University/NNP)
in/IN
(LOCATION NY/NNP)) """
from pyparsing import Suppress, ungroup, Word, alphas, Group, Dict, OneOrMore
LPAR,RPAR,SLASH = map(Suppress,"()/")
parsed_word = ungroup(Word(alphas) + Suppress(SLASH + Word(alphas)))
named_token = Group(LPAR + Word(alphas)("name") +
OneOrMore(parsed_word).setParseAction(' '.join)("value") +
RPAR)
subject = (Suppress("S") + named_token)
nltk_expr = (LPAR + subject("subject") +
Dict(OneOrMore(named_token | Suppress(parsed_word)))("predicate") +
RPAR)
def make_subject_main_key(t):
subname = t.pop('subject')[0].value
subdesc = t.pop('predicate')
t[subname] = subdesc
nltk_expr.setParseAction(make_subject_main_key)
print nltk_expr.parseString(nltk).asDict()
打印
{'Rami Eid': {'ORGANIZATION': 'Stony Brook University', 'LOCATION': 'NY'}}
这篇关于Json格式怎么做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文