16 package uk.ac.cam.eng.extraction.hadoop.features.lexical;
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
20 import java.io.DataInputStream;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23 import java.net.Socket;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.Map.Entry;
46 private String hostName;
50 private LexicalProbability prob;
52 private Map<List<Integer>, Double> wordAlignments =
new HashMap<>();
54 private int noOfProvs;
61 int noOfProvs,
boolean source2Target) {
63 port = params.ttableS2TServerPort;
64 hostName = params.ttableS2THost;
68 port = params.ttableT2SServerPort;
69 hostName = params.ttableT2SHost;
73 prob =
new LexicalProbability(source2Target);
74 this.noOfProvs = noOfProvs +1;
77 private double[] query(Collection<List<Integer>> query)
throws IOException {
79 try (Socket clientSocket =
new Socket(hostName, port);
80 DataInputStream in =
new DataInputStream(
81 new BufferedInputStream(clientSocket.getInputStream(),
83 DataOutputStream out =
new DataOutputStream(
84 new BufferedOutputStream(
85 clientSocket.getOutputStream(),
88 double[] result =
new double[query.size()];
89 out.writeInt(query.size());
90 for (List<Integer> queryKey : query) {
91 out.writeInt(queryKey.get(0));
92 out.writeInt(queryKey.get(1));
93 out.writeInt(queryKey.get(2));
97 for (
int i = 0; i < query.size(); ++i) {
98 double val = in.readDouble();
102 }
catch (java.net.ConnectException e) {
103 String message =
"Failed to connect to ttable server. Hostname: " 104 + hostName +
" Port: " + port;
105 throw new IOException(message, e);
113 Rule key = entry.getKey();
115 prob.buildQuery(key, noOfProvs, wordAlignments);
117 List<List<Integer>> keys =
new ArrayList<>(wordAlignments.keySet());
118 double[] results = query(keys);
119 wordAlignments.clear();
120 for (
int i = 0; i < keys.size(); ++i) {
121 if (results[i] != Double.MAX_VALUE) {
122 wordAlignments.put(keys.get(i), results[i]);
126 Rule key = entry.getKey();
127 RuleData features = entry.getValue().getSecond();
129 for (
int j = 0; j < noOfProvs ; ++j) {
130 double lexProb = prob.value(key, (byte) j, wordAlignments);
133 globalLexProb.
put(j, lexProb);
134 features.
getFeatures().put(lexicalF, globalLexProb);
136 provLexProbs.
put(j, lexProb);
138 features.
getFeatures().put(provLexicalF, provLexProbs);
SOURCE2TARGET_LEXICAL_PROBABILITY
TARGET2SOURCE_LEXICAL_PROBABILITY
fst::TropicalWeightTpl< F > Map(double)
PROVENANCE_SOURCE2TARGET_LEXICAL_PROBABILITY
PROVENANCE_TARGET2SOURCE_LEXICAL_PROBABILITY