自然语言处理之判断句子合法性的Chart-Parsing算法

package nlp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Map.Entry;

class ChartLine{
	public String wordAttr;
	public int start;
	public int end;
	
	public ChartLine(String wordAttr,int start,int end){
		this.wordAttr=wordAttr;
		this.start=start;
		this.end=end;
	}
	
	public ChartLine(AgendaELement agEl){
		this.wordAttr=agEl.wordAttr;
		this.start=agEl.start;
		this.end=agEl.end;
	}
}
class ActivityLine{
	public String key;
	public String value;
	public int start;
	public int end;
	public int posStart;
	public int posEnd;
	
	public ActivityLine(String key,String value,int start,int end,int posStart,int posEnd){
		this.key=key;
		this.value=value;
		this.start=start;
		this.end=end;
		this.posStart=posStart;
		this.posEnd=posEnd;
	}
}
class AgendaELement{
	public String wordAttr;
	public int start;
	public int end;
	
	public AgendaELement(String wordAttr,int start,int end){
		this.wordAttr=wordAttr;
		this.start=start;
		this.end=end;
	}
}
public class Proj3 {
	
	public HashMap<String,String[]> grammers=new HashMap<String,String[]>();
	public HashMap<String,String[]> wordsAttrs=new HashMap<String,String[]>();
	public ArrayList<ChartLine> chart=new ArrayList<ChartLine>();
	public ArrayList<ActivityLine> activities=new ArrayList<ActivityLine>();
	public ArrayList<AgendaELement> agenda=new ArrayList<AgendaELement>();
	
	public Proj3(){
		grammers.put("S",new String[]{"NP,VP"});
		grammers.put("NP",new String[]{"ART,N","ART,ADJ,N"});
		grammers.put("VP",new String[]{"V","V,NP"});
		wordsAttrs.put("ART",new String[]{"The","a"});
		wordsAttrs.put("N",new String[]{"cat","mouse","dog"});
		wordsAttrs.put("V",new String[]{"caught","eat","walk"});	
	} 
	
	public void handle(String sentence){
		String[]words=sentence.split(" ");
		int index=0;
		while(index<words.length){
			String word=words[index];
			Iterator<Entry<String,String[]>> ite=wordsAttrs.entrySet().iterator();
			String attr="";
			while(ite.hasNext()){
				Entry<String,String[]> entry=ite.next();
				String key=entry.getKey();
				String []valueTemp=entry.getValue();
				for(String value:valueTemp){
					if(value.equals(word))
						attr=key;
				}
			}
			AgendaELement agEl=new AgendaELement(attr,index+1,index+2);
			agenda.add(agEl);
			while(agenda.size()>0){
				AgendaELement ele=agenda.remove(0);
				ChartLine line=new ChartLine(ele);
				//添加图标中
				chart.add(line);
				Iterator<Entry<String,String[]>> gIte=grammers.entrySet().iterator();
				while(gIte.hasNext()){
					Entry<String,String[]> entry=gIte.next();
					String gKey=entry.getKey();
					String[]gValue=entry.getValue();
					for(String g:gValue){
						if(g.startsWith(ele.wordAttr)){
							String[]ss=g.split(",");
							if(ss.length>1&&ss[0].equals(ele.wordAttr)){
								ActivityLine aLine=new ActivityLine(gKey,g,ele.start,ele.end,1,2);
								//添加活动边
								activities.add(aLine);
							}else if(ss.length==1){
								agenda.add(new AgendaELement(gKey,ele.start,ele.end));
							}
						}
					}
				}
				//查找活动边
				for(int i=0;i<activities.size();i++){
					ActivityLine aLine2=activities.get(i);
					String graKey=aLine2.key;
					String gra=aLine2.value;
					String[]ws=gra.split(",");
					if(ele.start==aLine2.end&&ele.wordAttr.equals(ws[aLine2.posStart])){
						if(aLine2.posStart!=ws.length-1){
							ActivityLine aLine3=new ActivityLine(graKey,gra,aLine2.start,ele.end,aLine2.posStart+1,aLine2.end+1);
							activities.add(aLine3);
						}else{
							agenda.add(new AgendaELement(graKey,aLine2.start,ele.end));
						}
					}
				}
			}
			index++;
		}
	}
	
	public static void main(String args[]) throws Exception{
		Proj3 p=new Proj3();
		while(true){
			BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));   
			String str=buffer.readLine();
			p.handle(str);
			ArrayList<ChartLine> arr=p.chart;
			boolean flag=false;
			for(int i=0;i<arr.size();i++){
				ChartLine line=arr.get(i);
				String[]words=str.split(" ");
				if(line.start==1&&line.end==words.length+1&&line.wordAttr=="S")
					flag=true;
			}
			if(flag)
				System.out.println("这个句子是合法的");
			else
				System.out.println("这个句子是不合法的");
		}
		
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自然语言处理(Natural Language Processing,NLP)是一门研究如何使计算机能够理解和处理人类语言的学科。基于规则的方法是NLP中的一种常见方法,它使用预定义的规则和模式来处理文本。 基于规则的自然语言处理算法通常包括以下几个步骤: 1. 分词(Tokenization):将文本分割成单词或子词的序列。这是NLP任务的第一步,目的是将连续的文本划分为有意义的单元。 2. 词性标注(Part-of-Speech Tagging):为每个单词标注其词性,例如名词、动词、形容词等。这有助于理解句子中各个单词的语法角色。 3. 句法分析(Syntactic Parsing):分析句子的结构和语法关系,例如主谓宾关系、修饰关系等。句法分析可以帮助理解句子的语法结构。 4. 语义分析(Semantic Analysis):理解句子的意义和语义关系。这包括词义消歧、指代消解、语义角色标注等任务。 5. 命名实体识别(Named Entity Recognition):识别文本中的命名实体,如人名、地名、组织机构等。这有助于理解文本中的重要实体信息。 6. 关系抽取(Relation Extraction):从文本中提取出实体之间的关系。例如,从新闻报道中提取出人物之间的关系。 7. 文本分类(Text Classification):将文本划分到不同的类别或标签中。例如,将电子邮件分类为垃圾邮件或非垃圾邮件。 8. 信息抽取(Information Extraction):从文本中提取出结构化的信息。例如,从新闻文章中提取出事件、时间、地点等信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值