java英语单词查询,输入一个单词根据字典查询单词意思
2018-01-30 16:31
465 查看
输入一个单词,可以查询单词意思,使用了字典
算法思想:字典文件读取后按单词长度进行了分组,单词查询时也是按照单词长度
选择对应的单词组进行查询,查询效率高
使用单词库:https://wenku.baidu.com/view/503b1318b14e852459fb57b0.html,转换成txt进行读取
读取文件输出遇到的错误总结:
1.读txt文件第一行会乱码;
方案:将txt转换成UTF-8无BOM编码格式,使用超级文本编辑器UltraEdit另存转换
2.eclipse读取中文输出乱码
方案:将eclipse文本格式设置为UTF-8格式,项目project-properties-resource
不足:单词库不足,单词语义解释不全
使用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; } }
相关文章推荐
- 给你一个单词a,如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的兄弟单词。现在给你一个字典,用户输入一个单词,让你根据字典找出这个单词有多少个兄弟单词。
- [转]给你一个单词a,如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的兄弟单词。现在给你一个字典,用户输入一个单词,让你根据字典找出这个单词有多少个兄弟单词
- 用户输入一个单词,要求在一个字典中找出该单词的所有兄弟单词,并输出
- 把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”
- 把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”
- java 输入一个整数n,根据n获取Fibonacci数列的第n个值
- linux下结合sqlite3做一个英语查询字典小项目 <1>
- 编写一个程序,输入一串不超过60个字符组成的且不包含换行符的字符串,将字符串中的A到Z的字母按英文字典字母顺序重新排列,排列后的单词的长度要与原始句子中的长度相同,并且要求只对A到Z的字母重新排列,其
- java中根据是否输入日期进行动态查询的sql查询语句
- 练习 6-4 编写一个程序,根据单词的出现频率按降序打印输入的各个不同单词,并在 每个单词的前面标上它的出现次数
- 3.3 从标准输入读入一个数,把它转为英语单词输出,如读入 234,输出“two three four”。
- Java,Mysql-根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法
- java实现至少输入一个条件的数据库查询
- Java进阶(五十)根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法
- 把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”;
- JAVA文本框根据输入内容自动模糊查询(动态)
- 编写一个程序,从标准输入中读取若干string对象并查找连续重复出现的单词。所谓连续重复出现的意思是:一个单词后面紧跟着这个单词本身。要求记录连续重复出现的最大次数以及对应的单词
- Java,Mysql-根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法
- Java,Mysql-根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法
- 把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”