How to parse OR AND within text
假设你有一行 String condition = "A or B and C";
语句,请问怎么做才能变成一行真正的逻辑表达式(能在计算机中运行计算)?
Resolution
- 声明一个
List<List<String>>
结构; - 先分割 or ;
变成 [ A, B and C ] - 不包含and的,插入
List<List<String>>
结构;
List<List<String>>
.add( [A] ) - 声明一个
List<String>
, 再分割 and;
List<String>
.add(B);
List<String>
.add(C); - 把④加入
List<List<String>>
结构,
List<List<String>>
.add( [B, C]); - 最终
List<List<String>>
结构如下:
[ [A], [B,C] ] - 这个
List<List<String>>
结构里面的条件语句就是任意一行必须为真语句,简而言之:判断A是不是为真,A为真则整个结构都为真, 或者判断[B, C]是否都为真,如果都为真则整个结构都为真。以此类推。
Example 2
如果是从文本里一行一行的读取用户的自定义配置,并且每行后面是一些特殊的Payload,就有可能会混着OR 、AND 语句,那就不适合使用上面的分割法,容易将Payload也分割了。
NO, IamSentence A
OR, IamSentence B
AN, IamSentence C
先固定Text的逻辑关键词的长度,NO表示第一行,OR=or, AN=and。
思路就是:
- 先将所有语句都并列成一句来分析,
NO A OR B AN C
=A OR (B AN C)
,就能看出整体性的逻辑; - 循环所有语句;
- 先将第一行的NO 语句存储起来;
- Next 无非就是
AN
或OR
两种情况,针对这两个条件分别做不同的处理即可;
Java代码实现该算法:
public static void main(String[] args) {
List<String> rawSentence = new ArrayList<String>();
rawSentence.add("NO, IamSentence A");
rawSentence.add("OR, IamSentence B");
rawSentence.add("AN, IamSentence C");
parseAnOr(rawSentence);
}
public static List<List<String>> parseAnOr(List<String> rawSentence) {
List<List<String>> allList = new ArrayList<>();
String temp = "";
String last = "";
ArrayList<String> tempList = new ArrayList<String>();
for (int i = 0; i < rawSentence.size(); i++) {
if (rawSentence.get(i).substring(0, 2).equals("NO")) {
last = rawSentence.get(i).substring(3);
last = last.trim();
}
if (rawSentence.get(i).substring(0, 2).equals("OR")) {
if (!last.equals("")) {
tempList.add(last);
last = "";
allList.add(new ArrayList<>(tempList));
tempList.clear();
}
if (tempList.size() > 0) {
allList.add(new ArrayList<>(tempList));
tempList.clear();
}
//
last = rawSentence.get(i).substring(3);
last = last.trim();
tempList.clear();
}
if (rawSentence.get(i).substring(0, 2).equals("AN")) {
tempList.add(last);
last = "";
last = rawSentence.get(i).substring(3);
last = last.trim();
}
}
if (!last.equals("")) {
tempList.add(last);
allList.add(new ArrayList<>(tempList));
}
System.out.println(allList);
return allList;
}
out
[[IamSentence A], [IamSentence B, IamSentence C]]
Practice
If it were A or B and C and D or E
, what would you do?
How to parse OR AND within text的更多相关文章
- 使用 JSON.parse 反序列化 ISO 格式的日期字符串, 将返回Date格式对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- go标准库的学习-text/template
参考:https://studygolang.com/pkgdoc 导入方式: import "text/template" template包实现了数据驱动的用于生成文本输出的模 ...
- How to return plain text from AWS Lambda & API Gateway
With limited experience in AWS Lambda & API Gateway, it's struggling to find the correct way to ...
- JSON对象和JSON字符串以及JSON.parse 函数的使用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JSON.parse 的用法,在js中用的。也是反序列化用法。
参数 text 必需. 一个有效的 JSON 字符串. reviver 可选. 一个转换结果的函数. 将为对象的每个成员调用此函数. 如果成员包含嵌套对象,则先于父对象转换嵌套对象. 对于每个成员,会 ...
- 从 Newtonsoft.Json 迁移到 System.Text.Json
一.写在前面 System.Text.Json 是 .NET Core 3 及以上版本内置的 Json 序列化组件,刚推出的时候经常看到踩各种坑的吐槽,现在经过几个版本的迭代优化,提升了易用性,修复了 ...
- C# ListView点击列头进行排序
/// <summary> /// This class is an implementation of the 'IComparer' interface. /// </summa ...
- J2EE项目开发中常用到的公共方法
在项目IDCM中涉及到多种工单,包括有:服务器|网络设备上下架工单.服务器|网络设备重启工单.服务器光纤网线更换工单.网络设备撤线布线工单.服务器|网络设备替换工单.服务器|网络设备RMA工单.通用原 ...
- Java 中类型转换
int -> String int i=12345; String s=""; 第一种方法:s=i+""; 第二种方法:s=String.valueOf( ...
- C#算法知识点记录
针对算法的知识点进行记录 简易桶排序 首先看一个简易桶排序,有一串数字,进行从大到小排列.数字间隔不大,使用一维数组来当作桶,进行插入排序. static void Main(string[] arg ...
随机推荐
- 旧版Vue配置API_ROOT,开发、生产地址切换
1 目录 config/dev.env.js1 'use strict' 2 const merge = require('webpack-merge') 3 const prodEnv = requ ...
- Android-NDK开发——基本概念
在Android开发中,有时候出于安全,性能,代码共用的考虑,需要使用C/C++编写的库.虽然在现代化工具链的支持下,这个工作的难度已经大大降低,但是毕竟万事开头难,初学者往往还是会遇到很多不可预测的 ...
- 认识Dubbo与RPC
关注王有志,分享硬核Java技术的互金摸鱼侠 加入Java人的提桶跑路群:共同富裕的Java人 开个新坑,和大家一起学习Dubbo 3.X.我们按照一个由浅入深顺序来学习,先从使用Dubbo开始,再深 ...
- iota简介
当声明枚举类型或定义一组相关常量时,Go语言中的iota关键字可以帮助我们简化代码并自动生成递增的值.本文档将详细介绍iota的用法和行为. iota关键字 iota是Go语言中的一个预定义标识符,它 ...
- LaTeX 的学习笔记
摘自我的洛谷博客 该文章被打开的次数(包括洛谷平台): \(\LaTeX\) 中所有命令都以\开头,后面可以跟一个花括号,代表参数. \documentclass{} 指定了文章类型,有 articl ...
- IP 地址斜杠后的数字和子网掩码
目录 先上结论 IP 地址类型 ABC 类地址的划分 网络地址与广播地址 网络地址 广播地址 0.0.0.0 与 127.0.0.1 子网掩码 ABC 类 IP 地址最大网络范围与最大可用主机数 以 ...
- modulemap的使用方法
modulemap的作用 modulemap 文件是用来解决 C,Object-C,C++ 代码在 Swift 项目中集成的问题的. 在 Swift 项目中,如果需要使用 C,Object-C 或 ...
- Unity UGUI的PhysicsRaycaster (物理射线检测)组件的介绍及使用
Unity UGUI的PhysicsRaycaster (物理射线检测)组件的介绍及使用 1. 什么是PhysicsRaycaster组件? PhysicsRaycaster是Unity UGUI中的 ...
- CSS:使用透明色
使用如下代码: background-color="#00000000"
- django执行makemigrations报AttributeError: 'str' object has no attribute 'decode'
顺着报错文件点进去,找到query = query.decode(errors='replace')将decode修改为encode即可.