CooccurrenceGraph extends JCasAnnotator_ImplBase { @Override public void process(JCas jcas) throws AnalysisEngineProcessException { getContext().getLogger().log(Level.CONFIG, "Entering " + this.getClass().getSimpleName()); termMap = new HashMap<String, Integer>(); edgeMap = new HashMap<Edge,Integer>(); initializeGraph(jcas); int maxAmount = 1; for(Integer amountOfConnections : edgeMap.values()){ if(amountOfConnections > maxAmount){ maxAmount = amountOfConnections; } } for (Edge edge : edgeMap.keySet()) { WeightedAnnotationPair pair = new WeightedAnnotationPair(jcas); pair.setAnnotation1( (Annotation) edge.annotation1 ); pair.setAnnotation2( (Annotation) edge.annotation2 ); pair.setStringRepresentation1( edge.term1 ); pair.setStringRepresentation2( edge.term2 ); pair.setWeight(edgeMap.get(edge)/(double) maxAmount); pair.addToIndexes(); } }  @Override void process(JCas jcas); static final String PARAM_WINDOW_SIZE; static final String PARAM_FEATURE_PATH; }
@Test public void cooccurrenceGraphTest() throws Exception { String testDocument = "example sentence funny. second example."; Set<String> expectedValues = new HashSet<String>(); expectedValues.add("example"); expectedValues.add("sentence"); expectedValues.add("funny"); expectedValues.add("second"); AnalysisEngine analysisEngine = createEngine( CooccurrenceGraph.class, CooccurrenceGraph.PARAM_FEATURE_PATH, Token.class.getName()+"/"); JCas jcas = setup(testDocument, analysisEngine); analysisEngine.process(jcas); int i=0; for (AnnotationPair ap : JCasUtil.select(jcas, AnnotationPair.class)) { assertTrue(ap.getStringRepresentation1(), expectedValues.contains(ap.getStringRepresentation1())); assertTrue(ap.getStringRepresentation2(), expectedValues.contains(ap.getStringRepresentation2())); System.out.println(ap); i++; } assertEquals(4,i); }
@Test public void cooccurrenceGraphTest2() throws Exception { String testDocument = "example sentence funny. second example."; Set<String> expectedValues = new HashSet<String>(); expectedValues.add("examp"); expectedValues.add("sent"); expectedValues.add("fu"); expectedValues.add("sec"); AnalysisEngine analysisEngine = AnalysisEngineFactory.createEngine( CooccurrenceGraph.class, CooccurrenceGraph.PARAM_FEATURE_PATH, Stem.class.getName()+"/value", CooccurrenceGraph.PARAM_WINDOW_SIZE, 3 ); JCas jcas = setup(testDocument, analysisEngine); analysisEngine.process(jcas); int i=0; for (AnnotationPair ap : JCasUtil.select(jcas, AnnotationPair.class)) { assertTrue(ap.getStringRepresentation1(), expectedValues.contains(ap.getStringRepresentation1())); assertTrue(ap.getStringRepresentation2(), expectedValues.contains(ap.getStringRepresentation2())); System.out.println(ap); i++; } assertEquals(6,i); }
@Test public void cooccurrenceGraphTest3() throws Exception { String testDocument = "example sentence funny. second example."; Set<String> expectedValues = new HashSet<String>(); expectedValues.add("example"); expectedValues.add("sentence"); expectedValues.add("funny"); expectedValues.add("second"); AnalysisEngine analysisEngine = AnalysisEngineFactory.createEngine( CooccurrenceGraph.class, CooccurrenceGraph.PARAM_FEATURE_PATH, Lemma.class.getName()+"/value" ); JCas jcas = setup(testDocument, analysisEngine); analysisEngine.process(jcas); int i=0; for (AnnotationPair ap : JCasUtil.select(jcas, AnnotationPair.class)) { assertTrue(ap.getStringRepresentation1(), expectedValues.contains(ap.getStringRepresentation1())); assertTrue(ap.getStringRepresentation2(), expectedValues.contains(ap.getStringRepresentation2())); System.out.println(ap); i++; } assertEquals(4,i); }