22
33import com .fasterxml .jackson .databind .ObjectMapper ;
44import net .lingala .zip4j .exception .ZipException ;
5- import org .apache .commons .io .IOUtils ;
65
76import 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+ *
22112312 */
2413public 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 }
0 commit comments