Skip to content

Commit c956119

Browse files
committed
Put menu in a loop rather than quitting after each query
1 parent 65e03db commit c956119

File tree

4 files changed

+78
-62
lines changed

4 files changed

+78
-62
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Language Detector
22

3-
Y3S1 Object Oriented Programming Assignment
3+
Y3S1 Object-Oriented Programming Assignment
44

55
## Description
66

@@ -39,29 +39,29 @@ While the application's accuracy is limited, it is still often able to accuratel
3939
### Japanese
4040

4141
```
42-
$ Enter WiLi data location (or press enter to use the default): datasets/wili-2018-Large-117500-Edited.txt
42+
$ Choose WiLi dataset ('L' for Large, 'S' for Small): S
4343
$ Enter the query text/file: samples/jp.txt
44+
Query file entered.
4445
Processing query...
4546
Building subject database...
4647
Finished processing query.
4748
Finished building subject database.
4849
4950
The text appears to be written in Japanese.
50-
51-
Time: 2.643 (s)
51+
Time: 0.433 (s)
5252
```
5353

5454
### French
5555

5656
```
57-
$ Enter WiLi data location (or press enter to use the default):
57+
$ Choose WiLi dataset ('L' for Large, 'S' for Small): L
5858
$ Enter the query text/file: samples/fr.txt
59+
Query file entered.
5960
Processing query...
6061
Building subject database...
6162
Finished processing query.
6263
Finished building subject database.
6364
6465
The text appears to be written in French.
65-
66-
Time: 35.619 (s)
66+
Time: 34.957 (s)
6767
```

src/ie/gmit/sw/query/QueryFile.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
public class QueryFile implements Query {
1313
private final Path filepath;
14+
private String queryString;
1415

1516
/**
1617
* Constructs a new QueryFile using the given filepath
@@ -30,12 +31,16 @@ public QueryFile(Path filepath) {
3031
*/
3132
@Override
3233
public String getQuery() throws IOException {
33-
String queryString = removeWhitespace(Files.readString(filepath));
34+
if (this.queryString == null) {
35+
String queryString = removeWhitespace(Files.readString(filepath));
3436

35-
if (queryString.length() > MAX_QUERY_LENGTH) {
36-
return queryString.substring(0, MAX_QUERY_LENGTH);
37+
if (queryString.length() > MAX_QUERY_LENGTH) {
38+
this.queryString = queryString.substring(0, MAX_QUERY_LENGTH);
39+
} else {
40+
this.queryString = queryString;
41+
}
3742
}
3843

39-
return queryString;
44+
return this.queryString;
4045
}
4146
}

src/ie/gmit/sw/ui/Menu.java

+52-31
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import java.io.File;
1212
import java.io.FileNotFoundException;
13-
import java.io.IOException;
1413
import java.nio.file.Path;
1514
import java.util.Scanner;
1615
import java.util.concurrent.ExecutionException;
@@ -30,7 +29,8 @@ public class Menu {
3029
private Path dataPath;
3130
private Query query;
3231

33-
private static final String DEFAULT_DATA_PATH = "datasets/wili-2018-Large-117500-Edited.txt";
32+
private static final String LARGE_DATASET = "datasets/wili-2018-Large-117500-Edited.txt";
33+
private static final String SMALL_DATASET = "datasets/wili-2018-Small-11750-Edited.txt";
3434

3535
public Path getDataPath() {
3636
return dataPath;
@@ -65,38 +65,14 @@ public void display() {
6565
System.out.println("=============================================================================");
6666

6767
try (var console = new Scanner(System.in)) {
68-
String input;
69-
7068
while (true) {
7169
try {
7270
if (dataPath == null) {
73-
System.out.print("$ Enter WiLi data location (or press enter to use the default): ");
74-
input = console.nextLine().trim();
75-
76-
if (input.isEmpty()) {
77-
input = DEFAULT_DATA_PATH;
78-
}
79-
80-
if (isFile(input)) {
81-
this.dataPath = Path.of(input);
82-
} else {
83-
throw new FileNotFoundException("The dataset file could not be found");
84-
}
71+
getDatasetInput(console);
8572
}
8673

8774
if (query == null) {
88-
System.out.print("$ Enter the query text/file: ");
89-
input = console.nextLine().trim();
90-
91-
if (input.isEmpty()) {
92-
continue;
93-
}
94-
95-
if (isFile(input)) {
96-
this.query = new QueryFile(Path.of(input));
97-
} else {
98-
this.query = new QueryString(input);
99-
}
75+
getQueryInput(console);
10076
}
10177

10278
break;
@@ -105,8 +81,53 @@ public void display() {
10581
}
10682
}
10783
}
84+
}
85+
86+
private void getDatasetInput(Scanner console) throws FileNotFoundException {
87+
while (true) {
88+
System.out.print("$ Choose WiLi dataset ('L' for Large, 'S' for Small): ");
89+
String input = console.nextLine().toUpperCase().trim();
90+
91+
if (input.equals("L")) {
92+
input = LARGE_DATASET;
93+
} else if (input.equals("S")) {
94+
input = SMALL_DATASET;
95+
} else {
96+
continue;
97+
}
98+
99+
if (isFile(input)) {
100+
this.dataPath = Path.of(input);
101+
break;
102+
} else {
103+
throw new FileNotFoundException("The dataset file could not be found");
104+
}
105+
}
106+
}
108107

109-
detect();
108+
private void getQueryInput(Scanner console) {
109+
while (true) {
110+
System.out.print("$ Enter the query text/file: ");
111+
String input = console.nextLine().trim();
112+
113+
if (input.isEmpty()) {
114+
continue;
115+
}
116+
117+
if (input.equals("-1")) {
118+
break;
119+
}
120+
121+
if (isFile(input)) {
122+
this.query = new QueryFile(Path.of(input));
123+
System.out.println("Query file entered.");
124+
} else {
125+
this.query = new QueryString(input);
126+
System.out.println("Query string entered.");
127+
}
128+
129+
detect();
130+
}
110131
}
111132

112133
private void detect() {
@@ -150,12 +171,12 @@ private void detect() {
150171

151172
try {
152173
Language result = subParser.detect(qParser.getQueryMapping());
153-
System.out.printf("\nThe text appears to be written in %s.\n", result.toString());
174+
System.out.printf("\nThe text appears to be written in %s.", result.language());
154175
} catch (IllegalStateException e) {
155176
System.err.println("\n[Error] Failed to detect the language: " + e.getMessage());
156177
}
157178

158-
System.out.println("\nTime: " + calcDuration(startTime) + " (s)");
179+
System.out.println("\nTime: " + calcDuration(startTime) + " (s)\n");
159180
}
160181

161182
private static boolean isFile(String filename) {

src/ie/gmit/sw/utils/Script.java

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package ie.gmit.sw.utils;
22

3-
import java.util.Map;
3+
import java.util.SortedMap;
44
import java.util.TreeMap;
55

6-
import static java.util.stream.Collectors.*;
76
import static java.lang.Character.*;
87

98
/**
@@ -14,35 +13,26 @@ private Script() {
1413
}
1514

1615
/**
17-
* Attempt to determine which script a given string is most likely written in.
16+
* Determines which script a given string is most likely written in.
1817
* <p>
1918
* This is done by finding the {@link UnicodeScript} of each 1-gram in the text.
2019
* <p>
21-
* Since the text may contain characters in multiple scripts, this method will try to return the closest match.
20+
* Since the text may contain characters in multiple scripts, this method will return the closest match.
2221
*
2322
* @param text The text to process
24-
* @return The {@link UnicodeScript} that was the closest match, or null if there was no match
23+
* @return The {@link UnicodeScript} that was the closest match
2524
*/
2625
public static UnicodeScript of(String text) {
27-
// Find the highest Integer value in the map since that is most likely the script.
28-
// Reference: https://codereview.stackexchange.com/a/153400
29-
return findMatchingScripts(text)
30-
.entrySet()
31-
.stream()
32-
.collect(groupingBy(Map.Entry::getValue, TreeMap::new, toMap(Map.Entry::getKey, Map.Entry::getValue)))
33-
.lastEntry()
34-
.getValue()
35-
.keySet()
36-
.stream()
37-
.findFirst()
38-
.orElse(null);
26+
// Returns the script with the highest value/frequency in the map
27+
return findMatchingScripts(text).lastKey();
3928
}
4029

4130
/**
42-
* Gets a mapping of each {@link UnicodeScript} found in a given String along with its frequency of occurrence.
31+
* Gets a mapping of each {@link UnicodeScript} found in a given String along with its frequency of
32+
* occurrence in ascending order.
4333
*/
44-
private static Map<UnicodeScript, Integer> findMatchingScripts(String text) {
45-
Map<UnicodeScript, Integer> matchingScripts = new TreeMap<>();
34+
private static SortedMap<UnicodeScript, Integer> findMatchingScripts(String text) {
35+
SortedMap<UnicodeScript, Integer> matchingScripts = new TreeMap<>();
4636

4737
text = stripWhitespace(text);
4838

0 commit comments

Comments
 (0)