@Test public void testDisambiguate() throws UIMAException { System.setProperty("ytex.conceptGraphName", "test"); System.setProperty("ytex.conceptPreload", "false"); System.setProperty("ytex.conceptSetName", ""); URL urlTypeSystem = getClass().getClassLoader().getResource("org/apache/ctakes/ytex/types/TypeSystem.xml"); JCas jCas = JCasFactory.createJCasFromPath(urlTypeSystem.getPath()); String text = "concept1 concept2 concept3"; jCas.setDocumentText(text); EntityMention em1 = new EntityMention(jCas); em1.setBegin(0); em1.setEnd(8); setConcepts(jCas, em1, new String[] { "dog" }); em1.addToIndexes(); EntityMention em2 = new EntityMention(jCas); em2.setBegin(9); em2.setEnd(17); setConcepts(jCas, em2, new String[] { "e coli", "animal" }); em2.addToIndexes(); EntityMention em3 = new EntityMention(jCas); em3.setBegin(18); em3.setEnd(26); setConcepts(jCas, em3, new String[] { "cat" }); em3.addToIndexes(); SenseDisambiguatorAnnotator sda = new SenseDisambiguatorAnnotator(); sda.wsd = ApplicationContextHolder.getApplicationContext().getBean(WordSenseDisambiguator.class); sda.process(jCas); AnnotationIndex<Annotation> annoIdx = jCas.getAnnotationIndex(EntityMention.type); List<Annotation> annoList = Lists.newArrayList(annoIdx); EntityMention emD = (EntityMention)annoList.get(1); FSArray fsa = emD.getOntologyConceptArr(); for(int i = 0; i < fsa.size(); i++) { OntologyConcept oc = (OntologyConcept)fsa.get(i); if("animal".equals(oc.getCode())) { Assert.assertTrue(oc.getDisambiguated()); } else { Assert.assertFalse(oc.getDisambiguated()); } } }
protected void disambiguate(JCas jcas, List<IdentifiedAnnotation> listNE) throws AnalysisEngineProcessException { List<IdentifiedAnnotation> listNonTrivialNE = new ArrayList<IdentifiedAnnotation>(); List<Set<String>> listConcept = new ArrayList<Set<String>>(); for (IdentifiedAnnotation ne : listNE) { FSArray concepts = ne.getOntologyConceptArr(); if (concepts != null && concepts.size() > 0) { listNonTrivialNE.add(ne); Set<String> conceptSenses = new HashSet<String>(); listConcept.add(conceptSenses); for (int i = 0; i < concepts.size(); i++) { if (concepts.get(i) != null) { String conceptId = this.getConceptId(concepts.get(i)); if (!Strings.isNullOrEmpty(conceptId)) conceptSenses.add(conceptId); } } } } if(log.isTraceEnabled()) log.trace("listConcept: " + listConcept); for (int i = 0; i < listConcept.size(); i++) { Set<String> conceptSenses = listConcept.get(i); if (conceptSenses.size() > 1) { if(log.isTraceEnabled()) log.trace("i: " + i + ", conceptSenses: " + conceptSenses); Map<String, Double> scores = new HashMap<String, Double>(); String concept = this.wsd.disambiguate(listConcept, i, null, windowSize, metric, scores, true); if(log.isTraceEnabled()) log.trace("i: " + i + ", concept: " + concept); IdentifiedAnnotation ne = listNonTrivialNE.get(i); FSArray concepts = ne.getOntologyConceptArr(); for (int j = 0; j < concepts.size(); j++) { OntologyConcept yoc = (OntologyConcept) concepts.get(j); String conceptId = this.getConceptId(yoc); if (concept == null || concept.equals(conceptId)) yoc.setDisambiguated(true); if (scores.containsKey(conceptId)) yoc.setScore(scores.get(conceptId)); } } else if (conceptSenses.size() == 1) { IdentifiedAnnotation ne = listNonTrivialNE.get(i); FSArray concepts = ne.getOntologyConceptArr(); OntologyConcept oc = (OntologyConcept) concepts.get(0); oc.setDisambiguated(true); } } }
SenseDisambiguatorAnnotator extends JCasAnnotator_ImplBase { protected void disambiguate(JCas jcas, List<IdentifiedAnnotation> listNE) throws AnalysisEngineProcessException { List<IdentifiedAnnotation> listNonTrivialNE = new ArrayList<IdentifiedAnnotation>(); List<Set<String>> listConcept = new ArrayList<Set<String>>(); for (IdentifiedAnnotation ne : listNE) { FSArray concepts = ne.getOntologyConceptArr(); if (concepts != null && concepts.size() > 0) { listNonTrivialNE.add(ne); Set<String> conceptSenses = new HashSet<String>(); listConcept.add(conceptSenses); for (int i = 0; i < concepts.size(); i++) { if (concepts.get(i) != null) { String conceptId = this.getConceptId(concepts.get(i)); if (!Strings.isNullOrEmpty(conceptId)) conceptSenses.add(conceptId); } } } } if(log.isTraceEnabled()) log.trace("listConcept: " + listConcept); for (int i = 0; i < listConcept.size(); i++) { Set<String> conceptSenses = listConcept.get(i); if (conceptSenses.size() > 1) { if(log.isTraceEnabled()) log.trace("i: " + i + ", conceptSenses: " + conceptSenses); Map<String, Double> scores = new HashMap<String, Double>(); String concept = this.wsd.disambiguate(listConcept, i, null, windowSize, metric, scores, true); if(log.isTraceEnabled()) log.trace("i: " + i + ", concept: " + concept); IdentifiedAnnotation ne = listNonTrivialNE.get(i); FSArray concepts = ne.getOntologyConceptArr(); for (int j = 0; j < concepts.size(); j++) { OntologyConcept yoc = (OntologyConcept) concepts.get(j); String conceptId = this.getConceptId(yoc); if (concept == null || concept.equals(conceptId)) yoc.setDisambiguated(true); if (scores.containsKey(conceptId)) yoc.setScore(scores.get(conceptId)); } } else if (conceptSenses.size() == 1) { IdentifiedAnnotation ne = listNonTrivialNE.get(i); FSArray concepts = ne.getOntologyConceptArr(); OntologyConcept oc = (OntologyConcept) concepts.get(0); oc.setDisambiguated(true); } } } }
SenseDisambiguatorAnnotator extends JCasAnnotator_ImplBase { protected void disambiguate(JCas jcas, List<IdentifiedAnnotation> listNE) throws AnalysisEngineProcessException { List<IdentifiedAnnotation> listNonTrivialNE = new ArrayList<IdentifiedAnnotation>(); List<Set<String>> listConcept = new ArrayList<Set<String>>(); for (IdentifiedAnnotation ne : listNE) { FSArray concepts = ne.getOntologyConceptArr(); if (concepts != null && concepts.size() > 0) { listNonTrivialNE.add(ne); Set<String> conceptSenses = new HashSet<String>(); listConcept.add(conceptSenses); for (int i = 0; i < concepts.size(); i++) { if (concepts.get(i) != null) { String conceptId = this.getConceptId(concepts.get(i)); if (!Strings.isNullOrEmpty(conceptId)) conceptSenses.add(conceptId); } } } } if(log.isTraceEnabled()) log.trace("listConcept: " + listConcept); for (int i = 0; i < listConcept.size(); i++) { Set<String> conceptSenses = listConcept.get(i); if (conceptSenses.size() > 1) { if(log.isTraceEnabled()) log.trace("i: " + i + ", conceptSenses: " + conceptSenses); Map<String, Double> scores = new HashMap<String, Double>(); String concept = this.wsd.disambiguate(listConcept, i, null, windowSize, metric, scores, true); if(log.isTraceEnabled()) log.trace("i: " + i + ", concept: " + concept); IdentifiedAnnotation ne = listNonTrivialNE.get(i); FSArray concepts = ne.getOntologyConceptArr(); for (int j = 0; j < concepts.size(); j++) { OntologyConcept yoc = (OntologyConcept) concepts.get(j); String conceptId = this.getConceptId(yoc); if (concept == null || concept.equals(conceptId)) yoc.setDisambiguated(true); if (scores.containsKey(conceptId)) yoc.setScore(scores.get(conceptId)); } } else if (conceptSenses.size() == 1) { IdentifiedAnnotation ne = listNonTrivialNE.get(i); FSArray concepts = ne.getOntologyConceptArr(); OntologyConcept oc = (OntologyConcept) concepts.get(0); oc.setDisambiguated(true); } } }  }
SenseDisambiguatorAnnotator extends JCasAnnotator_ImplBase { protected void disambiguate(JCas jcas, List<IdentifiedAnnotation> listNE) throws AnalysisEngineProcessException { List<IdentifiedAnnotation> listNonTrivialNE = new ArrayList<IdentifiedAnnotation>(); List<Set<String>> listConcept = new ArrayList<Set<String>>(); for (IdentifiedAnnotation ne : listNE) { FSArray concepts = ne.getOntologyConceptArr(); if (concepts != null && concepts.size() > 0) { listNonTrivialNE.add(ne); Set<String> conceptSenses = new HashSet<String>(); listConcept.add(conceptSenses); for (int i = 0; i < concepts.size(); i++) { if (concepts.get(i) != null) { String conceptId = this.getConceptId(concepts.get(i)); if (!Strings.isNullOrEmpty(conceptId)) conceptSenses.add(conceptId); } } } } if(log.isTraceEnabled()) log.trace("listConcept: " + listConcept); for (int i = 0; i < listConcept.size(); i++) { Set<String> conceptSenses = listConcept.get(i); if (conceptSenses.size() > 1) { if(log.isTraceEnabled()) log.trace("i: " + i + ", conceptSenses: " + conceptSenses); Map<String, Double> scores = new HashMap<String, Double>(); String concept = this.wsd.disambiguate(listConcept, i, null, windowSize, metric, scores, true); if(log.isTraceEnabled()) log.trace("i: " + i + ", concept: " + concept); IdentifiedAnnotation ne = listNonTrivialNE.get(i); FSArray concepts = ne.getOntologyConceptArr(); for (int j = 0; j < concepts.size(); j++) { OntologyConcept yoc = (OntologyConcept) concepts.get(j); String conceptId = this.getConceptId(yoc); if (concept == null || concept.equals(conceptId)) yoc.setDisambiguated(true); if (scores.containsKey(conceptId)) yoc.setScore(scores.get(conceptId)); } } else if (conceptSenses.size() == 1) { IdentifiedAnnotation ne = listNonTrivialNE.get(i); FSArray concepts = ne.getOntologyConceptArr(); OntologyConcept oc = (OntologyConcept) concepts.get(0); oc.setDisambiguated(true); } } }  @Override void initialize(UimaContext aContext); @Override void process(JCas jcas); }
SenseDisambiguatorAnnotator extends JCasAnnotator_ImplBase { protected void disambiguate(JCas jcas, List<IdentifiedAnnotation> listNE) throws AnalysisEngineProcessException { List<IdentifiedAnnotation> listNonTrivialNE = new ArrayList<IdentifiedAnnotation>(); List<Set<String>> listConcept = new ArrayList<Set<String>>(); for (IdentifiedAnnotation ne : listNE) { FSArray concepts = ne.getOntologyConceptArr(); if (concepts != null && concepts.size() > 0) { listNonTrivialNE.add(ne); Set<String> conceptSenses = new HashSet<String>(); listConcept.add(conceptSenses); for (int i = 0; i < concepts.size(); i++) { if (concepts.get(i) != null) { String conceptId = this.getConceptId(concepts.get(i)); if (!Strings.isNullOrEmpty(conceptId)) conceptSenses.add(conceptId); } } } } if(log.isTraceEnabled()) log.trace("listConcept: " + listConcept); for (int i = 0; i < listConcept.size(); i++) { Set<String> conceptSenses = listConcept.get(i); if (conceptSenses.size() > 1) { if(log.isTraceEnabled()) log.trace("i: " + i + ", conceptSenses: " + conceptSenses); Map<String, Double> scores = new HashMap<String, Double>(); String concept = this.wsd.disambiguate(listConcept, i, null, windowSize, metric, scores, true); if(log.isTraceEnabled()) log.trace("i: " + i + ", concept: " + concept); IdentifiedAnnotation ne = listNonTrivialNE.get(i); FSArray concepts = ne.getOntologyConceptArr(); for (int j = 0; j < concepts.size(); j++) { OntologyConcept yoc = (OntologyConcept) concepts.get(j); String conceptId = this.getConceptId(yoc); if (concept == null || concept.equals(conceptId)) yoc.setDisambiguated(true); if (scores.containsKey(conceptId)) yoc.setScore(scores.get(conceptId)); } } else if (conceptSenses.size() == 1) { IdentifiedAnnotation ne = listNonTrivialNE.get(i); FSArray concepts = ne.getOntologyConceptArr(); OntologyConcept oc = (OntologyConcept) concepts.get(0); oc.setDisambiguated(true); } } }  @Override void initialize(UimaContext aContext); @Override void process(JCas jcas);  }