【问题标题】:How to tokenize input text in android studio to process in NLP model?如何标记 android studio 中的输入文本以在 NLP 模型中处理?
【发布时间】:2020-05-15 05:17:35
【问题描述】:
当我创建 NLP 模型时,我使用 keras 分词器对我的训练数据进行分词。所以训练数据中的每个单词都有一个与之关联的数字。现在我想在 android 应用程序中运行模型。所以我将模型转换为 tflite 格式。
现在在我的应用程序中,当用户给我一个文本输入时,我应该使用我用于训练数据的相同标记将其转换为数字数组。我无法这样做,因为 tflite 仅包含模型而不包含标记器。
如何做到这一点?
【问题讨论】:
标签:
android
tensorflow
tensorflow2.0
tf.keras
tensorflow-lite
【解决方案1】:
您需要将标记化单词的词汇表从 Python 迁移到 Android。使用tf.keras.preprocessing.text.Tokenizer.word_index 属性。这是dict 中的( word , index ),您需要将其导出为 JSON 文件。
import json
with open( 'android/word_dict.json' , 'w' ) as file:
json.dump( tokenizer.word_index , file )
现在,我们在 Android 中解析 JSON 文件并创建一个Hashmap<String,Integer>。
- 从用户那里获取输入字符串并对其进行标记。
- 接下来,查找 Hashmap 中使用的每个单词的索引。
- 将这些整数存储在
int[] 中,这是我们模型的输入。
整个过程我已经在这个博客里讨论过了->Text Classification in Android with TensorFlow