[jsword-svn] r1378 - in trunk/jsword/src/main/java/org/crosswire/jsword: book examples

dmsmith at www.crosswire.org dmsmith at www.crosswire.org
Fri Jun 1 13:11:29 MST 2007


Author: dmsmith
Date: 2007-06-01 13:11:29 -0700 (Fri, 01 Jun 2007)
New Revision: 1378

Added:
   trunk/jsword/src/main/java/org/crosswire/jsword/examples/StrongsAnalysis.java
Modified:
   trunk/jsword/src/main/java/org/crosswire/jsword/book/BookFilters.java
Log:
Example code

Modified: trunk/jsword/src/main/java/org/crosswire/jsword/book/BookFilters.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/book/BookFilters.java	2007-06-01 18:47:33 UTC (rev 1377)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/book/BookFilters.java	2007-06-01 20:11:29 UTC (rev 1378)
@@ -247,9 +247,9 @@
     /**
      * Filter for books by feature
      */
-    static class BookFeatureFilter implements BookFilter
+    public static class BookFeatureFilter implements BookFilter
     {
-        BookFeatureFilter(FeatureType feature)
+        public BookFeatureFilter(FeatureType feature)
         {
             this.feature = feature;
         }

Added: trunk/jsword/src/main/java/org/crosswire/jsword/examples/StrongsAnalysis.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/examples/StrongsAnalysis.java	                        (rev 0)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/examples/StrongsAnalysis.java	2007-06-01 20:11:29 UTC (rev 1378)
@@ -0,0 +1,130 @@
+package org.crosswire.jsword.examples;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.crosswire.jsword.book.Book;
+import org.crosswire.jsword.book.BookData;
+import org.crosswire.jsword.book.BookException;
+import org.crosswire.jsword.book.BookFilters;
+import org.crosswire.jsword.book.Books;
+import org.crosswire.jsword.book.Defaults;
+import org.crosswire.jsword.book.FeatureType;
+import org.crosswire.jsword.book.OSISUtil;
+import org.crosswire.jsword.book.study.StrongsMapSet;
+import org.crosswire.jsword.book.study.StrongsNumber;
+import org.crosswire.jsword.passage.Key;
+import org.jdom.Element;
+
+public class StrongsAnalysis
+{
+
+    public StrongsAnalysis()
+    {
+        Book bible = Books.installed().getBook("KJV"); //$NON-NLS-1$
+        if (!bible.hasFeature(FeatureType.STRONGS_NUMBERS))
+        {
+            bible = null;
+            List bibles = Books.installed().getBooks(new BookFilters.BookFeatureFilter(FeatureType.STRONGS_NUMBERS));
+
+            if (bibles.size() > 0)
+            {
+                bible = (Book) bibles.get(0);
+            }
+        }
+
+        if (bible == null)
+        {
+            return;
+        }
+
+        List errors = new ArrayList();
+        StrongsMapSet sms = new StrongsMapSet();
+        analyze(sms, bible, errors, bible.getGlobalKeyList());
+    }
+
+    public void analyze(StrongsMapSet sms, Book book, List errors, Key wholeBible)
+    {
+        Key subkey = null;
+        BookData data = null;
+        Element osis = null;
+        StringBuffer buffer = new StringBuffer();
+        for (Iterator it = wholeBible.iterator(); it.hasNext(); )
+        {
+            subkey = (Key) it.next();
+            if (subkey.canHaveChildren())
+            {
+                analyze(sms, book, errors, subkey);
+            }
+            else
+            {
+                data = new BookData(book, subkey);
+                osis = null;
+
+                try
+                {
+                    osis = data.getOsis();
+                }
+                catch (BookException e)
+                {
+                    errors.add(subkey);
+                    continue;
+                }
+
+                // Do the actual indexing
+                Collection allW = OSISUtil.getDeepContent(osis, OSISUtil.OSIS_ELEMENT_W);
+                Iterator wIter = allW.iterator();
+                while (wIter.hasNext())
+                {
+                    // Clear out the buffer for re-use
+                    int len = buffer.length();
+                    if (len > 0)
+                    {
+                        buffer.delete(0, len);
+                    }
+
+                    Element wElement = (Element) wIter.next();
+                    String snAttr = wElement.getAttributeValue(OSISUtil.ATTRIBUTE_W_LEMMA);
+
+                    String content = OSISUtil.getPlainText(wElement);
+
+                    Matcher matcher = strongsNumberPattern.matcher(snAttr);
+                    while (matcher.find())
+                    {
+                        try
+                        {
+                            StrongsNumber strongsNumber = new StrongsNumber(matcher.group(1));
+                            if (buffer.length() > 0)
+                            {
+                                buffer.append(' ');
+                            }
+                            buffer.append(strongsNumber.getStrongsNumber());
+                        }
+                        catch (BookException e)
+                        {
+                            errors.add(subkey);
+                            continue;
+                        }
+                    }
+
+                    // now we can actually store the mapping
+                    sms.add(buffer.toString(), content);
+               }
+            }
+        }
+    }
+
+    /**
+     * @param args
+     */
+    public static void main(String[] args)
+    {
+        new StrongsAnalysis();
+    }
+
+    private static Pattern strongsNumberPattern = Pattern.compile("strong:([GH][0-9]+)"); //$NON-NLS-1$
+}




More information about the jsword-svn mailing list