You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by jo...@apache.org on 2011/09/06 10:52:20 UTC

svn commit: r1165567 - /incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/EntityContentProvider.java

Author: joern
Date: Tue Sep  6 08:52:20 2011
New Revision: 1165567

URL: http://svn.apache.org/viewvc?rev=1165567&view=rev
Log:
OPENNLP-235 Changed merge logic slightly

Modified:
    incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/EntityContentProvider.java

Modified: incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/EntityContentProvider.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/EntityContentProvider.java?rev=1165567&r1=1165566&r2=1165567&view=diff
==============================================================================
--- incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/EntityContentProvider.java (original)
+++ incubator/opennlp/sandbox/caseditor-opennlp-plugin/src/main/java/org/apache/opennlp/caseditor/namefinder/EntityContentProvider.java Tue Sep  6 08:52:20 2011
@@ -108,14 +108,18 @@ public class EntityContentProvider imple
         Entity newEntity = new Entity(annotation.getBegin(), annotation.getEnd(),
             annotation.getCoveredText(), null, true);
         
-        Entity potentialEntity = searchEntity(EntityContentProvider.this.potentialEntities,
+        Entity entity = searchEntity(EntityContentProvider.this.knownEntities,
             annotation.getBegin(), annotation.getEnd());
         
-        if (potentialEntity != null)
-          EntityContentProvider.this.entityList.remove(potentialEntity);
-        
-        confirmedEntities.add(newEntity);
-        EntityContentProvider.this.entityList.add(newEntity);
+        if (entity != null) {
+          // Should be updated ... potentialEntity list needs to be changed ...
+          EntityContentProvider.this.entityListViewer.remove(entity);
+          EntityContentProvider.this.knownEntities.remove(entity);
+        }
+        // else
+        // only if it was not updated, the else case
+        EntityContentProvider.this.entityListViewer.add(newEntity);
+        EntityContentProvider.this.knownEntities.add(newEntity);
       }
     }
 
@@ -137,12 +141,12 @@ public class EntityContentProvider imple
       if (fs instanceof AnnotationFS && fs.getType().getName().equals(nameTypeName)) {
         AnnotationFS annotation = (AnnotationFS) fs;
         
-        Entity confirmedEntity = searchEntity(EntityContentProvider.this.confirmedEntities,
+        Entity confirmedEntity = searchEntity(EntityContentProvider.this.knownEntities,
             annotation.getBegin(), annotation.getEnd());
         
         if (confirmedEntity != null) {
-          EntityContentProvider.this.confirmedEntities.remove(confirmedEntity);
-          EntityContentProvider.this.entityList.remove(confirmedEntity);
+          EntityContentProvider.this.knownEntities.remove(confirmedEntity);
+          EntityContentProvider.this.entityListViewer.remove(confirmedEntity);
         }
       }
       
@@ -176,20 +180,20 @@ public class EntityContentProvider imple
   private NameFinderTrigger nameFinderTrigger = new NameFinderTrigger();
   private ConfirmedEntityListener casChangeListener = new ConfirmedEntityListener();
   
-  private TableViewer entityList;
+  private TableViewer entityListViewer;
   
   private ICasDocument input;
   
   // contains all existing entity annotations and is synchronized!
   // needed by name finder to calculate updates ... 
-  private List<Entity> confirmedEntities = new ArrayList<Entity>();
-  private List<Entity> potentialEntities = new ArrayList<Entity>();
+//  private List<Entity> confirmedEntities = new ArrayList<Entity>();
+  private List<Entity> knownEntities = new ArrayList<Entity>();
   
   private String nameTypeName;
   
   EntityContentProvider(NameFinderJob nameFinder, TableViewer entityList) {
     this.nameFinder = nameFinder;
-    this.entityList = entityList;
+    this.entityListViewer = entityList;
     
     IPreferenceStore store = OpenNLPPlugin.getDefault().getPreferenceStore();
     nameTypeName = store.getString(OpenNLPPreferenceConstants.NAME_TYPE);
@@ -205,30 +209,48 @@ public class EntityContentProvider imple
             
             if (status.getSeverity() == IStatus.OK) {
               
-              List<Entity> newPotentialEntities = EntityContentProvider.this.nameFinder.getNames();
+              List<Entity> detectedEntities = EntityContentProvider.this.nameFinder.getNames();
               
-              // Remove all potential annotations from list ?! Yes! Note: We should compute a delta here in the future ...
-              EntityContentProvider.this.entityList.remove(potentialEntities.toArray());
+              // Remove all detected entities from the last run which are not detected anymore
+              for (Iterator<Entity> it = knownEntities.iterator(); it.hasNext();) {  
+                Entity entity = it.next();
+                if (searchEntity(detectedEntities, entity.getBeginIndex(),
+                    entity.getEndIndex()) == null)  {                
+                  
+                  // TODO: Create an array of entities that should be removed, much faster ...
+                  EntityContentProvider.this.entityListViewer.remove(entity);
+                  
+                  // Can safely be removed, since it can only be an un-confirmed entity
+                  it.remove();
+                }
+              }
               
-              // Then add like described below:
-              for (Entity newPotentialEntity : newPotentialEntities) {
+              // Update if entity already exist, or add it
+              for (Entity detectedEntity : detectedEntities) {
+                
+                Entity entity = searchEntity(knownEntities, detectedEntity.getBeginIndex(),
+                    detectedEntity.getEndIndex());
                 
                 // A confirmed entity already exists, update its confidence score
-                Entity confirmedEntity = searchEntity(confirmedEntities, newPotentialEntity.getBeginIndex(), newPotentialEntity.getEndIndex());
-                if (confirmedEntity != null) {
-                  confirmedEntity.setConfidence(newPotentialEntity.getConfidence());
-                  EntityContentProvider.this.entityList.refresh(confirmedEntity);
-                  continue;
+                if (entity != null) {
+                  if (entity.isConfirmed()) {
+                    entity.setConfidence(detectedEntity.getConfidence());
+                    EntityContentProvider.this.entityListViewer.refresh(entity);
+                    continue;
+                  }
+                  else {
+                    entity.setBeginIndex(detectedEntity.getBeginIndex());
+                    entity.setEndIndex(detectedEntity.getEndIndex());
+                    entity.setConfidence(detectedEntity.getConfidence());
+                    
+                    EntityContentProvider.this.entityListViewer.refresh(entity);
+                  }
+                }
+                else {
+                  EntityContentProvider.this.entityListViewer.add(detectedEntity);
+                  knownEntities.add(detectedEntity);
                 }
-                
-                // potential entity should be added!
-                // TODO: that is slow and should be done in a bulk update ... to do so remember all
-                //       and do a bulk update after the for loop
-                EntityContentProvider.this.entityList.add(newPotentialEntity);
               }
-              
-              // Remember entities for next update
-              potentialEntities = newPotentialEntities;
             }
           }
         });
@@ -269,7 +291,7 @@ public class EntityContentProvider imple
         
         AnnotationFS nameAnnotation = (AnnotationFS) nameIterator.next();
         
-        confirmedEntities.add(new Entity(nameAnnotation.getBegin(),
+        knownEntities.add(new Entity(nameAnnotation.getBegin(),
             nameAnnotation.getEnd(), nameAnnotation.getCoveredText(), null, true));
       }
       
@@ -356,7 +378,7 @@ public class EntityContentProvider imple
     // Called directly after showing the view, the
     // name finder is triggered to produce names
     // which will be added to the viewer
-    return confirmedEntities.toArray();
+    return knownEntities.toArray();
   }
   
   public void dispose() {