Cambridge SMT System
TTableClient.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use these files except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  *
14  * Copyright 2014 - Juan Pino, Aurelien Waite, William Byrne
15  *******************************************************************************/
16 package uk.ac.cam.eng.extraction.hadoop.features.lexical;
17 
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;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 
31 import uk.ac.cam.eng.extraction.Rule;
36 import uk.ac.cam.eng.util.CLI;
37 import uk.ac.cam.eng.util.Pair;
38 
44 public class TTableClient {
45 
46  private String hostName;
47 
48  private int port;
49 
50  private LexicalProbability prob;
51 
52  private Map<List<Integer>, Double> wordAlignments = new HashMap<>();
53 
54  private int noOfProvs;
55 
56  Feature lexicalF;
57 
58  Feature provLexicalF;
59 
60  public void setup(CLI.ServerParams params,
61  int noOfProvs, boolean source2Target) {
62  if (source2Target) {
63  port = params.ttableS2TServerPort;
64  hostName = params.ttableS2THost;
67  } else {
68  port = params.ttableT2SServerPort;
69  hostName = params.ttableT2SHost;
72  }
73  prob = new LexicalProbability(source2Target);
74  this.noOfProvs = noOfProvs +1;
75  }
76 
77  private double[] query(Collection<List<Integer>> query) throws IOException {
78 
79  try (Socket clientSocket = new Socket(hostName, port);
80  DataInputStream in = new DataInputStream(
81  new BufferedInputStream(clientSocket.getInputStream(),
82  TTableServer.BUFFER_SIZE));
83  DataOutputStream out = new DataOutputStream(
84  new BufferedOutputStream(
85  clientSocket.getOutputStream(),
86  TTableServer.BUFFER_SIZE))) {
87 
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));
94  }
95  out.flush();
96 
97  for (int i = 0; i < query.size(); ++i) {
98  double val = in.readDouble();
99  result[i] = val;
100  }
101  return result;
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);
106  }
107  }
108 
109  public void queryRules(
110  Map<Rule, Pair<EnumRuleType, RuleData>> rules)
111  throws IOException {
112  for (Entry<Rule, Pair<EnumRuleType,RuleData>> entry : rules.entrySet()) {
113  Rule key = entry.getKey();
114  // Need to add the 0th element for the global scope
115  prob.buildQuery(key, noOfProvs, wordAlignments);
116  }
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]);
123  }
124  }
125  for (Entry<Rule, Pair<EnumRuleType,RuleData>> entry : rules.entrySet()) {
126  Rule key = entry.getKey();
127  RuleData features = entry.getValue().getSecond();
128  ProvenanceProbMap provLexProbs = new ProvenanceProbMap();
129  for (int j = 0; j < noOfProvs ; ++j) {
130  double lexProb = prob.value(key, (byte) j, wordAlignments);
131  if(j==0){
132  ProvenanceProbMap globalLexProb = new ProvenanceProbMap();
133  globalLexProb.put(j, lexProb);
134  features.getFeatures().put(lexicalF, globalLexProb);
135  }else{
136  provLexProbs.put(j, lexProb);
137  }
138  features.getFeatures().put(provLexicalF, provLexProbs);
139  }
140  }
141  }
142 
143 }
DoubleWritable put(IntWritable key, DoubleWritable value)
void setup(CLI.ServerParams params, int noOfProvs, boolean source2Target)
fst::TropicalWeightTpl< F > Map(double)
void queryRules(Map< Rule, Pair< EnumRuleType, RuleData >> rules)