InteractionValueEncoder extends FeatureVectorEncoder { public void addInteractionToVector(String original1, String original2, double weight, Vector data) { byte[] originalForm1 = bytesForString(original1); byte[] originalForm2 = bytesForString(original2); addInteractionToVector(originalForm1, originalForm2, weight, data); } InteractionValueEncoder(String name, FeatureVectorEncoder encoderOne, FeatureVectorEncoder encoderTwo); @Override void addToVector(String originalForm, double w, Vector data); @Override void addToVector(byte[] originalForm, double w, Vector data); void addInteractionToVector(String original1, String original2, double weight, Vector data); void addInteractionToVector(byte[] originalForm1, byte[] originalForm2, double weight, Vector data); @Override String asString(String originalForm);  }
@Test public void testAddToVectorUsesProductOfWeights() { WordValueEncoder wv = new StaticWordValueEncoder("word"); ContinuousValueEncoder cv = new ContinuousValueEncoder("cont"); InteractionValueEncoder enc = new InteractionValueEncoder("interactions", wv, cv); Vector v1 = new DenseVector(200); enc.addInteractionToVector("a","0.9",0.5, v1); int k = enc.getProbes(); assertEquals((float) k*0.5*0.9, v1.norm(1), 0); assertEquals(0.5*0.9, v1.maxValue(), 0); }
@Test public void testAddToVectorWithTextValueEncoder() { WordValueEncoder wv = new StaticWordValueEncoder("word"); TextValueEncoder tv = new TextValueEncoder("text"); InteractionValueEncoder enc = new InteractionValueEncoder("interactions", wv, tv); Vector v1 = new DenseVector(200); enc.addInteractionToVector("a","some text here",1.0, v1); int k = enc.getProbes(); assertEquals((float) k*3, v1.norm(1), 0); }
@Test public void testTraceDictionary() { StaticWordValueEncoder encoder1 = new StaticWordValueEncoder("first"); StaticWordValueEncoder encoder2 = new StaticWordValueEncoder("second"); Map<String, Set<Integer>> traceDictionary = Maps.newHashMap(); InteractionValueEncoder interactions = new InteractionValueEncoder("interactions", encoder1, encoder2); interactions.setProbes(1); interactions.setTraceDictionary(traceDictionary); Vector v = new DenseVector(10); interactions.addInteractionToVector("a", "b", 1, v); assertEquals(1, v.getNumNonZeroElements()); assertEquals(1, traceDictionary.size()); assertEquals("interactions=a:b", getFirst(traceDictionary.keySet(), null)); }