1

After one day of looking through Internet I ask this question here:

Is there any way to get pyparsing results, f.e.

result = longSentence.parseString(text.lower())

in a JSON format?

Update 11:46 27 May 2013:
Ok. So I try to make a parser for easy English.
The results for a sentence "Go 5 metres and what do you see?" now are printed as lists:

[[[['go'], [['5', 'metres']]]], 'and', ['what do you see?']]

['what do you see?'] is marked by SetResultsName('Question')
Others are marked similar.

It would be great if it will print that data in this form:

{
    "Sentence1":
    {
        "Order":
        {
            "Predicate": 'go',
            "Subject": ['5', 'metres']
        }
    },
    "Sentence2":
    {
        "Question": 'what do you see?'
    }
}

2 Answers2

3

You'd have to be more explicit, but the general approach would to be to take the parse result and use the json module (whether you want this as a nested dictionary/list, flattened list/etc... is up to you)

import json
json_string = json.dumps(result.asList())
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
2

Ok. After another try of going through documentation I found a very nice way to return this data in a XML form:

result = longSentence.parseString(text.lower()).asXML()

If I add this question with answers - it's done. So it's not so easy, but also not so hard to get results in JSON form. I hope anyone in need will find that.

Community
  • 1
  • 1