//过了个年,学的忘了好多,准备从数据结构开始复习,复习了TreeMap时就写了个小程序,是一个简单的辞典,就是输出同//义词的
//给定辞典文件和键盘输入的单词,找出其近义词
package TreeMAP;
public class GUI {
public GUI(ThesaurusDrivers td){}
public void println(String str){
System.out.println(str) ;
}
}
*
* 一个简单的辞典
*/
package TreeMAP;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Thesaurus {
private TreeMap thesaurusMap ;
public Thesaurus(){
thesaurusMap = new TreeMap() ;
}
public void add(String line){
LinkedList synonymList = new LinkedList() ;
StringTokenizer st = new StringTokenizer(line) ;
String word = st.nextToken() ;
while(st.hasMoreTokens())
synonymList.add(st.nextToken()) ;
thesaurusMap.put(word, synonymList) ;
}
public LinkedList getSynonyms(String word){
return (LinkedList)thesaurusMap.get(word) ;
}
}
package TreeMAP;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
public class ThesaurusDrivers {
Thesaurus thesaurus ;
GUI gui ;
boolean readingInFileName ;
public ThesaurusDrivers(){
final String IN_FILE_PROMPT = "Please enter the path for the input file." ;
gui = new GUI(this) ;
thesaurus = new Thesaurus() ;
readingInFileName = true ;
}
public void processInput(String str){
final String WORD_PROMPT = "\n\nPlease enter a word. The sentinel is " ;
final String SENTINEL = "***" ;
final String WORD_NOT_FOUND_MESSAGE ="That word does not appear in hte thesaurus." ;
final String SYNONYM_MESSAGE = "The synonyms of that word are " ;
final String CLOSE_WINDOW_MESSAGE = "\n\nThe execution of the project is complete. Please close " + "this window when you are ready." ;
LinkedList synonymList ;
if(readingInFileName){
if(thesaurusWasConstructed(str)){
readingInFileName = false ;
gui.println(WORD_PROMPT + SENTINEL) ;
}
}
else if(!str.equals(SENTINEL)){
synonymList = (LinkedList)thesaurus.getSynonyms(str) ;
if(synonymList == null)
gui.println(WORD_NOT_FOUND_MESSAGE) ;
else{
gui.println(SYNONYM_MESSAGE + synonymList) ;
gui.println(WORD_PROMPT + SENTINEL) ;
}
}
else{
gui.println(CLOSE_WINDOW_MESSAGE) ;
}
}
protected boolean thesaurusWasConstructed (String inFileName){
final String NO_INPUT_FILE_FOUND_MESSAGE = "Error: there is no file with that path. \n\n" ;
final String ERROR_MESSAGE = "Exception:" ;
final String IN_FILE_PROMPT = "Please enter the path for the input file." ;
BufferedReader inFile ;
String line ;
boolean success = false ;
try{
inFile = new BufferedReader(new FileReader(inFileName)) ;
success = true ;
while((line = inFile.readLine()) != null)
thesaurus.add(line) ;
inFile.close() ;
}catch(FileNotFoundException e){
gui.println(NO_INPUT_FILE_FOUND_MESSAGE + IN_FILE_PROMPT) ;
}catch(IOException e){
gui.println(ERROR_MESSAGE + e) ;
}
return success ;
}
}
package TreeMAP;
public class Test_Thesaurus {
public static void main(String[] args) {
// TODO Auto-generated method stub
ThesaurusDrivers td = new ThesaurusDrivers() ;
td.processInput("thesaurus.in") ;
}
}
/***/
/***thesaurus.in:
close near confined
confined cramped
correct true
cramped confined
near close
one singular unique
singular one unique
true correct
unique singular one
***/