468,321 Members | 1,820 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Text retrieval systems - 3A: the Library Builder

11,448 Expert 8TB


Before we start designing and implementing our text builder class(es), I'd like
to mention a reply by Prometheuzz: he had a Dutch version of the entire bible
available, including those apocryphical books. I downloaded the entire shebang,
hacked my King James text processor a bit and now I have two bibles available:
the English 'King James' version and the Dutch 'Staten Vertaling' version.

The Dutch version was even bigger than the King James version, so there :-P

Thanks to Prometheuzz for that valuable piece of text; I really appreciate it.
The Dutch version had less (read: no) inconsistencies while my late neighbour
Dirk's version was a mess here and there. Most of the raw parsing text trouble
happened in Dirk's version. Just a bit of rewriting and quick hacking was
enough to be able to process the Dutch version supplied by Prometheuzz.

The LibraryBuilder

We've got work to do: here's the LibraryBuilder interface again:

Expand|Select|Wrap|Line Numbers
  1. public interface LibraryBuilder {
  3.     public void preProcess();
  4.     public void postProcess();
  6.     public void setTitle(String title);
  8.     public void buildGroup(String group);
  9.     public void buildParagraph(String book, String chapter, int para, String text) throws IOException;
  11.     public Library build();
  12. }
The AbstractBuilder class implements all of this interface and leaves a few
abstract protected methods to be implemented by the specific builders such as
the KJBuilder (for the King James bible version) and the SVBuilder (for the
Staten Vertaling version).

The AbstractBuilder doesn't do all the work itself, i.e. it delegates a lot of
the work to smaller and simpler builders.

Here's the simples builder of them all: the StringsBuilder:

Expand|Select|Wrap|Line Numbers
  1. class StringsBuilder extends ArrayList<String> {
  3.     private static final long serialVersionUID = 4709552086518644567L;
  5.     String[] build() { return this.toArray(new String[size()]); }
  6. }
It is simply an implementation of the List<String> interface and it can build
a String[] for us. That's all it does. Remember that the Library itself just
uses a bunch of arrays for the entire text. Arrays can't be resized; that's
one of the reasons I prefer builders that manipulate more flexible, but larger
data structures (classes) while at the end all the arrays are produced.

Here's another simple builder; it builds a single section:

Expand|Select|Wrap|Line Numbers
  1. package text.builders;
  3. import text.library.Section;
  5. class SectionBuilder {
  7.     private String name;
  8.     private int index;
  9.     private int total;
  11.     SectionBuilder(String name, int index) {
  13.         this.name = name;
  14.         this.index= index;
  15.     }
  17.     void setTotal(int end) { this.total= end-index; }
  19.     String getName() { return name; }
  21.     Section build() { return new Section(name, index, total); }
  22. }
A Section can be either a section in a group, a book or a chapter. The 'index'
member refers to the corresponding section in an arrray of groups, books or
chapters. The 'total' member is the total number of sections in that particular
array. Also see the previous part of this article for examples of Sections.

The above code can build a Section. The next little builder builds an array
of Sections for us; here it is:

Expand|Select|Wrap|Line Numbers
  1. class SectionsBuilder extends ArrayList<SectionBuilder> {
  3.     private static final long serialVersionUID = 1476840322184839715L;
  5.     boolean addSection(String name, int index, boolean add) {
  7.         int n= size();
  8.         SectionBuilder s= (n == 0)?null:get(n-1);
  10.         if (add= (add || s == null || !s.getName().equals(name))) {
  11.             add(new SectionBuilder(name, index));
  12.             if (s != null) s.setTotal(index);
  13.         }    
  15.         return add;
  16.     }
  18.     void postProcess(int total) {
  20.         if (size() > 0)
  21.             get(size()-1).setTotal(total);
  22.     }
  24.     Section[] build() { 
  26.         int n= size();
  27.         Section[] sections= new Section[n];
  29.         for (int i= 0; i < n; i++)
  30.             sections[i]= this.get(i).build();
  32.         return sections;
  33.     }
  34. }
The SectionsBuilder is a List of SectionBuilders (not the different plurals).
When a new Section is about to start, it figures out how many 'things' are
in the previous section. See the addSection() method for that. The boolean
'add' variable (if true) forces a new Section to be build no matter what.
When this variable is false a new Section needs to be built if the name of
the current Section differs from the previous Section, or, if the Section to
be built is the very first Section of them all.

Actually this builder doesn't keep track of Sections at all: it handles
SectionBuilders. The build() method of this builder builds the actual Sections:
it invokes the build() method on every SectionBuilder in its list which in turn
build a single Section each.


Let's pause for a minute here: we can build single Sections (using the
SectionBuilder) and we can build arrays of Sections (using the SectionsBuilder).
We can also build arrays of Strings using the StringsBuilder.

But we need more: we need to know in which paragraphs a word occurs. In other
words (sic) we need a mapping between a single word and list of indexes in the
paragraphs List. A single paragraph is a String representing the indexes of
the single words and punctuation characters again. (see the previous article
part for how paragraphs are compressed).

More work to do ...

More builders

Here is a WordBuilder; it keeps track of the paragraph numbers in which a word
occurs. It doesn't care which word though; that has been figured out somewhere
else. It does know that it's the 'index'-th word in the entire text. Here's
the implementation:

Expand|Select|Wrap|Line Numbers
  1. class WordBuilder {
  3.     private int index;
  4.     private List<Integer> paragraphs= new ArrayList<Integer>();
  6.     WordBuilder(int paragraph) {
  8.         this.index= paragraph;
  9.     }
  11.     int getIndex() { return index; }
  13.     void addParagraph(int paragraph) { paragraphs.add(paragraph); }
  15.     int[] build() {
  17.         int n= paragraphs.size();
  18.         int array[]= new int[n];
  20.         for (int i= 0; i < n; i++) array[i]= paragraphs.get(i);
  22.         return array;
  23.     }
  24. }
It basically keeps track of a list of paragraph numbers in which this particular
word occurs and it can build an array of integers, the indexes of the paragraphs
in which the word occurs again. It's not particularly rocket science.

The last builder maps words to WordBuilders; it doesn't come as a surprise that
this builder is named WordMapBuilder; here it is:

Expand|Select|Wrap|Line Numbers
  1. class WordMapBuilder extends HashMap<String, WordBuilder> {
  3.     private static final long serialVersionUID = -5282145470567550534L;
  5.     WordMap build() {
  7.         WordMap map= new WordMap();
  9.         for (Map.Entry<String, WordBuilder> entry : entrySet()) 
  10.             map.put(entry.getKey(), entry.getValue().build());
  12.         return map;
  13.     }
  14. }
It maps a word (a String) to a WordBuilder (see above) and it can produce
a WordMap. We'll see that thing later but I think you can guess what it is: it
maps Strings (words) to Integer[]s which are the paragraph numbers in which the
words occur.


We have the constituents for our LibraryBuilder implementation now, let's see
what this implementation is all about. This class is an abstract class because
it leaves a few simple methods to an extending class:

Expand|Select|Wrap|Line Numbers
  1. protected abstract String clean(String book, String chapter, 
  2.                 int paragraph, String text);
  3. protected abstract Collection<String> getNoise();
The first method is supposed to clean up a paragraph text. Most of it has been
cleaned up already in a general way, but a specific builder may want to clean
up the text even a bit more.

In Dirk's King James version some of the words had curly braces and some other
braces around them. I don't want those curly braces, so my KJBuilder's clean()
method implementation looks like this:

Expand|Select|Wrap|Line Numbers
  1. protected String clean(String book, String chapter, 
  2.             int paragraph, String text) {
  4.     text= text.replaceAll("[({\\[\\]})]", "");
  6.     return text.replaceAll("[^\\x20-\\x7e]", "");
  7. }
I clean up the text a bit more by removing all the non 7 bits ASCII characters
from the text (remember those 'y's with dots on top from the first article part?
This gets rid of them).

The second method is supposed to return a Collection of Strings that are not
supposed to be indexed at all. Think of words such as 'the', 'a', 'is' etc.
The AbstractBuilder does all the work, it just needs to know *which* words are
supposed to be excluded from the WordMap (see above).

Here is how the KJBuilder (extending the AbstractBuilder) returns its noise words:

Expand|Select|Wrap|Line Numbers
  1. private static final Set<String> noise= new HashSet<String>();
  3. static { 
  4.     noise.addAll(Arrays.asList(new String[] {
  6.         "O", "s",
  7.         "The", "the", 
  8.         "A", "a", "An", "an", 
  9.         "Of", "of", "By", "by", "On", "on",
  10.         "Am", "am", "Are", "are", "Is", "is", "Be", "be",
  11.         "Had", "had", "Has", "has", "Have", "have",
  12.         "At", "at", "As", "as", "In", "in", 
  13.         "Do", "do",
  15.     }));
  16. }
  18. protected Collection<String> getNoise() {
  20.     return noise;
  21. }
A HashSet is a Set and a Set is a Collection. The Arrays.asList() method can
build a List out of an array and a Set can take a List which is also a
Collection as a constructor parameter and that's what is finally returned by
the getNoise() method.

This was enough typing for today; I go get myself some coffee. I hope to see
you in the next fragment of this article part below.

kind regards,


Part B
Jul 21 '07 #1
0 3445

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by hplloyd | last post: by
reply views Thread by SoftComplete Development | last post: by
1 post views Thread by JosAH | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.