Nan*_*ard 5 python java string unicode
我们正在构建一个调用 Java 程序的 Python 3 程序。Java 程序(这是我们无法修改的第 3 方程序)用于标记字符串(查找单词)并提供其他注释。这些注释采用字符偏移的形式。
例如,我们可能会向程序提供字符串数据,例如"lovely weather today"
. 它提供类似于以下输出的内容:
0,6
7,14
15,20
Run Code Online (Sandbox Code Playgroud)
其中0,6
词“lovely”7,14
对应的偏移量,“weather”15,20
这个词对应的偏移量,以及“today”这个词在源字符串中的偏移量。我们在 Python 中读取这些偏移量以提取这些点处的文本并执行进一步处理。
只要字符在基本多语言平面 ( BMP )内,一切都很好。但是,如果不是,则此 Java 程序报告的偏移量在 Python 端显示为错误。
例如,给定 string "I feel today"
,Java 程序将输出:
0,1
2,6
7,9
10,15
Run Code Online (Sandbox Code Playgroud)
在 Python 方面,这些转换为:
0,1 "I"
2,6 "feel"
7,9 " "
10,15 "oday"
Run Code Online (Sandbox Code Playgroud)
最后一个索引在技术上无效。Java 将 "" 视为长度 2,这会导致该点之后的所有注释从 Python 程序的角度来看都相差一个。
据推测,这是因为 Java 在内部以 UTF-16esqe 方式对字符串进行编码,并且所有字符串操作都作用于这些 UTF-16esque代码单元。另一方面,Python 字符串似乎对实际的 unicode 字符(代码点)进行操作。因此,当一个字符出现在 BMP 之外时,Java 程序将其视为长度 2,而 Python 将其视为长度 1。
所以现在的问题是:在 Python 使用它们之前“纠正”这些偏移量的最佳方法是什么,以便注释子字符串与 Java 程序打算输出的内容一致?
您可以将字符串转换为 UTF16 编码的字节数组,然后使用偏移量(乘以 2,因为每个 UTF-16 代码单元有两个字节)来索引该数组:
x = "I feel today"
y = bytearray(x, "UTF-16LE")
offsets = [(0,1),(2,6),(7,9),(10,15)]
for word in offsets:
print(str(y[word[0]*2:word[1]*2], 'UTF-16LE'))
Run Code Online (Sandbox Code Playgroud)
输出:
I
feel
today
Run Code Online (Sandbox Code Playgroud)
或者,您可以将字符串中的每个 python 字符单独转换为 UTF-16,并计算它所需的代码单元数。这使您可以将代码单元(来自 Java)的索引映射到 Python 字符的索引:
from itertools import accumulate
x = "I feel today"
utf16offsets = [(0,1),(2,6),(7,9),(10,15)] # from java program
# map python string indices to an index in terms of utf-16 code units
chrLengths = [len(bytearray(ch, "UTF-16LE"))//2 for ch in x]
utf16indices = [0] + list(itertools.accumulate(chrLengths))
# reverse the map so that it maps utf16 indices to python indices
index_map = dict((x,i) for i, x in enumerate(utf16indices))
# convert the offsets from utf16 code-unit indices to python string indices
offsets = [(index_map[o[0]], index_map[o[1]]) for o in utf16offsets]
# now you can just use those indices as normal
for word in offsets:
print(x[word[0]:word[1]])
Run Code Online (Sandbox Code Playgroud)
输出:
I
feel
today
Run Code Online (Sandbox Code Playgroud)
上面的代码很混乱,也许可以变得更清晰,但你明白了。