简体   繁体   中英

Converting java.lang.string to PYthon string/dictionary

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

This behaviour occurs because of changes in the JPype module.

You could amend that part of sutime.py which calls the JVM to supply the additional argument which is included for backwards compatibility :

        jpype.getDefaultJVMPath(),
        '-Djava.class.path={classpath}'.format(classpath=self._classpath),
        convertStrings=True

Alternately, you can reproduce the earlier behaviour by downgrading the JPype module to an earlier version like version 0.7.5 -- for example, by

pip3 uninstall JPype1
pip3 install "JPype1==0.7.5"

I have the exact problem and I solved it by converting the java.lang.String into python string before converting it into json.

First, amend the last two lines of your sutime.py to this

return json.loads(str(self._sutime.annotate(input_str, reference_date)))[0]
    return json.loads(str(self._sutime.annotate(input_str)))[0]

Then in your main class, assign the sutime.parse without dumps() to a variable

time = sutime.parse(test_case) 

and perform the following checking methods:

print(type(time))
print(time['value'])

The first print will return type'dict' and the second print will return "2020-06-10".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM