我正在尝试生成类似于以下格式的 YAML:
version: '3.1'
nlu:
- intent: anxiety_intent
examples: |
- my anxiety has been overwhelming lately.
- i am struggling with my stress levels right now.
- i'm having a bit of a crisis and not serious, but need to talk just a very long panic attack.
到目前为止,我能够生成如下 YAML:
version: '3.1'
nlu:
- intent: anxiety_intent
examples: |
- my anxiety has been overwhelming lately.
- i am struggling with my stress levels right now.
- my anxiety has been bad.
- i'm having a bit of a crisis and not serious, but need to talk just a very long
panic attack.
如您所见,最后一句话很长,被换行到下一行。我怎样才能防止这种情况发生?
到目前为止,这是我的方法:
import yaml
from yaml.resolver import BaseResolver
class AsLiteral(str):
pass
def represent_literal(dumper, data):
return dumper.represent_scalar(BaseResolver.DEFAULT_SCALAR_TAG, data, style="|")
yaml.add_representer(AsLiteral, represent_literal)
##########################################################################
###################### INTENT FUNCTIONS ##################################
##########################################################################
def generate_intent_dict(worksheet):
rows = worksheet.get_all_records()
df = pd.DataFrame(rows)
all_intent_list = {col : [x for x in df[col].values if x != ''] for col in df}
filtered_intent_list = {k: v for k, v in all_intent_list.items() if v}
return filtered_intent_list
def generate_yaml_specific_intent_dict(intents_dict):
yaml_dict = {
"version" : "3.1",
"nlu" : [
],
}
for k, val in intents_dict.items():
d1 = dict()
d1['intent'] = k
info_str = AsLiteral(yaml.dump(val))
d1['examples'] = info_str
yaml_dict['nlu'].append(d1)
return yaml_dict
##########################################################################
###################### FILE SAVING FUNCTIONS #############################
##########################################################################
def yaml_specific_intent_write(yaml_dict, path, fname):
with open(os.path.abspath(path + fname), 'w') as f:
yaml.dump(yaml_dict, f, sort_keys=False)
我尝试了很多东西,但都无济于事。我猜 YAML 转储存在一些问题。任何帮助将不胜感激。