-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordFinder.java
More file actions
93 lines (84 loc) · 2.89 KB
/
WordFinder.java
File metadata and controls
93 lines (84 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.io.*;
public class WordFinder {
public static String WordFilePath = "";
public static String DictionaryFilePath = "";
public void runWordFinder(String wordFilePath, String dictionaryPath, String inputFilePath, String outputFilePath) throws IOException{
WordFilePath = wordFilePath;
DictionaryFilePath = dictionaryPath;
File outputFile = new File(outputFilePath);
if(outputFile.exists())
outputFile.delete();
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath, true));
//Read input line by line and find its meaning and write it in output file.
try (BufferedReader br = new BufferedReader(new FileReader(inputFilePath))) {
String line;
while ((line = br.readLine()) != null) {
//Find the meaning
String meaning = begin(line.trim().toUpperCase());
//Write in file
try {
writer.write(line + " " + meaning);
writer.write("\n");
} catch (IOException ioe) {
System.err.format("IOException: %s%n", ioe);
}
}
} catch(Exception ex){
System.out.println("Error Occurred : " + ex.getMessage());
}
writer.close();
}
private static String begin(String inputWord) throws IOException {
//Check if input word is in word file
if(!verifyWord(inputWord)){
return "Word is not found.";
} else {
//Find the meaning of the word
return findWordMeaning(inputWord);
}
}
private static boolean verifyWord(String word){
boolean result = false;
try (BufferedReader br = new BufferedReader(new FileReader(WordFilePath))) {
String line;
while ((line = br.readLine()) != null) {
if(line.toUpperCase().equals(word)){
result = true;
break;
}
}
} catch(Exception ex){
System.out.println("Error Occurred : " + ex.getMessage());
}
return result;
}
private static String findWordMeaning(String word) throws IOException{
//Read the dictionary file
String wordMeaning = "";
try (BufferedReader br = new BufferedReader(new FileReader(DictionaryFilePath))) {
String line;
while ((line = br.readLine()) != null) {
if(line.equals(word)){
//Until next word is hit print all the line
while((line = br.readLine()) != null){
if(!processLine(line, word))
break;
wordMeaning += " " +line;
}
break;
}
}
} catch(Exception ex){
System.out.println("Error Occurred : " + ex.getMessage());
}
return wordMeaning;
}
private static boolean processLine(String line, String inputWord){
//Check if line has only single word
String[] splitLine = line.split(" ");
//If line's length is 1 that is single word and if it is upper case then next word is hit, so process it false.
if(splitLine.length == 1 && line.matches("[A-Z]+") && !line.equals(inputWord))
return false;
return true;
}
}