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

📄 test.java

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

/**
 * @author 赵秀成
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TEST extends JFrame {
	/*
	 * 这是测试文件,基本不用看,构造界面的代码^-^
	 */
	private JLabel inLabel=new JLabel("输入一个谓词语句:");
	private JLabel outLabel=new JLabel("词法分析后的单词:");
	
	//谓词输入文本区域,词法分析的输出区域
	private JTextArea output=new JTextArea();
	private JTextField input=new JTextField();
	
	//为了插入量词和联结词而设置的几个按钮
	private JButton and=new JButton("∧");//插入合取
	private JButton or=new JButton("∨");//插入析取
	private JButton ifonly=new JButton("≒");//插入双条件
	private JButton any=new JButton("ψ");//插入任意量词
	private JButton exist=new JButton("З");//插入存在量词
	private JButton imp=new JButton("→");//插入蕴含
	private JButton not=new JButton("~");//插入取反
	
	private JButton showWords=new JButton("显示词法分析结果");
	
	public TEST(){
		super("zxc's 词法分析器V1.0");
		
		//showWords按钮实现从谓词类提取分析后的结果的功能
		showWords.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				
				output.setText("");
				output.repaint();
				/*
				 * 谓词构造函数同时检查输入的谓词是否合乎语法要求:
				 * 检查是否以英文句号结尾
				 * 检查括号是否配对
				 * 检查字符串是否严格封装在[]对中
				 * 检查是否包含字符集以外的字符(谓词字符集见WordType类的定义)
				 *上面没有列出的语法错误不能检查出来,所以尽量按照语法格式书写谓词
				 *******扩展:以后会进一步增加检查范围
				 */
				
				Predicate p=new Predicate(input.getText());
				
				/*ψЗ→∧∨
				 * 谓词类的方法delIfAndImp将检查通过的谓词语句进行以下处理:
				 * 首先将双条件联结词转化为蕴含联结词,这通过:A≒ B==(A→B)∧(B→A)实现
				 * 将蕴含联结词使用假言推理去除,这通过:A→B==~A∨B实现
				 */
				p.delIfAndImp();
				//p.haveIf();
				
				/*
				 * DblList为自定义双向链表,用来存放谓词分析后的各种单词,
				 * 单词均统一为自定义的CodeString类
				 */
				DblList lst=p.getWords();
				
				//遍历双向链表,显示其内容于output文本区域中
				Node pos=lst.Head();
				while(pos.Next()!=lst.Tail()){
					pos=pos.Next();
					CodeString cur=(CodeString)pos.Data();
					output.append(cur.toString());
				}
			}
			});
		
		/*
		 * 以下7个按钮的事件监听器实现向输入区域插入联结词和量词的功能
		 */
		and.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int pos=input.getCaretPosition();
				String str1=input.getText().substring(0,pos);
				String str2=input.getText().substring(pos);
				input.setText(str1+and.getText()+str2);
				input.setCaretPosition(pos+1);
				input.grabFocus();
			}
			});
		or.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int pos=input.getCaretPosition();
				String str1=input.getText().substring(0,pos);
				String str2=input.getText().substring(pos);
				input.setText(str1+or.getText()+str2);
				input.setCaretPosition(pos+1);
				input.grabFocus();
			}
			});
		not.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int pos=input.getCaretPosition();
				String str1=input.getText().substring(0,pos);
				String str2=input.getText().substring(pos);
				input.setText(str1+not.getText()+str2);
				input.setCaretPosition(pos+1);
				input.grabFocus();
			}
			});
		imp.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int pos=input.getCaretPosition();
				String str1=input.getText().substring(0,pos);
				String str2=input.getText().substring(pos);
				input.setText(str1+imp.getText()+str2);
				input.setCaretPosition(pos+1);
				input.grabFocus();
			}
			});
		ifonly.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int pos=input.getCaretPosition();
				String str1=input.getText().substring(0,pos);
				String str2=input.getText().substring(pos);
				input.setText(str1+ifonly.getText()+str2);
				input.setCaretPosition(pos+1);
				input.grabFocus();
			}
			});
		any.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int pos=input.getCaretPosition();
				String str1=input.getText().substring(0,pos);
				String str2=input.getText().substring(pos);
				input.setText(str1+any.getText()+str2);
				input.setCaretPosition(pos+1);
				input.grabFocus();
			}
			});
		exist.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int pos=input.getCaretPosition();
				String str1=input.getText().substring(0,pos);
				String str2=input.getText().substring(pos);
				input.setText(str1+exist.getText()+str2);
				input.setCaretPosition(pos+1);
				input.grabFocus();
			}
			});
		
		//输入文本区域的事件监听器,用来提示谓词已装入
		input.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				JOptionPane.showMessageDialog(TEST.this,"谓词语句:\n"+input.getText()+"\n已经输入词法分析器");
			}
			});
		
		/*
		 * 以下代码用来设置用户界面,启动程序运行
		 */
		Font fnt=new Font("楷体",Font.PLAIN,14);
		Font fnt1=new Font("roman",Font.BOLD,16);
		
		inLabel.setFont(fnt);
		outLabel.setFont(fnt);
		output.setFont(fnt);
		input.setFont(fnt1);
		
		showWords.setFont(fnt);
		and.setFont(fnt1);
		or.setFont(fnt1);
		ifonly.setFont(fnt1);
		any.setFont(fnt1);
		exist.setFont(fnt1);
		imp.setFont(fnt1);
		not.setFont(fnt1);
		
		input.setForeground(Color.RED);
		output.setForeground(Color.WHITE);
		output.setBackground(Color.BLUE);
		
		Container pane=getContentPane();
		JPanel north=new JPanel(new BorderLayout());
		JPanel center=new JPanel(new BorderLayout());
		JPanel south=new JPanel();
		
		JPanel btnPanel=new JPanel();
		north.setBackground(Color.YELLOW);
		center.setBackground(Color.CYAN);
		south.setBackground(Color.PINK);
		
		btnPanel.add(and);
		btnPanel.add(or);
		btnPanel.add(not);
		btnPanel.add(imp);
		btnPanel.add(ifonly);
		btnPanel.add(any);
		btnPanel.add(exist);
		btnPanel.setBackground(Color.GREEN);
		
		north.add(inLabel,BorderLayout.NORTH);
		north.add(input,BorderLayout.CENTER);
		north.add(btnPanel,BorderLayout.SOUTH);
		center.add(outLabel,BorderLayout.NORTH);
		center.add(new JScrollPane(output),BorderLayout.CENTER);
		south.add(showWords);
		
		pane.add(north,BorderLayout.NORTH);
		pane.add(center,BorderLayout.CENTER);
		pane.add(south,BorderLayout.SOUTH);
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(640,480);
		setVisible(true);
		
	}
	
	public void main(String[] args){
		TEST aa = new TEST();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		aa.show();
	}
	
}

⌨️ 快捷键说明

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