0

I wrote a java code to execute Vowpal Wabbit in the following way:

 System.out.println("Executing command " + command);
        final Runtime r = Runtime.getRuntime();
        final Process p = r.exec(command);
        System.out.println("waiting for the process");
        try (final BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
            String line;

            while ((line = b.readLine()) != null) {
                final T lineResult = textParser.parseLine(line);
                parserResultCombiner.addToCombiner(lineResult);
            }
        }
        p.waitFor();
        System.out.println("done");
}

where the command is

vw -d input.txt --loss_function=logistic -f model.vw

The disadvantage of this is that it requires writing to disk. After some searching, I learned that vowpal wabbit supports reading data from standard input example in R

I could not find any example to accomplish this in Java 1.8. Could anyone share one with me?

Tad
  • 838
  • 2
  • 11
  • 22
  • have a look [here](http://stackoverflow.com/questions/10407308/redirect-stdin-and-stdout-in-java), it shows how to interact with an external program's stdin and stdout from Java. That post also points to [this question](http://stackoverflow.com/questions/3643939/java-process-with-input-output-stream/3644288#3644288), also worth reading. – fvu Oct 21 '14 at 16:37
  • Since your code already reads from the stdout of the external process it shouldn’t be so hard to figure out how to write to its stdin. Just use `p.getOutputStream()` instead of `p.getInputStream()` and write to it… – Holger Oct 23 '14 at 14:26

1 Answers1

3

You need to start vw in daemon mode. This starts a process that listens on the port specified.

$ vw -i model.vw  -t --daemon --quiet --port 26542

Once the daemon has started, you can send samples to predict using socket calls

$ echo " abc-example| a b c" | netcat localhost 26542
0.000000 abc-example

$ echo " xyz-example| x y z" | netcat localhost 26542
1.000000 xyz-example

source: https://github.com/JohnLangford/vowpal_wabbit/wiki/daemon-example

Recently they pushed a java version of the code that interacts with vw using jni https://github.com/JohnLangford/vowpal_wabbit/tree/master/java

viper
  • 2,220
  • 5
  • 27
  • 33