I am using SUTime library for a text related task. Here's the original code used:
import os
import json
from sutime import SUTime
import ast
import json
if __name__ == '__main__':
test_case = u'I need a desk for tomorrow from 2pm to 3pm'
jar_files = os.path.join(os.path.dirname('path to SUTime jar files'), 'jars')
sutime = SUTime(jars=jar_files, mark_time_ranges=True)
op=sutime.parse(test_case)
op1 = op
op2 = []
op2 = op1#json.loads(op1)#ast.literal_eval(op1)
#print(op2)
json_op = json.dumps(list2)
print(json_op)
Note: The original library(sutime.parse) returns json.loads(self._sutime.annotate(input_str)) which was returning this error:
TypeError: the JSON object must be str, bytes or bytearray, not 'java.lang.String'
So, I modified it such that sutime.parse returns self._sutime.annotate(input_str), which returns the output in java.lang.string format, as follows:
[{"start":18,"end":26,"text":"tomorrow","type":"DATE","value":"2020-07-28"},{"start":27,"end":42,"text":"from 2pm to 3pm","type":"DURATION","value":{"end":"T15:00","begin":"T14:00"}}]
It's been tricky to use this output for analysis/further processing, as some operations that I've been trying to perform on it, like converting to JSON, string assignment result in error saying that it is json.lang.string and hence the operation could not be performed. Is there a way to convert this to a Python string or a Python dictionary. Converting it to a dictionary would be ideal for my use case (or two dictionaries in the case of example provided above). I have tried json.loads to try to convert this java.lang.string to a list of dictionaries but that fails with an error saying:
TypeError: the JSON object must be str, bytes or bytearray, not 'java.
I've tried ast.literal_eval() as well which returns an error saying:
ValueError: malformed node or string: '[{"start":72,"end":80,"text":"December","type":"DATE","value":"2020-12"},{"start":111,"end":119,"text":"November","type":"DATE","value":"2020-11"}]'
So, in a final effort, I've tried to take off the square braces by assigning:
op1[0] = ''
which results in an error saying:
TypeError: 'java.lang.String' object does not support item assignment