简体   繁体   English

将 java.lang.string 转换为 PYthon 字符串/字典

[英]Converting java.lang.string to PYthon string/dictionary

I am using SUTime library for a text related task.我正在使用 SUTime 库来完成与文本相关的任务。 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:注意:原始库(sutime.parse)返回 json.loads(self._sutime.annotate(input_str)) 返回此错误:

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:所以,我修改它,使sutime.parse返回self._sutime.annotate(input_str),返回java.lang.string格式的output,如下:

[{"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.使用这个 output 进行分析/进一步处理是很棘手的,因为我一直在尝试对它执行一些操作,比如转换为 JSON,字符串分配导致错误,说它是 Z466DEEC76ECDFA6D38571 操作。无法执行。 Is there a way to convert this to a Python string or a Python dictionary.有没有办法将其转换为 Python 字符串或 Python 字典。 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:我已经尝试 json.loads 尝试将此 java.lang.string 转换为字典列表,但失败并显示错误消息:

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:我也尝试过 ast.literal_eval() ,它返回一个错误说:

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.由于 JPype 模块中的更改,会出现此问题。

You could amend that part of sutime.py which calls the JVM to supply the additional argument which is included for backwards compatibility :您可以修改调用 JVM 的sutime.py的那部分,以提供为向后兼容而包含的附加参数:

        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或者,您可以通过将 JPype 模块降级到早期版本(如 0.7.5 版)来重现早期行为 - 例如,通过

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.我有确切的问题,我通过将 java.lang.String 转换为 python 字符串然后将其转换为 json 来解决它。

First, amend the last two lines of your sutime.py to this首先,将 sutime.py 的最后两行修改为此

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然后在您的主要 class 中,将不带 dumps() 的 sutime.parse 分配给一个变量

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".第一次打印将返回 type'dict',第二次打印将返回“2020-06-10”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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