File size: 1,659 Bytes
158b61b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
public class LMClient {
private Socket sock;
private DataInputStream input;
private OutputStreamWriter output;
public LMClient(URI u) throws IOException {
sock = new Socket(u.getHost(), u.getPort());
System.err.println(sock);
input = new DataInputStream(sock.getInputStream());
output = new OutputStreamWriter(sock.getOutputStream(), "UTF8");
}
public float wordLogProb(String word, String context) throws IOException {
return wordLogProb(word, context.split("\\s+"));
}
public float wordLogProb(String word, String[] context) throws IOException {
StringBuffer sb = new StringBuffer();
sb.append("prob ");
sb.append(word);
for (int i = context.length-1; i >= 0; --i) {
sb.append(' ').append(context[i]);
}
sb.append("\r\n");
output.write(sb.toString());
output.flush();
byte b1 = input.readByte();
byte b2 = input.readByte();
byte b3 = input.readByte();
byte b4 = input.readByte();
Float f = Float.intBitsToFloat( (((b4 & 0xff) << 24) | ((b3 & 0xff) << 16) | ((b2 & 0xff) << 8) | (b1 & 0xff)) );
input.readByte(); input.readByte();
return f;
}
public static void main(String[] args) {
try {
LMClient lm = new LMClient(new URI("lm://csubmit02.umiacs.umd.edu:6666"));
System.err.println(lm.wordLogProb("want", "<s> the old man"));
System.err.println(lm.wordLogProb("wants", "<s> the old man"));
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|