1
20 package org.crosswire.jsword.examples;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import org.crosswire.jsword.book.Book;
28 import org.crosswire.jsword.book.BookData;
29 import org.crosswire.jsword.book.BookException;
30 import org.crosswire.jsword.book.BookFilters;
31 import org.crosswire.jsword.book.Books;
32 import org.crosswire.jsword.book.FeatureType;
33 import org.crosswire.jsword.book.OSISUtil;
34 import org.crosswire.jsword.book.study.StrongsMapSet;
35 import org.crosswire.jsword.book.study.StrongsNumber;
36 import org.crosswire.jsword.passage.Key;
37 import org.jdom2.Content;
38 import org.jdom2.Element;
39
40
46 public class StrongsAnalysis {
47
50 public StrongsAnalysis() {
51 Book bible = Books.installed().getBook("KJV");
52 if (!bible.hasFeature(FeatureType.STRONGS_NUMBERS)) {
53 bible = null;
54 List<Book> bibles = Books.installed().getBooks(new BookFilters.BookFeatureFilter(FeatureType.STRONGS_NUMBERS));
55
56 if (!bibles.isEmpty()) {
57 bible = bibles.get(0);
58 }
59 }
60
61 if (bible == null) {
62 return;
63 }
64
65 List<Key> errors = new ArrayList<Key>();
66 StrongsMapSet sms = new StrongsMapSet();
67 analyze(sms, bible, errors, bible.getGlobalKeyList());
68 }
69
70
76 public void analyze(StrongsMapSet sms, Book book, List<Key> errors, Key wholeBible) {
77 BookData data = null;
78 Element osis = null;
79 StringBuilder buffer = new StringBuilder();
80 for (Key subkey : wholeBible) {
81 if (subkey.canHaveChildren()) {
82 analyze(sms, book, errors, subkey);
83 } else {
84 data = new BookData(book, subkey);
85 osis = null;
86
87 try {
88 osis = data.getOsisFragment();
89 } catch (BookException e) {
90 errors.add(subkey);
91 continue;
92 }
93
94 for (Content content : OSISUtil.getDeepContent(osis, OSISUtil.OSIS_ELEMENT_W)) {
96 int len = buffer.length();
98 if (len > 0) {
99 buffer.delete(0, len);
100 }
101
102 Element wElement = (Element) content;
103 String snAttr = wElement.getAttributeValue(OSISUtil.ATTRIBUTE_W_LEMMA);
104
105 String text = OSISUtil.getPlainText(wElement);
106
107 Matcher matcher = strongsNumberPattern.matcher(snAttr);
108 while (matcher.find()) {
109 StrongsNumber strongsNumber = new StrongsNumber(matcher.group(1));
110 if (strongsNumber.isValid()) {
111 if (buffer.length() > 0) {
112 buffer.append(' ');
113 }
114 buffer.append(strongsNumber.getStrongsNumber());
115 }
116 }
117
118 sms.add(buffer.toString(), text);
120 }
121 }
122 }
123 }
124
125
128 public static void main(String[] args) {
129 new StrongsAnalysis();
130 }
131
132 private static Pattern strongsNumberPattern = Pattern.compile("strong:([GH][0-9]+)");
133 }
134