TfidfRanking extends JCasAnnotator_ImplBase { @Override public void process(JCas jcas) throws AnalysisEngineProcessException { for (Keyphrase keyphrase : select(jcas, Keyphrase.class)) { List<Double> tfidfScores = new ArrayList<Double>(); for (Tfidf tfidf : selectCovered(jcas, Tfidf.class, keyphrase)) { tfidfScores.add(tfidf.getTfidfValue()); } keyphrase.setScore( getAggregatedTfidfScore(tfidfScores, aggregateFunction) ); } }  @Override void process(JCas jcas); static final String PARAM_AGGREGATE; }
@Test public void defaultTest() throws Exception { System.out.println("Default settings. Maximum."); String testDocument = "example sentence funny. second example."; AnalysisEngine analysisEngine = AnalysisEngineFactory.createPrimitive( TfidfRanking.class); JCas jcas = setup(testDocument, analysisEngine); analysisEngine.process(jcas); int i=0; for (Keyphrase k : JCasUtil.select(jcas, Keyphrase.class)) { System.out.println(k); if (i==0) { assertEquals("example sentence funny", k.getKeyphrase()); assertEquals(1.0, k.getScore(), EPSILON); } if (i==1) { assertEquals("second example", k.getKeyphrase()); assertEquals(0.5, k.getScore(), EPSILON); } i++; } assertEquals(2,i); }
@Test public void averageTest() throws Exception { System.out.println("Average tfidf."); String testDocument = "example sentence funny. second example."; AnalysisEngine analysisEngine = AnalysisEngineFactory.createPrimitive( TfidfRanking.class, TfidfRanking.PARAM_AGGREGATE, TfidfAggregate.avg.toString() ); JCas jcas = setup(testDocument, analysisEngine); analysisEngine.process(jcas); int i=0; for (Keyphrase k : JCasUtil.select(jcas, Keyphrase.class)) { System.out.println(k); if (i==0) { assertEquals("example sentence funny", k.getKeyphrase()); assertEquals(0.6, k.getScore(), EPSILON); } if (i==1) { assertEquals("second example", k.getKeyphrase()); assertEquals(0.45, k.getScore(), EPSILON); } i++; } assertEquals(2,i); }