TypePathExtractor implements NamedFeatureExtractor1<T> { @Override public List<org.cleartk.ml.Feature> extract(JCas view, Annotation focusAnnotation) throws CleartkExtractorException { if (this.type == null) this.type = JCasUtil.getType(view, this.focusClass); this.typeSystem = view.getTypeSystem(); if (!isValidPath(view)) throw CleartkExtractorException.invalidTypePath(path, type); String[] pathMembers = path.split("/"); List<Object> pathValues = new ArrayList<Object>(); _extract(view, focusAnnotation, pathMembers, pathValues); List<org.cleartk.ml.Feature> returnValues = new ArrayList<org.cleartk.ml.Feature>(); Set<Object> values = new HashSet<Object>(); for (Object pathValue : pathValues) { if (!uniqueValues || !values.contains(pathValue)) { returnValues.add(new TypePathFeature(null, pathValue, this.path, this.featureName)); values.add(pathValue); } } return returnValues; } TypePathExtractor(
      Class<T> focusClass,
      String typePath,
      boolean traverseAllPaths,
      boolean returnAllValues,
      boolean uniqueValues);  TypePathExtractor(Class<T> focusClass, String typePath); static String createName(String namePrefix, String typePath); @Override String getFeatureName(); @Override List<org.cleartk.ml.Feature> extract(JCas view, Annotation focusAnnotation); static boolean isValidPath(Type type, String path, JCas view); static boolean isValidType(Type type, TypeSystem typeSystem); boolean isAllPaths(); boolean isAllValues(); Class<? extends Annotation> getFocusClass(); String getPath(); boolean isUniqueValues();  }
@Test public void testExtract() throws Throwable { AnalysisEngine engine = AnalysisEngineFactory.createEngine(Annotator.class); engine.process(jCas); engine.collectionProcessComplete(); FSIndex<Annotation> fsIndex = jCas.getAnnotationIndex(Token.type); assertTrue(jCas.getTypeSystem().subsumes( jCas.getTypeSystem().getType("uima.tcas.Annotation"), jCas.getCasType(PosTag.type))); Token targetToken = new Token(jCas); targetToken.setBegin(0); targetToken.setEnd(8); Token token1 = (Token) fsIndex.find(targetToken); TypePathExtractor<Token> typePathExtractor = new TypePathExtractor<Token>( Token.class, "lemma/value"); List<Feature> features = typePathExtractor.extract(jCas, token1); assertEquals(features.size(), 1); TypePathFeature feature = (TypePathFeature) features.get(0); assertEquals(feature.getValue().toString(), "wwwwwwww"); assertEquals(feature.getTypePath(), "lemma/value"); typePathExtractor = new TypePathExtractor<Token>(Token.class, "lemma"); features = typePathExtractor.extract(jCas, token1); assertEquals(features.size(), 1); feature = (TypePathFeature) features.get(0); assertEquals(feature.getValue().toString(), "www"); assertEquals(feature.getTypePath(), "lemma"); targetToken.setBegin(12); targetToken.setEnd(14); Token token3 = (Token) fsIndex.find(targetToken); typePathExtractor = new TypePathExtractor<Token>(Token.class, "lemma/begin"); features = typePathExtractor.extract(jCas, token1); assertEquals(features.size(), 1); feature = (TypePathFeature) features.get(0); assertEquals(feature.getValue(), 1); assertEquals(feature.getTypePath(), "lemma/begin"); typePathExtractor = new TypePathExtractor<Token>(Token.class, "posTag", true, true, false); features = typePathExtractor.extract(jCas, token1); assertEquals(features.size(), 2); assertEquals(features.get(0).getValue().toString(), "Wwwwwwww"); assertEquals(features.get(1).getValue().toString(), "Wwwwwwww"); typePathExtractor = new TypePathExtractor<Token>(Token.class, "posTag", true, true, true); features = typePathExtractor.extract(jCas, token1); assertEquals(features.size(), 1); assertEquals(features.get(0).getValue().toString(), "Wwwwwwww"); typePathExtractor = new TypePathExtractor<Token>(Token.class, "posTag", true, false, false); features = typePathExtractor.extract(jCas, token1); assertEquals(features.size(), 1); assertEquals(features.get(0).getValue().toString(), "Wwwwwwww"); typePathExtractor = new TypePathExtractor<Token>(Token.class, "posTag", false, false, false); features = typePathExtractor.extract(jCas, token1); assertEquals(features.size(), 1); assertEquals(features.get(0).getValue().toString(), "Wwwwwwww"); typePathExtractor = new TypePathExtractor<Token>( Token.class, "lemma/testFS", false, false, false); features = typePathExtractor.extract(jCas, token1); assertEquals(features.size(), 1); assertEquals(features.get(0).getValue().toString(), "A"); typePathExtractor = new TypePathExtractor<Token>( Token.class, "lemma/testFS", false, true, false); features = typePathExtractor.extract(jCas, token1); assertEquals(features.size(), 3); assertEquals(features.get(0).getValue().toString(), "A"); assertEquals(features.get(1).getValue().toString(), "B"); assertEquals(features.get(2).getValue().toString(), "A"); typePathExtractor = new TypePathExtractor<Token>(Token.class, "lemma/testFS", false, true, true); features = typePathExtractor.extract(jCas, token1); assertEquals(features.size(), 2); assertEquals(features.get(0).getValue().toString(), "A"); assertEquals(features.get(1).getValue().toString(), "B"); typePathExtractor = new TypePathExtractor<Token>(Token.class, "depRel/head"); features = typePathExtractor.extract(jCas, token3); assertEquals(features.size(), 1); feature = (TypePathFeature) features.get(0); assertEquals(feature.getValue().toString(), "Wwwwwwww"); assertEquals(feature.getTypePath(), "depRel/head"); typePathExtractor = new TypePathExtractor<Token>(Token.class, "depRel/head", true, true, true); features = typePathExtractor.extract(jCas, token3); assertEquals(features.size(), 2); feature = (TypePathFeature) features.get(0); assertEquals(feature.getValue().toString(), "Wwwwwwww"); assertEquals(feature.getTypePath(), "depRel/head"); feature = (TypePathFeature) features.get(1); assertEquals(feature.getTypePath(), "depRel/head"); assertEquals(feature.getValue().toString(), "ii"); typePathExtractor = new TypePathExtractor<Token>( Token.class, "depRel/head/posTag/value", true, true, true); features = typePathExtractor.extract(jCas, token3); assertEquals(features.size(), 3); assertEquals(features.get(0).getValue().toString(), "pos1"); assertEquals(features.get(1).getValue().toString(), "pos2"); assertEquals(features.get(2).getValue().toString(), "pos3"); typePathExtractor = new TypePathExtractor<Token>( Token.class, "depRel/head/posTag/value", true, false, true); features = typePathExtractor.extract(jCas, token3); assertEquals(features.size(), 3); assertEquals(features.get(0).getValue().toString(), "pos1"); assertEquals(features.get(1).getValue().toString(), "pos2"); assertEquals(features.get(2).getValue().toString(), "pos3"); typePathExtractor = new TypePathExtractor<Token>( Token.class, "depRel/head/posTag/value", false, true, true); features = typePathExtractor.extract(jCas, token3); assertEquals(features.size(), 1); assertEquals(features.get(0).getValue().toString(), "pos1"); typePathExtractor = new TypePathExtractor<Token>( Token.class, "depRel/head/posTag/value", false, false, true); features = typePathExtractor.extract(jCas, token3); assertEquals(features.size(), 1); assertEquals(features.get(0).getValue().toString(), "pos1"); }
@Test public void testTicket23() throws Throwable { tokenBuilder.buildTokens(jCas, "wide .\nThis place was a tolerable long ,"); Token token = JCasUtil.selectByIndex(jCas, Token.class, 3); assertEquals("place", token.getCoveredText()); token.setPos("A"); Token tokenL0 = JCasUtil.selectByIndex(jCas, Token.class, 2); tokenL0.setPos("B"); TypePathExtractor<Token> posExtractor = new TypePathExtractor<Token>(Token.class, "pos"); Feature feature = posExtractor.extract(jCas, token).get(0); assertEquals("A", feature.getValue().toString()); assertEquals("TypePath(Pos)", feature.getName()); feature = posExtractor.extract(jCas, tokenL0).get(0); assertEquals("B", feature.getValue().toString()); assertEquals("TypePath(Pos)", feature.getName()); }