问题
(S
(PERSON Rami/NNP Eid/NNP)
is/VBZ
studying/VBG
at/IN
(ORGANIZATION Stony/NNP Brook/NNP University/NNP)
in/IN
(LOCATION NY/NNP))
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'}
}
I want store chunk tree into json file like above format how to do?
回答1:
This should do it.
import json
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
回答2:
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()
prints
{'Rami Eid': {'ORGANIZATION': 'Stony Brook University', 'LOCATION': 'NY'}}
回答3:
This will give you nicely formatted json file.
with open('/path/to/your/new_json_file.json', "w") as f:
f.write(json.dumps(data, indent=4, sort_keys=True))
This will create a file your_json_file.json
in in your/
directory with.
{
"GuruRaj Bagali": {
"job": "professor",
"location": "NY"
},
"Rami Eid": {
"ORGANIZATION": "Stony Brook University",
"location": "NY"
}
}
The indentation and sort_keys arguments are optional. Depending on how you're using it they may be nice to have.
You could also put the same data on one line and wouldn't need to import json.
with open('/path/to/your/new_json_file.json', "w") as f:
f.write('%s' % data)
Will create{'Rami Eid': {'ORGANIZATION': 'Stony Brook University', 'location': 'NY'}, 'GuruRaj Bagali': {'job': 'professor', 'location': 'NY'}}
来源:https://stackoverflow.com/questions/35193400/how-to-do-json-format