Cambridge SMT System
FeatureFunctionRegistry.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  *******************************************************************************/
15 
16 package uk.ac.cam.eng.rule.features;
17 
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.function.BiFunction;
21 
22 import uk.ac.cam.eng.extraction.Rule;
28 
36 public final class FeatureFunctionRegistry {
37 
38  static class FeatureFunctionInputData {
39 
40  AlignmentCountMapWritable alignments;
41 
42  FeatureMap features;
43 
44  ProvenanceCountMap counts;
45 
46  Feature requested;
47 
48  FeatureRegistry fReg;
49 
50  private FeatureFunctionInputData() {
51 
52  }
53 
54  FeatureFunctionInputData build(RuleData data, Feature requested, FeatureRegistry fReg) {
55  alignments = data.getAlignments();
56  features = data.getFeatures();
57  counts = data.getProvCounts();
58  this.requested = requested;
59  this.fReg = fReg;
60  return this;
61  }
62 
63  }
64 
65  private static ThreadLocal<FeatureFunctionInputData> ffInput = new ThreadLocal<FeatureFunctionInputData>(){
66 
67  @Override
68  protected FeatureFunctionInputData initialValue() {
69  return new FeatureFunctionInputData();
70  }
71 
72  };
73 
74 
75  private static Map<Feature, BiFunction<Rule, FeatureFunctionInputData, double[]>> featureFunctions = new HashMap<>();
76 
77  static double[] computeFeature(Feature feature, Rule rule,
78  RuleData data, FeatureRegistry fReg) {
79  if (ComputeLocation.MAP_REDUCE == feature.computed) {
80  throw new UnsupportedOperationException(
81  "Attempting to compute the MapReduce featue: "
82  + feature.getConfName() + " at retrieval time");
83  }
84  BiFunction<Rule, FeatureFunctionInputData, double[]> f
85  = featureFunctions.get(feature);
86  if (f == null) {
87  return null;
88  }
89  return f.apply(new Rule(rule), ffInput.get().build(data, feature, fReg));
90  }
91 
92 
93  private static void registerFunction(Feature f,
94  BiFunction<Rule, FeatureFunctionInputData, double[]> ff) {
95  if (Feature.ComputeLocation.RETRIEVAL != f.computed) {
96  throw new UnsupportedOperationException(
97  "Trying to register feature functions for a non-retrieval time feature");
98  }
99  featureFunctions.put(f, ff);
100  }
101 
102 
103 
104  //Register functions!
105  static{
106  registerFunction(Feature.RULE_COUNT_1,
107  FeatureFunctions::ruleCount1);
108  registerFunction(Feature.RULE_COUNT_2,
109  FeatureFunctions::ruleCount2);
110  registerFunction(Feature.RULE_COUNT_GREATER_THAN_2, FeatureFunctions::ruleGreaterThan2);
111  registerFunction(Feature.WORD_INSERTION_PENALTY,
112  FeatureFunctions::noOfWords);
113  }
114 }