【问题标题】:Tensorflow cannot split Unicode UTF-8 characterTensorflow 无法拆分 Unicode UTF-8 字符
【发布时间】:2021-03-25 19:59:32
【问题描述】:

我使用tf.strings.unicode_split 分割文本字符。 当我使用英文字符时,它工作正常

example_texts = ['hello world']
chars = tf.strings.unicode_split(example_texts, input_encoding='UTF-8')
print(chars)

<tf.RaggedTensor [[b'h', b'e', b'l', b'l', b'o', b' ', b'w', b'o', b'r', b'l', b'd']]>

但如果更改为 UTF-8 Unicode 字符,它就不能像英文字符那样工作

example_texts = ['سلام دنیا']
chars = tf.strings.unicode_split(example_texts, input_encoding='UTF-8')
print(chars)

<tf.RaggedTensor [[b'\xd8\xb3', b'\xd9\x84', b'\xd8\xa7', b'\xd9\x85', b' ', b'\xd8\xaf', 
b'\xd9\x86', b'\xdb\x8c', b'\xd8\xa7']]>

谢谢。

【问题讨论】:

  • 对我来说看起来是正确的。你期待的是什么?
  • 我期待这样的分裂波斯字符:س ل ا م 。不是这样的:xd8\xb3 xd8\x84 ...
  • 显然,字符被编码为UTF-8。在英文示例中,同样的情况也发生了(字符是字节字符串——参见字节前缀),您似乎并不介意。试试这个:b'\xd8\xb3'.decode('utf8') == 'س',就像b'h'.decode('utf8') == 'h'
  • 我试过 ".decode('utf-8') " 成功了,谢谢

标签: python tensorflow unicode utf-8 python-unicode


【解决方案1】:

来自评论

显然,字符被编码为 UTF-8。在英语中 例如同样的情况(字符是字节字符串 - cf. 字节前缀),你似乎并不介意。要查看您尝试的波斯语字符 这个:b'\xd8\xb3'.decode('utf8') == 'س',就像b'h'.decode('utf8') == 'h'(译自 lenz)

【讨论】:

    猜你喜欢
    • 2019-03-08
    • 2021-09-08
    • 2014-03-06
    • 2015-03-17
    • 1970-01-01
    • 2021-08-13
    • 2014-07-15
    • 2014-10-14
    相关资源
    最近更新 更多