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

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

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

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

使用单词库: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;
	}

}

 
 
 

Java课程设计实训大作业:记事本+简易计算器+聊天系统+日历+中英查询 基础任务一:设计日历软件 根据如下图,综合运用GUI编程、事件处理、Calendar类应用等知识设计一款月历,要求能通过输入(或选择)年月的方式正确显示当前月份的所有日期。 基础任务二:设计中英查询软件 根据Java面向对象程序设计相关理论,及GUI编程、事件处理、数据库编程等技术,设计一个如下图所示的“中英文释义查询”程序。输入英文单词查询数据库将对应中文显示在下框中;输入中文查询数据库将对应英文单词显示在下框中。 提升任务三:设计简易记事本软件 1.使用Java图形界面组件设计记事本软件的界面,参考如图所示。 2.程序代码规范,逻辑正确,能够正常运行。 3.“文件”菜单,包括“新建”、“打开”、“保存”、“另存为”和“退出”等功能。 提升任务四:设计简易计算器软件 1.使用Java图形界面组件设计软件,界面如图所示。 2.软件能够满足基本的“加、减、乘、除”等运算要求。 3.程序代码清晰,语法规范,结构合理,逻辑正确。 进阶任务五:自选主题开发一个应用软件(如在线聊天系统,学籍管理系统等)下面给的软件界面只是参考,同学们可以根据自己的想法进行设计。 1.软件界面美观、功能完善软件,导航清晰,操作方便,使用菜单栏、工具栏、布局管理器、按钮、表格等多种Java图形界面组件。 2.程序代码清晰,语法规范,结构合理,逻辑正确。 3.功能完善,程序代码优化,执行效率高,具有较好可维护性和可扩展性。 4.软件功能设计具有一定的难度和创意。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值