同义词词林图转树

HIT同义词此林的上下位词关系不是树状结构,从图转到树型结构

算法:

1.察看word是否有下为词,得到下位词childlist

2.若第一步childlist为空,在entity表中查找该概念对应的实体,否则对于childlist中的每个word若之前没访问过,执行第一步

3.结果合并生成树型结构


缺点:

1.忽略了此的层次关系

2.对于结构较大的概念,效率较慢


package cn.dataprocess;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
public class WordTree {
	public static Map<String,String> iMap = new HashMap<String,String>();
	
	public static List<String> findDown(String word) throws ClassNotFoundException, SQLException{
		String driver = "com.mysql.jdbc.Driver";

        String url = "jdbc:mysql://localhost:3306/maya";


        String user = "root"; 


        String password = "root";
        Class.forName(driver);


        Connection conn = DriverManager.getConnection(url,user,password);
        
        //if(!conn.isClosed()) System.out.println("Succeeded connecting to the Database!");


        Statement statement = conn.createStatement();
        
        String sql = "select * from hpy_hpy where parent = '" + word + "'";
        
        ResultSet rs = statement.executeQuery(sql);
        
        List<String> res = new ArrayList<String>();
        
        while(rs.next()){
        	res.add(rs.getString("child"));
        }
        return res;
	}
	public static List<String> findEntity(String word) throws ClassNotFoundException, SQLException{
		String driver = "com.mysql.jdbc.Driver";

        String url = "jdbc:mysql://localhost:3306/maya";


        String user = "root"; 


        String password = "root";
        Class.forName(driver);


        Connection conn = DriverManager.getConnection(url,user,password);
        
        //if(!conn.isClosed()) System.out.println("Succeeded connecting to the Database!");


        Statement statement = conn.createStatement();
        
        String sql = "select * from ent_hpy where hpy = '" + word + "'";
        
        ResultSet rs = statement.executeQuery(sql);
        
        List<String> res = new ArrayList<String>();
        
        while(rs.next()){
        	res.add(rs.getString("ent"));
        }
        return res;
	}
	public static JSONObject solve(String word) throws ClassNotFoundException, SQLException, JSONException{
		if(iMap.containsKey(word))return null;
		iMap.put(word, "has");
		//System.out.println("word:" + word);
		JSONObject res = new JSONObject();
		List<String> word_down = findDown(word);
		
		if(word_down.size() == 0){
			List<String> ent_down = findEntity(word);
			//res.add(ent_down);
			//return ;
			
			JSONArray tem = new JSONArray();
			for(int i = 0;i < ent_down.size();i ++){
				tem.put(i,ent_down.get(i));
			}
			res.put(word, tem);
			return res;
		}
		JSONArray tem = new JSONArray();
		for(String key : word_down){
			//System.out.println("key:" + key);
			tem.put(solve(key));
			//System.out.println("tem:" + tem.toString());
			
		}
		res.put(word,tem);
		return res;
	}
	
	public static JSONObject getTree(String tem) throws IOException, ClassNotFoundException, SQLException, JSONException {
		// TODO Auto-generated method stub
		//List<List<String>> res = new ArrayList<List<String>>();
        
        
        
        	iMap.clear();
        	JSONObject res = solve(tem);
        	
        	return res;
        
        
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值