java英语单词查询,输入一个单词根据字典查询单词意思

时间:2021-07-21 14:11:58

输入一个单词,可以查询单词意思,使用了字典

算法思想:字典文件读取后按单词长度进行了分组,单词查询时也是按照单词长度

选择对应的单词组进行查询,查询效率高

使用单词库:https://wenku.baidu.com/view/503b1318b14e852459fb57b0.html,转换成txt进行读取

读取文件输出遇到的错误总结:

1.读txt文件第一行会乱码;

方案:将txt转换成UTF-8无BOM编码格式,使用超级文本编辑器UltraEdit另存转换

2.eclipse读取中文输出乱码

方案:将eclipse文本格式设置为UTF-8格式,项目project-properties-resource

不足:单词库不足,单词语义解释不全

使用java编程:

package wordQuery;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class WordQuery {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		while(true) {
        System.out.println("Please enter Englishword,If want end please input ending!!!");
        String wordin=sc.nextLine();
        if(wordin.equals("ending!!!")){break;}
        else {
		Map<Integer,List<Word>> dic=readDictionary();
		Word word=findWord(wordin,dic);
		if(word==null) {
			System.out.println("no such word,please input again");
		}
		else
		System.out.println(word);
		}
		}
		sc.close();
	}
	//从txt读入字典并按长度分组
	public static Map<Integer,List<Word>> readDictionary() throws IOException{
	    File file=new File("dic\\EnglishUTF-8noBOM.txt");
    	List<Word> list=new ArrayList<Word>();
    	list=read(file);
    	Map<Integer,List<Word>> wordmap=dividMap(list);
    	return wordmap;
    }
	//从txt读入字典 
	public static List<Word> read(File file) throws IOException{
		 List<Word> list=new ArrayList<Word>();
		 BufferedReader in=new BufferedReader(new FileReader(file));
		 String words;
		 while((words=in.readLine())!=null) {
			 //System.out.println(words);
			 String[] str=words.trim().split(" ");
			 Word wordObject=new Word(str[0],str[1],str[2]);
			 list.add(wordObject);
		 }
		 in.close();
		 return list;
	 }
	//按单词长短进行分组,单词是一个对象包含单词、拼读、词属性、含义
		public static Map<Integer,List<Word>> dividMap(List<Word> possibleWord){
			Map<Integer,List<Word>> wordmap=new TreeMap<Integer,List<Word>>();
			for(int i=0;i<possibleWord.size();i++) {
				Word wordObject =possibleWord.get(i);
				int len=wordObject.getWord().length();
				if(wordmap.get(len)==null) {
					List<Word> lset=new ArrayList<Word>();
					lset.add(wordObject);
					wordmap.put(len, lset);
				}
				else
				{
					List<Word> set=wordmap.get(len);
					set.add(wordObject);
				}
				
			}
			Map<Integer,List<Word>> words=new TreeMap<Integer,List<Word>>();
			for(Map.Entry<Integer, List<Word>> entry:wordmap.entrySet()) {
				Integer length=entry.getKey();
				List<Word> set=entry.getValue();
				List<Word> list=new ArrayList<Word>(set);
				words.put(length, list);
			}
			return words;
		}
		//在单词库里寻找单词读音及含义
		public static Word findWord(String word,Map<Integer,List<Word>>dic) {
			Integer len=word.length();
			List<Word> list=dic.get(len);
			for(Word wordtemp:list) {
				if(word.equalsIgnoreCase(wordtemp.getWord())) {
					return wordtemp;
				}
			}
			return null;
		}

}
单词定义
package wordQuery;

public class Word {

	//单词 单词拼读 单词属性
	private String word;
	private String spell;
	private String properties;
	public Word(String wordin,String spellin,String propertiesin){
		word=wordin;
		spell=spellin;
		properties=propertiesin;
	}
	public String toString() {
		return (word+"  "+spell+"  "+properties);
	}
	public String getWord() {
		return word;
	}
	public String getWordSpell() {
		return spell;
	}
	public String getWordProperties() {
		return properties;
	}
	public String addProperties(String proper) {
		StringBuffer buffer=new StringBuffer(properties);
		buffer.append(proper);
		properties=new String(buffer);
		return properties;
	}

}