Skip to main content
 首页 » 编程设计

python之如何直接传递 json 对象以从 python 在 rasa nlu 中训练

2024年10月01日8mengfanrong

我正在使用 rasa nlu 来训练数据。根据 http://nlu.rasa.ai/python.html 中的文档,必须使用以下代码来训练文件 demo-rasa.json 中存在的数据

from rasa_nlu.converters import load_data 
from rasa_nlu.config import RasaNLUConfig 
from rasa_nlu.model import Trainer 
 
training_data = load_data('data/examples/rasa/demo-rasa.json') 
trainer = Trainer(RasaNLUConfig("sample_configs/config_spacy.json")) 
trainer.train(training_data) 
model_directory = trainer.persist('./projects/default/') 

但是我们如何从 json 对象中读取数据进行训练。

请您参考如下方法:

如果您查看 load_data 的实现,它执行两个步骤:

  1. 猜猜文件格式
  2. 使用适当的加载方法加载文件

最简单的解决方案是将您的 json 对象写入文件或 StringIO 对象。

或者,您可以选择您需要的特定加载函数,例如 load_rasa_data并将读取的文件分开。对于此示例,您可能只使用整个函数并删除行 data = _read_json_from_file(filename) .

我有点惊讶地发现目前无法读取已加载的 json 对象。如果您决定为此调整功能,您可以考虑为其编写拉取请求。