Revision: 15245
http://gate.svn.sourceforge.net/gate/?rev=15245&view=rev
Author: markagreenwood
Date: 2012-01-28 16:31:41 +0000 (Sat, 28 Jan 2012)
Log Message:
-----------
code simplification and more variable renaming as I still try and figure out
how this thing works
Modified Paths:
--------------
gate/trunk/src/com/ontotext/gate/gazetteer/HashGazetteer.java
gate/trunk/src/com/ontotext/gate/gazetteer/TestHashGazetteer.java
Modified: gate/trunk/src/com/ontotext/gate/gazetteer/HashGazetteer.java
===================================================================
--- gate/trunk/src/com/ontotext/gate/gazetteer/HashGazetteer.java
2012-01-28 14:43:25 UTC (rev 15244)
+++ gate/trunk/src/com/ontotext/gate/gazetteer/HashGazetteer.java
2012-01-28 16:31:41 UTC (rev 15245)
@@ -38,13 +38,10 @@
private Map<String, List<Lookup>> mapsList[];
- private int mapsListSize;
-
private AnnotationSet annotationSet = null;
@SuppressWarnings("unchecked")
public Resource init() throws ResourceInstantiationException {
- System.out.println("HashGazetteer is being initialized!");
if(listsURL == null)
throw new ResourceInstantiationException(
"No URL provided for gazetteer creation!");
@@ -56,7 +53,6 @@
definition.load();
int i = definition.size();
listsByNode = definition.loadLists();
- mapsListSize = mapsList.length;
categoryList = new ArrayList<Lookup>(i + 1);
Iterator<LinearNode> iterator = definition.iterator();
int j = 0;
@@ -166,17 +162,15 @@
public boolean add(String word, Lookup lookup1) {
if(!super.caseSensitive.booleanValue()) {
- String s1 = word.toUpperCase();
- if(!s1.equals(word)) add(s1, lookup1);
+ word = word.toUpperCase();
}
+
String s2 = removeTrailingSymbols(word);
if(!s2.equals(word)) add(s2, lookup1);
String s3 = word + " ";
List<Lookup> arraylist = null;
- int j = 0;
- //s3.trim(); //doesn't do anything so comment it out
- j = s3.length();
+ int j = s3.length();
boolean prevIsLetter = false;
boolean prevIsDigit = false;
@@ -196,7 +190,10 @@
|| prevIsLetter && currIsLetter && prevIsLowercase
&& !currIsLowercase || prevIsDigit
&& (currIsLetter || currIsSymbol || currIsWhitespace);
+
+ //if we are on the last character
if(k + 1 == j) flag18 = true;
+
if(flag18) {
s4 = normalizeWhitespace(s3.substring(0, k));
int i = s4.length();
@@ -230,7 +227,7 @@
Set<Lookup> set = null;
String s1 = normalizeWhitespace(s);
int i = s1.length();
- if(mapsListSize < i) return set;
+ if(mapsList.length < i) return set;
Map<String, List<Lookup>> hashmap = (HashMap<String,
List<Lookup>>)mapsList[i];
if(hashmap == null) {
return set;
@@ -242,7 +239,7 @@
}
private boolean annotate(String word, int i, int documentPosition, int
wordLength) {
- if(wordLength >= mapsListSize) return false;
+ if(wordLength >= mapsList.length) return false;
Map<String, List<Lookup>> hashmap = mapsList[wordLength];
if(hashmap == null) return false;
if(!hashmap.containsKey(word)) return false;
@@ -285,9 +282,9 @@
*/
public boolean remove(String s) {
- String s1 = a(s, true);
+ String s1 = a(s);
int i = s1.length();
- if(i > mapsListSize) return false;
+ if(i > mapsList.length) return false;
Map<String, List<Lookup>> hashmap = mapsList[i];
if(hashmap == null) return false;
if(hashmap.containsKey(s1)) {
@@ -350,35 +347,37 @@
return stringbuffer.toString();
}
- private String a(String s, boolean flag) {
+ private String a(String s) {
StringBuffer stringbuffer = new StringBuffer();
- boolean flag1 = true;
+ boolean allLettersUppercase = true;
s = s.trim();
char ac[] = s.toCharArray();
int i = s.length();
if(i <= 1) return s;
- char c = ac[0];
- stringbuffer.append(c);
+
+ char firstCharacter = ac[0];
+ stringbuffer.append(firstCharacter);
boolean flag2 = true;
- boolean prevIsLetter = Character.isLetter(c);
- boolean prevNotLetterOrDigit = !Character.isLetterOrDigit(c);
+ boolean prevIsLetter = Character.isLetter(firstCharacter);
+ boolean prevNotLetterOrDigit = !Character.isLetterOrDigit(firstCharacter);
boolean flag10 = true;
char c2 = 'p';
for(int j = 1; j < i; j++) {
- char c1 = ac[j];
- boolean currNotLetterOrDigit = !Character.isLetterOrDigit(c1);
- boolean currIsWhitespace = Character.isWhitespace(c1);
- boolean currIsLetter = Character.isLetter(c1);
- boolean currIsDigit = Character.isDigit(c1);
- if(j > 0 && flag2) {
+ char currentCharacter = ac[j];
+ boolean currNotLetterOrDigit =
!Character.isLetterOrDigit(currentCharacter);
+ boolean currIsWhitespace = Character.isWhitespace(currentCharacter);
+ boolean currIsLetter = Character.isLetter(currentCharacter);
+ boolean currIsDigit = Character.isDigit(currentCharacter);
+
+ if(flag2) {
if(prevNotLetterOrDigit && currIsWhitespace) continue;
flag2 = prevIsLetter && currNotLetterOrDigit || prevNotLetterOrDigit
&& currIsLetter;
if(currNotLetterOrDigit) {
- if(c2 == 'p') c2 = c1;
- flag2 = flag10 = c2 == c1;
+ if(c2 == 'p') c2 = currentCharacter;
+ flag2 = flag10 = c2 == currentCharacter;
}
if(j > 2 && !flag2 && stringbuffer.length() > 0) {
char c3 = stringbuffer.charAt(stringbuffer.length() - 1);
@@ -386,18 +385,20 @@
stringbuffer.append(Character.toLowerCase(c3));
}
}
+
if(currIsLetter || currIsDigit) {
- if(flag && currIsLetter) flag1 &= Character.isUpperCase(c1);
- if(!flag10) c1 = Character.toLowerCase(c1);
- stringbuffer.append(c1);
+ if(currIsLetter) allLettersUppercase &=
Character.isUpperCase(currentCharacter);
+ if(!flag10) currentCharacter = Character.toLowerCase(currentCharacter);
+ stringbuffer.append(currentCharacter);
}
else if(!flag2) flag10 = false;
+
prevIsLetter = currIsLetter;
prevNotLetterOrDigit = currNotLetterOrDigit;
}
String s1 = stringbuffer.toString();
- if(flag && flag1) s1 = s1.toUpperCase();
+ if(allLettersUppercase) s1 = s1.toUpperCase();
return s1;
}
@@ -434,7 +435,6 @@
for(; iterator.hasNext(); add(normalisedWord, lookup1)) {
String word = iterator.next().toString();
- //s4.trim(); //doesn't do anything so comment it out
int wordLength = word.length();
for(int j = 0; j < wordLength; j++) {
if(j + 1 != wordLength && !Character.isWhitespace(word.charAt(j)))
continue;
Modified: gate/trunk/src/com/ontotext/gate/gazetteer/TestHashGazetteer.java
===================================================================
--- gate/trunk/src/com/ontotext/gate/gazetteer/TestHashGazetteer.java
2012-01-28 14:43:25 UTC (rev 15244)
+++ gate/trunk/src/com/ontotext/gate/gazetteer/TestHashGazetteer.java
2012-01-28 16:31:41 UTC (rev 15245)
@@ -9,18 +9,22 @@
* $Id$
*/
-import java.util.*;
-import java.io.*;
-import java.net.*;
-import java.beans.*;
-import java.lang.reflect.*;
-import junit.framework.*;
-
-import gate.*;
-import gate.util.*;
-import gate.creole.*;
+import gate.Annotation;
+import gate.AnnotationSet;
+import gate.Document;
+import gate.Factory;
+import gate.FeatureMap;
+import gate.Gate;
import gate.corpora.TestDocument;
+import gate.util.Strings;
+import java.net.URL;
+import java.util.Iterator;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
/**
* Tests the HashGazetteer.
*/
@@ -44,6 +48,8 @@
Document doc = Factory.newDocument(
new URL(TestDocument.getTestServerName() + "tests/doc0.html")
);
+
+ System.out.println(doc.getFeatures().get("gate.SourceURL"));
//create a default gazetteer
FeatureMap params = Factory.newFeatureMap();
@@ -55,25 +61,16 @@
gaz.setAnnotationSetName(GAZ_AS);
gaz.execute();
-// dumpAnnotationSet(doc.getAnnotations(Gaz_AS));
-
assertTrue("the Annotation set resulting of the execution of the OntoText "
+"Natural Gazetteer is empty."
,!doc.getAnnotations(GAZ_AS).isEmpty());
+
//check whether the annotations are as expected
+ assertTrue("Found in "+ doc.getSourceUrl().getFile()+ " "+
+ doc.getAnnotations(GAZ_AS).size() +
+ " Lookup annotations, instead of the expected 70.",
+ doc.getAnnotations(GAZ_AS).size()== 70);
-
-// assertTrue("Found in "+ doc.getSourceUrl().getFile()+ " "+
-// doc.getAnnotations(GAZ_AS).size() +
-// " Lookup annotations, instead of the expected 53.",
-// doc.getAnnotations(GAZ_AS).size()== 53);
-
-/*very complex compare */
-// assertTrue("the Annotation set resulting from the OntoText Natural
Gazetteer "
-// +"is not exactly the same as expected. Possible reasons: change in
the test file "
-// +"doc0.html or malfunctioning of the gazetteer"
-// ,EqualAnnotationSets(doc.getAnnotations(GAZ_AS)));
-
} // testHashGazetteer();
/** Test suite routine for the test runner */
@@ -92,157 +89,4 @@
e.printStackTrace();
}
} // main
-
-
- /** dumps the annotation set to system ouput
- * @param marks an annotation set
- */
- private void dumpAnnotationSet(AnnotationSet marks) {
- if (marks != null) {
- Iterator<Annotation> iter = marks.iterator();
- while(iter.hasNext()) {
- Annotation lookup = iter.next();
- FeatureMap lookFeats = lookup.getFeatures();
- String majorStr = (String) lookFeats.get("majorType");
- String minorStr = (String) lookFeats.get("minorType");
- String position = " "+lookup.getStartNode().getOffset()+"-"+
lookup.getEndNode().getOffset();
- System.out.println(position+":"+majorStr + "." + minorStr +
Strings.getNl());
- }
- } //if
- } // void dumpAnnotationSet(AnnotationSet set)
-
- /** Tests whether the annotation set has the same elements
- * as statet in DESIRED_ANNOTATIONS
- * @param marks an annotation set
- * @return true if they match, false otherwise.
- */
- private boolean EqualAnnotationSets(AnnotationSet marks) {
- boolean areEqual = true;
- String currentMark = null;
- int index = 0;
-
- areEqual = areEqual && (marks.size() == DESIRED_ANNOTATIONS.length);
-
- if (marks != null) {
- Iterator<Annotation> iter = marks.iterator();
-
- while(iter.hasNext() & areEqual) {
- Annotation lookup = iter.next();
- FeatureMap lookFeats = lookup.getFeatures();
- String majorStr = (String) lookFeats.get("majorType");
- String minorStr = (String) lookFeats.get("minorType");
- String position = ""+lookup.getStartNode().getOffset()+"-"+
lookup.getEndNode().getOffset();
-
- currentMark = position+":"+majorStr + "." + minorStr;
- areEqual = areEqual &&
(currentMark.equals(DESIRED_ANNOTATIONS[index]));
- index++;
- }
- } else {
- areEqual = false;
- } // else
-
-
- return areEqual;
- } // boolean testGazAnnotationSet(AnnotationSet marks) {
-
- private static String [] DESIRED_ANNOTATIONS =
- {
- "1067-1072:date_unit.null",
-
- "1033-1038:person_first.male",
-
- "1029-1032:title.male",
-
- "1014-1023:jobtitle.null",
-
- "1008-1013:jobtitle.null",
-
- "995-1003:jobtitle.null",
-
- "846-853:number.null",
-
- "814-822:date.month",
-
- "799-802:title.male",
-
- "765-768:org_ending.null",
-
- "765-768:cdg.null",
-
- "753-764:org_key.null",
-
- "738-741:org_ending.null",
-
- "738-741:cdg.null",
-
- "723-737:org_key.null",
-
- "713-722:organization.company",
-
- "696-701:cdg.null",
-
- "677-686:organization.company",
-
- "664-673:jobtitle.null",
-
- "658-663:jobtitle.null",
-
- "645-653:jobtitle.null",
-
- "636-641:date_unit.null",
-
- "614-616:stop.null",
-
- "603-613:organization.company",
-
- "582-587:cdg.null",
-
- "555-576:organization.company",
-
- "546-549:org_ending.null",
-
- "546-549:cdg.null",
-
- "529-538:jobtitle.null",
-
- "523-528:jobtitle.null",
-
- "510-518:jobtitle.null",
-
- "484-487:title.male",
-
- "465-473:jobtitle.null",
-
- "424-429:person_first.male",
-
- "414-420:person_first.male",
-
- "394-399:date_unit.null",
-
- "379-382:title.male",
-
- "350-373:jobtitle.null",
-
- "337-345:jobtitle.null",
-
- "320-325:person_first.male",
-
- "295-298:org_ending.null",
-
- "295-298:cdg.null",
-
- "274-277:location.province",
-
- "265-272:location.city",
-
- "182-189:cdg.null",
-
- "161-165:person_first.female",
-
- "100-115:title.civilian",
-
- "100-115:jobtitle.null",
-
- "87-95:title.civilian"
- }; // private static String [] DESIRED_ANNOTATIONS
} // TestHashGazetteer
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs