首页 > 解决方案 > 'tf.keras.preprocessing.text_dataset_from_directory' 函数未读取阿拉伯语文本文件

问题描述

我制作了一个机器学习模型,对英语句子进行情感分析,但现在我想为阿拉伯语句子训练相同的模型,但是性能与英语模型不同,因为我知道我使用了翻译成阿拉伯语的相同数据集。我注意到来自 tensorflow 的函数raw_val_ds = tf.keras.preprocessing.text_dataset_from_directory(train_dir, batch_size=batch_size, validation_split=0.2, subset='validation', seed=seed)没有以阿拉伯语输出:

for text_batch, label_batch in raw_train_ds.take(1):
  for i in range(3):
    print("Review", text_batch.numpy()[i])
    print("Label", label_batch.numpy()[i])

输出:

Review b'.\xef\xba\xaa\xef\xbb\xb4\xef\xba\x9f \xef\xbb\xaa\xef\xbb\xa8\xef\xbb\x9c\xef\xbb\x9f \xef\xba\x80\xef\xbb\xb2\xef\xba\xb8\xef\xbb\x9f\xef\xba\x8d \xef\xba\xbe\xef\xbb\x8c\xef\xba\x91 \xef\xbb\x92\xef\xba\xa3\xef\xba\x8d\xef\xba\xaf \xef\xbb\xaa\xef\xbb\xa7\xef\xba\x87 .Quatermain \xef\xba\x94\xef\xba\xbc\xef\xbb\x98\xef\xbb\x9f 
Label 1

但是用英语:

Review b"David Mamet is a very interesting and a very un-equal director. His first movie 'House of Games' was the one I liked best, and it set a series of films with characters whose perspective of life changes as they get into complicated situations, and so does the perspective of the viewer.
Label 1

所以我认为这是问题所在,所以如果有人可以提供帮助,我将不胜感激!谢谢!

标签: pythontensorflowkeras

解决方案


在这一行中,您将输出转换为 UTF-8 编码。

print("Review", text_batch.numpy()[i], encoding='utf-8')

阿拉伯文本中的字符以字节序列打印而英文字符不打印的原因是阿拉伯字符的代码点> = 128,英文字符<128。(参考)

省略编码参数,您应该得到正确的输出。

print("Review", text_batch.numpy()[i])

推荐阅读