Skip to content

Commit d05111b

Browse files
committed
parse text as well as files
1 parent 5d37b49 commit d05111b

10 files changed

+141
-79
lines changed

src/ie/gmit/sw/Runner.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public static void main(String[] args) throws InterruptedException {
1818

1919
double startTime = System.currentTimeMillis();
2020

21-
QueryParser qParser = new QueryParser(menu.getQueryFileLoc(), 1, 2, 3, 4);
22-
SubjectParser dsParser = new SubjectParser(menu.getDataLoc(), 1, 2, 3, 4);
21+
QueryParser qParser = new QueryParser(menu.getQuery(), 1, 2, 3, 4);
22+
SubjectParser dsParser = new SubjectParser(menu.getDataPath(), 1, 2, 3, 4);
2323

2424
ExecutorService ex = Executors.newFixedThreadPool(2);
2525

@@ -35,7 +35,7 @@ public static void main(String[] args) throws InterruptedException {
3535
System.out.println("Finished processing query.");
3636
} catch (ExecutionException e) {
3737
if (e.getCause() instanceof IOException) {
38-
System.err.println("[Error] There was an issue with reading: "+ menu.getQueryFileLoc());
38+
System.err.println("[Error] There was an issue with reading: "+ menu.getQuery());
3939
} else {
4040
System.err.println(e.getMessage());
4141
}
@@ -47,7 +47,7 @@ public static void main(String[] args) throws InterruptedException {
4747
System.out.println("Finished building subject database.");
4848
} catch (ExecutionException e) {
4949
if (e.getCause() instanceof IOException) {
50-
System.err.println("[Error] There was an issue with reading: "+ menu.getDataLoc());
50+
System.err.println("[Error] There was an issue with reading: "+ menu.getDataPath());
5151
} else {
5252
System.err.println(e.getMessage());
5353
}
@@ -65,7 +65,7 @@ public static void main(String[] args) throws InterruptedException {
6565
Language result = dsParser.detect(qParser.getQueryMapping());
6666
System.out.printf("\nThe text appears to be written in %s.\n", OutColour.format(result.toString(), OutColour.RESULT));
6767
} catch (IllegalStateException e) {
68-
System.err.println("[Error] Failed to detect the language.");
68+
System.err.println("\nFailed to detect the language: "+ e.getMessage());
6969
}
7070

7171
System.out.println("\nTime: "+ (System.currentTimeMillis() - startTime) / 1000 +" (s)");

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

+32-25
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
11
package ie.gmit.sw.menu;
22

3+
import ie.gmit.sw.query.Query;
4+
import ie.gmit.sw.query.QueryFile;
5+
import ie.gmit.sw.query.QueryText;
6+
37
import java.io.File;
48
import java.io.FileNotFoundException;
9+
import java.nio.file.Path;
510
import java.util.Scanner;
611

712
public class Menu {
8-
private String dataLoc;
9-
private String queryFileLoc;
13+
private Path dataPath;
14+
private Query query;
1015

11-
public String getDataLoc() {
12-
return dataLoc;
16+
public Path getDataPath() {
17+
return dataPath;
1318
}
1419

15-
public void setDataLoc(String dataLoc) throws FileNotFoundException {
16-
if (isInvalidFile(dataLoc)) {
17-
throw new FileNotFoundException("Couldn't find file: \""+ dataLoc + "\"");
18-
}
19-
20-
this.dataLoc = dataLoc;
20+
public void setDataPath(Path dataPath) {
21+
this.dataPath = dataPath;
2122
}
2223

23-
public String getQueryFileLoc() {
24-
return queryFileLoc;
24+
public Query getQuery() {
25+
return query;
2526
}
2627

27-
public void setQueryFileLoc(String queryFileLoc) throws FileNotFoundException {
28-
if (isInvalidFile(queryFileLoc)) {
29-
throw new FileNotFoundException("Couldn't find file: \""+ queryFileLoc + "\"");
30-
}
31-
32-
this.queryFileLoc = queryFileLoc;
28+
public void setQuery(Query query) {
29+
this.query = query;
3330
}
3431

3532
public void display() {
@@ -42,16 +39,26 @@ public void display() {
4239

4340
do {
4441
try {
45-
if (dataLoc == null) {
42+
if (dataPath == null) {
4643
System.out.print("$ Enter WiLi data location: ");
4744
input = console.nextLine().trim();
48-
setDataLoc(input);
45+
46+
if (isFile(input)) {
47+
setDataPath(Path.of(input));
48+
} else {
49+
throw new FileNotFoundException("That file does not exist");
50+
}
4951
}
5052

51-
if (queryFileLoc == null) {
52-
System.out.print("$ Enter the query file location: ");
53+
if (query == null) {
54+
System.out.print("$ Enter the query text/file: ");
5355
input = console.nextLine().trim();
54-
setQueryFileLoc(input);
56+
57+
if (isFile(input)) {
58+
setQuery(new QueryFile(Path.of(input)));
59+
} else {
60+
setQuery(new QueryText(input));
61+
}
5562
}
5663

5764
break;
@@ -61,7 +68,7 @@ public void display() {
6168
} while (true);
6269
}
6370

64-
private static boolean isInvalidFile(String path) {
65-
return !new File(path).isFile();
71+
private static boolean isFile(String filename) {
72+
return new File(filename).isFile();
6673
}
6774
}

src/ie/gmit/sw/LanguageEntry.java src/ie/gmit/sw/parser/LanguageEntry.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ie.gmit.sw;
1+
package ie.gmit.sw.parser;
22

33
public class LanguageEntry implements Comparable<LanguageEntry> {
44
private int kmer;

src/ie/gmit/sw/parser/Parser.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@
33
import java.util.Arrays;
44
import java.util.concurrent.Callable;
55

6-
public abstract class Parser implements Callable<Void> {
7-
private final String filePath;
6+
public abstract class Parser<T> implements Callable<Void> {
7+
private T parsable;
88
private int[] k;
99

10-
public Parser(String filePath, int ... k) {
10+
public Parser(T parsable, int ... k) {
1111
if (k.length == 0) {
1212
throw new IllegalArgumentException("k[] cannot be empty.");
1313
}
14-
this.filePath = filePath;
14+
this.parsable = parsable;
1515
this.k = k;
1616
}
1717

18-
public String getFilePath() {
19-
return filePath;
18+
public T getParsable() {
19+
return parsable;
20+
}
21+
22+
public Parser<T> setParsable(T parsable) {
23+
this.parsable = parsable;
24+
25+
return this;
2026
}
2127

2228
public int[] getK() {
+6-36
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,26 @@
11
package ie.gmit.sw.parser;
22

3-
import ie.gmit.sw.LanguageEntry;
3+
import ie.gmit.sw.query.Query;
44

5-
import java.io.IOException;
6-
import java.nio.file.Files;
7-
import java.nio.file.Path;
85
import java.util.Map;
96
import java.util.Set;
107
import java.util.TreeMap;
118
import java.util.TreeSet;
129

13-
public class QueryParser extends Parser {
10+
public class QueryParser extends Parser<Query> {
1411
private Map<Integer, LanguageEntry> queryMap = new TreeMap<>();
15-
private int queryLength = 400;
1612

17-
public QueryParser(String filePath, int ... k) {
18-
super(filePath, k);
13+
public QueryParser(Query query, int ... k) {
14+
super(query, k);
1915
}
2016

2117
public Map<Integer, LanguageEntry> getQueryMapping() {
2218
return new TreeMap<>(queryMap);
2319
}
2420

25-
public int getQueryLength() {
26-
return queryLength;
27-
}
28-
29-
public void setQueryLength(int queryLength) {
30-
this.queryLength = queryLength;
31-
}
32-
3321
@Override
34-
public Void call() throws IOException {
35-
String text = getQueryString();
22+
public Void call() throws Exception {
23+
String text = getParsable().getQueryString();
3624

3725
for (int i = 0; i < getK().length; i++) {
3826
for (int j = 0; j <= text.length() - getK()[i]; j++) {
@@ -59,22 +47,4 @@ private void rank() {
5947
queryMap.put(entry.getKmer(), entry.setRank(++rank));
6048
}
6149
}
62-
63-
/**
64-
* Parse the first 400 or so characters from the query file into a query sentence.
65-
* @throws IOException if an IO error occurs when reading the query file
66-
*/
67-
private String getQueryString() throws IOException {
68-
// Read string and get rid of any extra whitespace
69-
String queryString = Files.readString(Path.of(getFilePath()))
70-
.replace("\r", " ")
71-
.replace("\n", " ")
72-
.replaceAll(" +", " ");
73-
74-
if (queryString.length() > queryLength) {
75-
return queryString.substring(0, queryLength);
76-
}
77-
78-
return queryString;
79-
}
8050
}

src/ie/gmit/sw/SubjectDatabase.java src/ie/gmit/sw/parser/SubjectDatabase.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package ie.gmit.sw;
1+
package ie.gmit.sw.parser;
2+
3+
import ie.gmit.sw.Language;
24

35
import java.util.Collection;
46
import java.util.Map;

src/ie/gmit/sw/parser/SubjectParser.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
package ie.gmit.sw.parser;
22

3-
import ie.gmit.sw.SubjectDatabase;
43
import ie.gmit.sw.Language;
5-
import ie.gmit.sw.LanguageEntry;
64

75
import java.io.BufferedReader;
86
import java.io.FileInputStream;
97
import java.io.IOException;
108
import java.io.InputStreamReader;
9+
import java.nio.file.Path;
1110
import java.util.Map;
1211
import java.util.concurrent.ExecutorService;
1312
import java.util.concurrent.Executors;
1413
import java.util.concurrent.TimeUnit;
1514

16-
public class SubjectParser extends Parser {
15+
public class SubjectParser extends Parser<Path> {
1716
private SubjectDatabase db = new SubjectDatabase();
1817

19-
public SubjectParser(String filePath, int ... k) {
18+
public SubjectParser(Path filePath, int ... k) {
2019
super(filePath, k);
2120
}
2221

@@ -41,7 +40,7 @@ public void resize(int max) {
4140
public Void call() throws IOException {
4241
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
4342

44-
var br = new BufferedReader(new InputStreamReader(new FileInputStream(getFilePath())));
43+
var br = new BufferedReader(new InputStreamReader(new FileInputStream(getParsable().toFile())));
4544
String line;
4645

4746
while ((line = br.readLine()) != null) {

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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ie.gmit.sw.query;
2+
3+
public interface Query {
4+
int QUERY_LENGTH = 400;
5+
6+
String getQueryString() throws Exception;
7+
8+
default String removeWhitespace(String qs) {
9+
return qs.replace("\r", " ")
10+
.replace("\n", " ")
11+
.replaceAll(" +", " ");
12+
}
13+
}

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

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package ie.gmit.sw.query;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
7+
public class QueryFile implements Query {
8+
private Path filepath;
9+
10+
public QueryFile(Path filepath) {
11+
this.filepath = filepath;
12+
}
13+
14+
public Path getFilepath() {
15+
return filepath;
16+
}
17+
18+
public QueryFile setFilepath(Path filepath) {
19+
this.filepath = filepath;
20+
21+
return this;
22+
}
23+
24+
@Override
25+
public String getQueryString() throws IOException {
26+
// Read string and get rid of any extra whitespace
27+
String queryString = removeWhitespace(Files.readString(filepath));
28+
29+
if (queryString.length() > QUERY_LENGTH) {
30+
return queryString.substring(0, QUERY_LENGTH);
31+
}
32+
33+
return queryString;
34+
}
35+
}

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

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ie.gmit.sw.query;
2+
3+
public class QueryText implements Query {
4+
private String queryText;
5+
6+
public QueryText(String queryText) {
7+
this.queryText = queryText;
8+
}
9+
10+
public String getQueryText() {
11+
return queryText;
12+
}
13+
14+
public QueryText setQueryText(String queryText) {
15+
this.queryText = queryText;
16+
17+
return this;
18+
}
19+
20+
@Override
21+
public String getQueryString() {
22+
String queryString = removeWhitespace(queryText);
23+
24+
if (queryString.length() > QUERY_LENGTH) {
25+
return queryString.substring(0, QUERY_LENGTH);
26+
}
27+
28+
return queryString;
29+
}
30+
}

0 commit comments

Comments
 (0)