⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 predicate.java

📁 本程序利用java语言实现了对英文单词的模拟识别。
💻 JAVA
字号:
/*
 * Created on 2004-9-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package ai.base;
import java.util.*;
import javax.swing.*;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Predicate {
	DblList words;
	Node from;
	Node to;
	
	//ψЗ→∧∨≒ 
	//用一个用户输入的谓词为参数来构造谓词实例
	//将分析并更改变量名字后的容器送交给谓词类的words引用
	public Predicate(String str){
		try{
			Vector lst=WordType.parse(str);
			words=new DblList(lst);
		}
		catch(Exception e){
			//抓取所有可能的异常,包括自定义的4个异常类
			//如果扩展词法检查功能,应在此插入代码
			;
			;
			;
			;
			JOptionPane.showMessageDialog(null,e.getMessage());
		}
	}
	
	//使用此类的方法可以通过这个方法返回的单词容器进行更进一步的分析,
	//已经更名,并且保存了必要的层次和每个单词的类型信息
	public DblList getWords(){
		return words;
	}
	
	//公开的方法:转换
	public void delIfAndImp(){
		delAllIf();
		changeImp();
	}
	
	//转换所有双条件和蕴含联结
	private void changeImp(){
		Node pos=words.Head();
		CodeString cur=null;
		
		int code=-1;
		while(pos.Next()!=words.Tail()){
			pos=pos.Next();
			cur=(CodeString)pos.Data();
			if(cur.getValue().equals("→")){
				code=cur.getLayer();
				cur.setValue("∨");

				doOneImp(pos,code);
			}	
		}
	}
	
	//转换一个发现的蕴含联结
	private void doOneImp(Node _pos,int _code){
		Node pos=_pos;
		while(pos.Prev()!=words.Head()){
			pos=pos.Prev();
			CodeString cur=(CodeString)pos.Data();
			int curCode=cur.getLayer();
			if(curCode<_code) break;
			if(cur.getType()==WordType.PREDICATE){
				CodeString notStr=new CodeString("~",curCode);
				pos=words.insAfter(pos.Prev(),notStr);
			}
			if(cur.getValue().equals("∧"))
				cur.setValue("∨");
			if(cur.getValue().equals("∨"))
				cur.setValue("∧");
		}
	}
	
	//转换所有双条件
	private void delAllIf(){
		for(int i=0;i<getNumIfOnly();i++)
			haveIf();
	}
	
	//转换一个双条件
	private void haveIf(){
		from=null;
		to=null;
		Stack s=new Stack();
		Stack ss=new Stack();
		Vector v=new Vector();
		CodeString imp=null;
		CodeString ands=null;
		
		int code=-1;
		
		Node pos=words.Head();
		while(pos.Next()!=words.Tail()){
			pos=pos.Next();
			CodeString cur=(CodeString)pos.Data();
			if(cur.getValue().equals("≒")){
				code=cur.getLayer();
				
				imp=new CodeString("→",code+1);
				ands=new CodeString("∧",code);
				
				s=getPrev(pos.Prev(),code);
				
				v=getNext(pos.Next(),code);
				setPrev(from,pos,s);
				pos.setData(imp);
				setNext(to,pos,v);
				ss=getPrev(pos.Prev(),code);
				ins(gens(ss,v,imp),to,ands);
				//return true;
			}
		}
		//return false;
	}
	
	//获取双条件的个数
	private int getNumIfOnly(){
		Node pos=words.Head();
		int num=0;
		try{
			while(pos.Next()!=words.Tail()){
				pos=pos.Next();
				CodeString cur=(CodeString)pos.Data();
				if(cur.getValue().equals("≒")){
					num++;
				}
			}
		}
		catch(NullPointerException e){
			JOptionPane.showMessageDialog(null,"无效的空引用!");
		}
		return num;
	}
	
	//获取后面的单词
	private Vector getNext(Node pos,int codeBase){
		Vector v=new Vector();
		Node cur=pos;
		to=words.Tail();
		while(cur!=words.Tail()){
			CodeString str=(CodeString)cur.Data();
			if(str.getLayer()<codeBase){
				to=cur;
				break;
			}
			str.setLayer(str.getLayer()+1);
			v.add(str);
			cur=cur.Next();
		}
		return v;
	}
	
	//获取前面的单词
	private Stack getPrev(Node pos,int codeBase){
		Stack s=new Stack();
		Node cur=pos;
		from=words.Head();
		while(cur!=words.Head()){
			CodeString str=(CodeString)cur.Data();
			if(str.getLayer()<codeBase){
				from=cur;
				break;
			}
			str.setLayer(str.getLayer()+1);
			s.push(str);
			cur=cur.Prev();
		}
		return s;
	}
	
	//设置前面的单词,从from到pos
	private void setPrev(Node _from,Node _pos,Stack _s){
		Node frm=_from;
		while(frm.Next()!=_pos){
			frm=frm.Next();
			frm.setData(_s.pop());
		}
	}
	
	//设置后面的单词,从pos到to
	private void setNext(Node _to,Node _pos,Vector _v){
		Node frm=_pos.Next();
		int i=0;
		while(frm!=_to){
			frm.setData(_v.get(i));
			i++;
			frm=frm.Next();
		}
	}
	
	//生成合取的候补
	private Vector gens(Stack _s,Vector _v,CodeString _imp){
		Vector newV=new Vector();
		newV.addAll(_v);
		newV.add(_imp);
		while(!_s.isEmpty())
			newV.add(_s.pop());
		return newV;
	}
	
	//插入合取
	private void ins(Vector _v,Node _to,CodeString _ands){
		Node froms=words.insAfter(_to.Prev(),_ands);
		for(int i=0;i<_v.size();i++){
			froms=words.insAfter(froms,_v.get(i));
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -