forked from RainerYuan/Teamex4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordProcessor.java
More file actions
92 lines (87 loc) · 3.93 KB
/
Copy pathWordProcessor.java
File metadata and controls
92 lines (87 loc) · 3.93 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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
/**
* This class contains some utility helper methods
*
* @author sapan (sapan@cs.wisc.edu)
*/
public class WordProcessor {
/**
* Gets a Stream of words from the filepath.
*
* The Stream should only contain trimmed, non-empty and UPPERCASE words.
*
* @see <a href="http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html">java8 stream blog</a>
*
* @param filepath file path to the dictionary file
* @return Stream<String> stream of words read from the filepath
* @throws IOException exception resulting from accessing the filepath
*/
public static Stream<String> getWordStream(String filepath) throws IOException {
/**
* @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html">java.nio.file.Files</a>
* @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/file/Paths.html">java.nio.file.Paths</a>
* @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html">java.nio.file.Path</a>
* @see <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html">java.util.stream.Stream</a>
*
* class Files has a method lines() which accepts an interface Path object and
* produces a Stream<String> object via which one can read all the lines from a file as a Stream.
*
* class Paths has a method get() which accepts one or more strings (filepath),
* joins them if required and produces a interface Path object
*
* Combining these two methods:
* Files.lines(Paths.get(<string filepath>))
* produces
* a Stream of lines read from the filepath
*
* Once this Stream of lines is available, you can use the powerful operations available for Stream objects to combine
* multiple pre-processing operations of each line in a single statement.
*
* Few of these features:
* 1. map( ) [changes a line to the result of the applied function. Mathematically, line = operation(line)]
* - trim all the lines
* - convert all the lines to UpperCase
* - example takes each of the lines one by one and apply the function toString on them as line.toString()
* and returns the Stream:
* streamOfLines = streamOfLines.map(String::toString)
*
* 2. filter( ) [keeps only lines which satisfy the provided condition]
* - can be used to only keep non-empty lines and drop empty lines
* - example below removes all the lines from the Stream which do not equal the string "apple"
* and returns the Stream:
* streamOfLines = streamOfLines.filter(x -> x != "apple");
*
* 3. collect( ) [collects all the lines into a java.util.List object]
* - can be used in the function which will invoke this method to convert Stream<String> of lines to List<String> of lines
* - example below collects all the elements of the Stream into a List and returns the List:
* List<String> listOfLines = streamOfLines.collect(Collectors::toList);
*
* Note: since map and filter return the updated Stream objects, they can chained together as:
* streamOfLines.map(...).filter(a -> ...).map(...) and so on
*/
return null;
}
/**
* Adjacency between word1 and word2 is defined by:
* if the difference between word1 and word2 is of
* 1 char replacement
* 1 char addition
* 1 char deletion
* then
* word1 and word2 are adjacent
* else
* word1 and word2 are not adjacent
*
* Note: if word1 is equal to word2, they are not adjacent
*
* @param word1 first word
* @param word2 second word
* @return true if word1 and word2 are adjacent else false
*/
public static boolean isAdjacent(String word1, String word2) {
return false;
}
}