Skip to content

Commit

Permalink
feat: ✨ ONA转译器「ANTICIPATE」输出转译支持
Browse files Browse the repository at this point in the history
基于正则匹配+Narsese方言解析,对ONA的「decision expectation」支持转译成「ANTICIPATE」类型输出
  • Loading branch information
ARCJ137442 committed Apr 9, 2024
1 parent db604e8 commit 9e28776
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "babel_nar"
version = "0.20.0"
version = "0.20.1"
edition = "2021"
description = """
Implementation and application supports of the NAVM model
Expand Down
1 change: 0 additions & 1 deletion src/bin/babelnar_cli/runtime_manage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ where
{
// 缓存输出
// * 🚩在缓存时格式化输出
// TODO: 【2024-04-08 19:15:30】现在必须不再能直接`put`输出了:要兼容Websocket情形
match output_cache.lock() {
Ok(mut output_cache) => output_cache.put(output)?,
Err(e) => eprintln_cli!([Error] "缓存NAVM运行时输出时发生错误:{e}"),
Expand Down
35 changes: 35 additions & 0 deletions src/cin_implements/ona/translators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use super::dialect::parse as parse_dialect_ona;
use crate::{
cin_implements::ona::{fold_pest_compound, DialectParser, Rule},
cli_support::io::output_print::OutputType,
runtimes::TranslateError,
};
use anyhow::Result;
Expand Down Expand Up @@ -180,6 +181,13 @@ pub fn output_translate(content_raw: String) -> Result<Output> {
operation: parse_operation_ona(&content_raw)?,
content_raw,
},
// * 🚩对于「决策预期→ANTICIPATE」的特殊语法
// * 🚩【2024-04-02 18:45:17】仅截取`executed with args`,不截取`executed by NAR`
_ if content_raw.contains("decision expectation=") => Output::UNCLASSIFIED {
r#type: "ANTICIPATE".into(),
narsese: parse_anticipate_ona(&content_raw)?,
content: content_raw,
},
// 若是连续的「头部」⇒识别为「未归类」类型
_ if !content_raw.contains(char::is_whitespace) => Output::UNCLASSIFIED {
r#type: head.into(),
Expand Down Expand Up @@ -244,6 +252,33 @@ pub fn parse_operation_ona(content_raw: &str) -> Result<Operation> {
}
}

/// (ONA)从原始输出中解析「ANTICIPATE」预期
/// * 🚩通过「前缀正则截取」分割并解析随后Narsese获得
/// * 📄`"decision expectation=0.502326 implication: <((<{SELF} --> [good]> &/ <a --> b>) &/ <(* {SELF}) --> ^left>) =/> <{SELF} --> [good]>>. Truth: frequency=0.872512 confidence=0.294720 dt=12.000000 precondition: (<{SELF} --> [good]> &/ <a --> b>). :|: Truth: frequency=1.000000 confidence=0.360000 occurrenceTime=35124\n"`
/// * 📄`"decision expectation=0.578198 implication: <(a &/ ^left) =/> g>. Truth: frequency=1.000000 confidence=0.241351 dt=1.000000 precondition: a. :|: Truth: frequency=1.000000 confidence=0.900000 occurrenceTime=4\n"`
pub fn parse_anticipate_ona(content_raw: &str) -> Result<Option<Narsese>> {
// 正则捕获
let re_operation = Regex::new(r"implication:\s*(.*)\s*dt=").unwrap();
let captures = re_capture(&re_operation, content_raw.trim())?;
match captures {
Some(captures) => {
// 获取内容
let narsese_content = captures[1].to_string();
// 解析
let parse_result =
parse_narsese_ona("ANTICIPATE", narsese_content.trim()).inspect_err(|e| {
OutputType::Error.eprint_line(&format!("ONA「预期」解析失败:{e}"));
});
// 返回
parse_result
}
// 截取失败的情形
None => {
OutputType::Error.eprint_line(&format!("ONA「预期」正则捕获失败:{content_raw:?}"));
Ok(None)
}
}
}
/// 操作参数提取
/// * 🎯从一个解析出来的词项中提取出「操作参数列表」
/// * 🚩测试环境中仅允许「复合词项」被解包
Expand Down

0 comments on commit 9e28776

Please sign in to comment.