当前位置: 首页 > news >正文

Rail开发日志_2

上一篇笔记概括了分词方法。

确定一个简单的目标

先实现Comment类型和Category类型,因为最接近行和领域的概念本质。

struct Comment {content: String,
}
struct Category {name: String,
}

以上两者只不过是中间结果。
其中,Line_data::Category携带的String应当为Element,但遵循从少到多的思路,先写成String。(其实是太菜了不会写)
Category用于创建Domain
以下是从分词结果到语法分析结果的桥梁。

enum LineData {Comment(Comment),Category(Category),
}
struct Line {indent: usize,data: LineData,
}

其中Line用于代表行。

语法分析

输入分词结果和缩进

fn into_lines(line_list: Vec<Vec<String>>, indent_list: Vec<usize>) -> Vec<Line> {let mut local: Vec<Line> = Vec::new();for (i, tokens) in line_list.iter().enumerate() {let indent = indent_list[i];let data = match tokens[0].as_str() {"/" => {LineData::Comment(Comment { content: tokens[1].clone() })}"+" => {LineData::Category(Category { name: tokens[1].clone() })}_ => {continue}};local.push(Line { indent, data });}local
}

检查函数如下

fn check_lines(lines: &Vec<Line>) {for line in lines.iter() {let indent_str = " ".repeat(line.indent);match &line.data {LineData::Comment(comment) => {println!("{}Comment: {}", indent_str, comment.content);}LineData::Category(category) => {println!("{}Category: {}", indent_str, category.name);}}}
}

测试

完整的测试代码如下。

fn main() {let test_input = String::from(r"
/ test_line_for_a_comment
+ test_category/ test_line_for_a_comment/ test_line_for_a_comment+ test_category/ test_line_for_a_comment/ test_line_for_a_comment/ test_line_for_a_comment/ test_line_for_a_comment/ test_line_for_a_comment+ test_category/ test_line_for_a_comment/ test_line_for_a_comment+ test_category
+ test_category/ test_line_for_a_comment
+ test_category_with_an_empty_category+ test_an_empty_category
/ test_line_for_a_comment
/ test_line_for_a_comment
/ test_line_for_a_comment
/ test_line_for_a_comment
");let (line_list, indent_list) = into_tokens(&test_input);check_tokens(&line_list, &indent_list);let lines: Vec<Line> = into_lines(line_list, indent_list);check_lines(&lines)
}fn into_tokens(lines_input: &str) -> (Vec<Vec<String>>, Vec<usize>) {let mut word_current = String::new();let mut line_current = Vec::new();let mut lines_list_splited = Vec::new();let mut indent_current = 0;let mut indent_list = Vec::new();let mut indent_state_in = true;for char in lines_input.chars() {if char == '\n' {if !word_current.is_empty() {line_current.push(word_current.clone());word_current.clear();}if !line_current.is_empty() {lines_list_splited.push(line_current.clone());indent_list.push(indent_current);}line_current.clear();indent_current = 0;indent_state_in = true;} else if char == ' ' {if indent_state_in {indent_current += 1;} else if !word_current.is_empty() {line_current.push(word_current.clone());word_current.clear();}} else {indent_state_in = false;word_current.push(char);}}if !word_current.is_empty() {line_current.push(word_current);}if !line_current.is_empty() {lines_list_splited.push(line_current);indent_list.push(indent_current);}(lines_list_splited, indent_list)
}
fn check_tokens(token_list: &Vec<Vec<String>>, indent_list: &Vec<usize>) {for i in 0..token_list.len() {println!("[{}: {}] {:?}", i + 1,  indent_list[i], token_list[i]);}
}struct Comment {content: String,
}
struct Category {name: String,
}
fn into_lines(line_list: Vec<Vec<String>>, indent_list: Vec<usize>) -> Vec<Line> {let mut local: Vec<Line> = Vec::new();for (i, tokens) in line_list.iter().enumerate() {let indent = indent_list[i];let data = match tokens[0].as_str() {"/" => {LineData::Comment(Comment { content: tokens[1].clone() })}"+" => {LineData::Category(Category { name: tokens[1].clone() })}_ => {continue}};local.push(Line { indent, data });}local
}
fn check_lines(lines: &Vec<Line>) {for line in lines.iter() {let indent_str = " ".repeat(line.indent);match &line.data {LineData::Comment(comment) => {println!("{}Comment: {}", indent_str, comment.content);}LineData::Category(category) => {println!("{}Category: {}", indent_str, category.name);}}}
}enum LineData {Comment(Comment),Category(Category),
}
struct Line {indent: usize,data: LineData,
}

测试结果如下。

Comment: test_line_for_a_comment
Category: test_categoryComment: test_line_for_a_commentComment: test_line_for_a_commentCategory: test_categoryComment: test_line_for_a_commentComment: test_line_for_a_commentComment: test_line_for_a_commentComment: test_line_for_a_commentComment: test_line_for_a_commentCategory: test_categoryComment: test_line_for_a_commentComment: test_line_for_a_commentCategory: test_category
Category: test_categoryComment: test_line_for_a_comment
Category: test_category_with_an_empty_categoryCategory: test_an_empty_category
Comment: test_line_for_a_comment
Comment: test_line_for_a_comment
Comment: test_line_for_a_comment
Comment: test_line_for_a_comment
http://www.dtcms.com/a/273199.html

相关文章:

  • burpsuite记录
  • 深入理解oracle ADG和RAC
  • kotlin中集合的用法
  • kotlin中withContext,async,launch几种异步的区别
  • 【Python练习】035. 编写一个函数,实现简单的文本搜索功能
  • CPU调度调度算法
  • 低功耗小尺寸的空间入侵监测报警设备的市场需求方向
  • 【Python常见问题】【路径】路径总是有问题?深度剖析
  • 【JVM|垃圾回收】第二天
  • Transformer模型原理概述
  • 【Linux】Linux 操作系统 - 27 , 进程间通信(三) --System V 共享内存
  • 零基础入门物联网-远程门禁开关:硬件介绍
  • 多线程学习
  • 指针的const应用
  • 老式MVC架构Web应用:经典框架下的技术坚守与现代挑战
  • STM32F103C8T6驱动无源蜂鸣器详解:从硬件设计到音乐播放
  • 使用SpringAOP自定义权限控制注解
  • 从零开始的语言模型构建 CS336 第一课(一)
  • 【Python练习】036. 编写一个函数,将一个字符串中的所有字符按ASCII值排序
  • 用OpenCV标定相机内参应用示例(C++和Python)
  • Git简单命令
  • 获取印度股票数据API实战指南:NSE与BSE双市场对接
  • 华为OD 周末爬山
  • upload-labs靶场通关详解:第21关 数组绕过
  • 微服务架构下的自动化测试策略调优经验分享
  • 【基于大模型 + FAISS 的本地知识库与智能 PPT 生成系统:从架构到实现】
  • Datawhale AI 夏令营:用户洞察挑战赛 Notebook(2)
  • HVV注意事项(个人总结 非技术)
  • 【HTTP服务端】Cookie?Session?Token?
  • React 自定义Hook——页面或元素滚动到底部监听 Hook