Skip to content

Commit 7286c10

Browse files
committed
Prepare for release
1 parent 2160b94 commit 7286c10

File tree

2 files changed

+63
-39
lines changed

2 files changed

+63
-39
lines changed

src/main/java/org/netspeak/NetspeakClient.java

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,39 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import net.lingala.zip4j.exception.ZipException;
5-
import org.apache.commons.io.IOUtils;
65

76
import java.io.*;
8-
import java.net.URI;
9-
import java.net.URISyntaxException;
10-
import java.net.URL;
11-
import java.nio.channels.Channels;
12-
import java.nio.channels.FileChannel;
13-
import java.nio.channels.ReadableByteChannel;
14-
import java.nio.file.FileSystems;
15-
import java.nio.file.Files;
16-
import java.nio.file.Path;
17-
import java.nio.file.Paths;
18-
import java.security.ProtectionDomain;
19-
import java.util.Map;
207

218
/**
9+
* Netspeak client
10+
*
2211
2312
*/
2413
public class NetspeakClient {
2514
private static final String PYTHON_IMAGE =
2615
"https://files.webis.de/data-in-production/data-research/netspeak/netspeak-client/py3.10-linux-x86_64.zip";
2716

28-
private Process netspeakProcess;
17+
private final Process netspeakProcess;
2918

30-
private BufferedWriter stdOut;
31-
private BufferedReader stdIn;
32-
private BufferedReader stdErr;
19+
private final BufferedWriter stdOut;
20+
private final BufferedReader stdIn;
21+
private final BufferedReader stdErr;
3322

3423
private final ObjectMapper objectMapper;
3524

25+
26+
/**
27+
* Constructor that downloads the Netspeak client binary and executes it as subprocess.
28+
*
29+
* @throws IOException if project filesystems is ot writable
30+
* @throws ZipException if binary can not be inflated
31+
*/
3632
public NetspeakClient() throws IOException, ZipException {
37-
File dir = new File("netspeak");
33+
final File dir = new File("netspeak");
3834
if(!dir.exists()){
3935
if(!dir.mkdirs()){
4036
throw new IOException("Can't create directory!");
41-
};
37+
}
4238

4339
Utils.downloadFile(PYTHON_IMAGE, "netspeak/python-netspeak-client.zip");
4440
Utils.unzipFile("netspeak/python-netspeak-client.zip", "netspeak");
@@ -55,14 +51,21 @@ public NetspeakClient() throws IOException, ZipException {
5551
objectMapper = new ObjectMapper();
5652
}
5753

54+
/**
55+
* Submits a search query to the Netspeak API.
56+
* @param query Search query (support Netspeak syntax)
57+
* @return Search results
58+
* @throws InterruptedException if subprocess crashes
59+
* @throws IOException if an error occurred in the subprocess
60+
*/
5861
public SearchResults search(final String query) throws InterruptedException, IOException {
5962
try{
6063
stdOut.write(query);
6164
stdOut.newLine();
6265
stdOut.flush();
6366
} catch (IOException e){
6467
String line;
65-
StringBuilder errStringBuilder = new StringBuilder();
68+
final StringBuilder errStringBuilder = new StringBuilder();
6669
while ((line = stdErr.readLine()) != null){
6770
errStringBuilder.append(line).append("\n");
6871
}
@@ -73,7 +76,7 @@ public SearchResults search(final String query) throws InterruptedException, IOE
7376

7477
String line = stdIn.readLine();
7578
if (line == null){
76-
StringBuilder errStringBuilder = new StringBuilder();
79+
final StringBuilder errStringBuilder = new StringBuilder();
7780
while ((line = stdErr.readLine()) != null){
7881
errStringBuilder.append(line).append("\n");
7982
}
@@ -84,6 +87,10 @@ public SearchResults search(final String query) throws InterruptedException, IOE
8487
return objectMapper.readValue(line, SearchResults.class);
8588
}
8689

90+
/***
91+
* Ends the client subprocess.
92+
* @throws IOException if closing of IO streams fail.
93+
*/
8794
public void close() throws IOException {
8895
stdOut.write("\\exit");
8996
stdOut.newLine();
@@ -93,16 +100,17 @@ public void close() throws IOException {
93100
netspeakProcess.destroy();
94101
}
95102

96-
public static void main(String[] args) throws InterruptedException, IOException {
103+
public static void main(String[] args) throws IOException {
97104
NetspeakClient netspeakClient = null;
98-
try{
105+
try {
99106
netspeakClient = new NetspeakClient();
100-
SearchResults searchResults = netspeakClient.search("this is ... test");
101-
searchResults = netspeakClient.search("this ... test");
107+
SearchResults searchResults = netspeakClient.search("how to ? this");
108+
searchResults.getPhrases().forEach(System.out::println);
109+
searchResults = netspeakClient.search("see ... works");
102110
searchResults.getPhrases().forEach(System.out::println);
103-
}catch (Exception e){
104-
e.printStackTrace();
105-
}finally {
111+
} catch (Exception e) {
112+
throw new RuntimeException("Oh no. Something went wrong :(", e);
113+
} finally {
106114
if (netspeakClient != null) {
107115
netspeakClient.close();
108116
}

src/main/java/org/netspeak/SearchResults.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
11
package org.netspeak;
22

3+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
4+
35
import java.util.List;
46

57
/**
68
79
*/
10+
@JsonSerialize
811
public class SearchResults {
9-
private List<Phrase> phrases;
12+
private final List<Phrase> phrases;
1013

11-
public List<Phrase> getPhrases() {
12-
return phrases;
14+
public SearchResults() {
15+
phrases = null;
1316
}
1417

15-
public void setPhrases(List<Phrase> phrases) {
18+
public SearchResults(List<Phrase> phrases) {
1619
this.phrases = phrases;
1720
}
1821

19-
public static class Phrase {
20-
private long id;
21-
private long frequency;
22-
private List<String> words;
22+
public List<Phrase> getPhrases() {
23+
return phrases;
24+
}
2325

24-
public long getId() {
25-
return id;
26+
@JsonSerialize
27+
public static class Phrase {
28+
private final long id;
29+
private final long frequency;
30+
private final List<String> words;
31+
32+
public Phrase() {
33+
id = 0L;
34+
frequency = 0L;
35+
words = null;
2636
}
2737

28-
public void setId(long id) {
38+
public Phrase(final long id, final long frequency, final List<String> words) {
2939
this.id = id;
40+
this.frequency = frequency;
41+
this.words = words;
42+
}
43+
44+
public long getId() {
45+
return id;
3046
}
3147

3248
public long getFrequency() {

0 commit comments

Comments
 (0)