Repository navigation
Expand file tree
/
Copy pathWordFinder.java
More file actions
85 lines (75 loc) · 2.34 KB
/
Copy pathWordFinder.java
File metadata and controls
85 lines (75 loc) · 2.34 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
import java.io.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileSystem;
public class WordFinder {
public static String WordFilePath = "";
public static String DictionaryFilePath = "";
public WordFinder(String wordFilePath, String dictionaryPath) {
WordFilePath = wordFilePath;
DictionaryFilePath = dictionaryPath;
}
public String runWordFinder(String inputWord, FileSystem fs, Path pt) throws IOException {
// Check if input word is in word file
if (!verifyWord(inputWord, fs, pt)) {
return "Word is not found.";
} else {
// Find the meaning of the word
return findWordMeaning(inputWord, fs, pt);
}
}
public boolean verifyWord(String word, FileSystem fs, Path pt) throws IOException {
boolean result = false;
BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(pt)));
String line;
while ((line = br.readLine()) != null) {
if (line.toUpperCase().equals(word)) {
result = true;
break;
}
}
br.close();
//fs.close();
return result;
}
public String findWordMeaning(String word, FileSystem fs, Path pt) throws IOException {
// Read the dictionary file
BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(pt)));
String wordMeaning = "";
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;
}
}
br.close();
//fs.close();
return wordMeaning;
}
private 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 uppercase then
// next word is hit, so process it false.
if (splitLine.length == 1 && line.matches("[A-Z]+") && !line.equals(inputWord))
return false;
return true;
}
public boolean addWord(String inputWord) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(WordFilePath, true));
writer.write("\n");
writer.write(inputWord);
writer.close();
return true;
} catch (IOException ioe) {
System.err.format("IOException: %s%n", ioe);
return false;
}
}
}