473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,399 developers and data experts.

Text retrieval systems - 4A: the Library

11,448 Expert 8TB
Greetings,

the last two article parts described the design and implementation of the
text Processor which spoonfeeds paragraphs of text to the LibraryBuilder.
The latter object organizes, cleans up and stores the text being fed to it.
Finally the LibrayBuilder is able to produce a Library which is the topic
of this part of the article.

Introduction

A Library manages groups of books. Each book contains chapters which contain
paragraphs of text. The object can store itself in a compressed way given
any OutputStream. A static method can load Library objects given any Input-
Stream again. A Library can produce pieces of text, either entire books or
chapters or a single paragraph of text.

A Library can also produce lists of 'meta' data, i.e. it can produces a list
of group names of book or chapter titles; it can also produce simple statistics
such as the number of books in the library etc.

Basically a Library can produce Lists. The Lists are either lists of pure
text (Strings) or Lists of BookMarks. A BookMark represents a single paragraph
of text and has additional functionality as well.

Let's see how it's all done.

The Library object

In the previous part in the article we already saw a Library constructor being
invoked by the LibraryBuikder; here are the data parts and de constructor of
the Library object:

Expand|Select|Wrap|Line Numbers
  1. protected String    title;
  2. protected WordMap   wordMap;
  3. protected Section[] groups;    
  4. protected Section[] books;
  5. protected Section[] chapters;
  6. protected String[]  paragraphs;
  7. protected String[]  words;
  8. protected String[]  punctuation;
  9.  
  10. protected NotesMap  notesMap;
  11.  
  12. public Library(String    title,
  13.            WordMap   wordMap,
  14.            Section[] groups,
  15.            Section[] books, 
  16.            Section[] chapters,
  17.            String[]  paragraphs, 
  18.            String[]  words, 
  19.            String[]  punctuation) {
  20.  
  21.  
  22.     this.title        = title;
  23.     this.wordMap    = wordMap;
  24.  
  25.     this.groups        = groups;
  26.     this.books      = books;
  27.     this.chapters   = chapters;
  28.     this.paragraphs = paragraphs;
  29.     this.words      = words;
  30.     this.punctuation= punctuation;
  31.  
  32.     this.notesMap   = new NotesMap();
  33. }
  34.  
This is quite a bit of code but it is just simple stuff; all the parameters of
the constructor are stored in the member variables. As already discussed, there
are Sections for groups, books and chapters. The paragraphs and punctuations
are strings. There's a A String[] array for the unique words and there's the
WordMap that glues it all together. See the previous article part for an
explanation of these arrays and objects.

Also a NotesMap is initialized. a NoteMap is used for additional text for every
single paragraph in the text. Here's its definition:

Expand|Select|Wrap|Line Numbers
  1. class NotesMap extends HashMap<Integer, String> {
  2.  
  3.     private static final long serialVersionUID = 4627674436919142999L;
  4.  
  5.     private boolean modified= false;
  6.  
  7.     public void setModified(boolean modified) {this.modified= modified; }
  8.     public boolean isModified() { return modified; }
  9.  
  10.     public void put(int paragraph, String note) {
  11.  
  12.         String oldNote= get(paragraph);
  13.  
  14.         note= (note != null)?note.trim():"";
  15.         note= (note.length() == 0)?null:note;
  16.  
  17.         if (note == null) 
  18.             modified|= remove(paragraph) != null;
  19.         else {
  20.             put(paragraph, note);
  21.             modified|= !note.equals(oldNote);
  22.         }
  23.     }
  24. }
  25.  
Basically a NotesMap is a simple Map that maps Integers (the index number of
a paragraph) to a simple String that stores the annotations for the paragraph.

It applies a bit of intelligence to check whether or not the map was modified.
Empty text is considered no text at all. The modified flag can be used to check
whether or not the Library object should be saved again when it isn't needed
anymore.

The Library object implements a little convenience method so you never have
to deal with a NoteMap object yourself directly:

Expand|Select|Wrap|Line Numbers
  1. public boolean isModified() { return notesMap.isModified(); }
  2.  
The NotesMap is not a very complicated nor a very interesting class except for
one aspect: it implements the Serializable interface because its parent class
implements this interface. This is needed because this class must be saved or
loaded along with the Library object itself.

As we'll see later the Library class itself also implements the Serializable
interface.

BookMarks

As briefly mentioned in the introduction, a Library uses BookMarks to represent
one single paragraph of text. Here's its interface definition:

Expand|Select|Wrap|Line Numbers
  1. public interface BookMark {
  2.  
  3.     public String  getGroup();
  4.     public String  getBook();
  5.     public String  getChapter();
  6.     public String  getParagraph();
  7.  
  8.     public int     getGroupNumber();
  9.     public int     getBookNumber();
  10.     public int     getChapterNumber();
  11.     public int     getParagraphNumber();
  12.  
  13.     public int     getRelativeBook();
  14.     public int     getRelativeChapter();
  15.     public int     getRelativeParagraph();
  16.  
  17.     public void    putNote(String note);
  18.     public String  getNote();
  19. }
  20.  
If the Library has produced a BookMark object for you, you can query that object
for several aspects of that text, i.e. you can get the name of the chapter the
paragraph is part of; you can also get the name of the book where the paragraph
can be found and you can find the name of the group where the book is stored.
You can also get the text of the paragraph of course.

Paragraphs, chapters, books and groups all have a number, starting at zero.
There are methods available to retrieve those absolute index numbers. There
are also methods available to retrieve relative numbers for those entities.
Suppose there are two chapters in the first book and three chapters in a
second book. The absolute chapter numbers are 0, 1, 2, 3, 4 and the relative
chapter numbers are 0, 1, 0, 1, 2, i.e. the relative chapter numbers start
at zero per book. A similar reasoning applies to books and paragraphs as well.

Note that for obvious reasons there is no relative group number available.

The last two methods set and get optional additional text given the paragraph
represented by this bookmark. These two methods together with the Library
delegator isModified() method completely shield the NotesMap class from you,
i.e. you never have to deal with it yourself.

Loading and Saving a Library

If you want to load a library given an InputStream you don't have that object
available yet (it isn't loaded yet). That's why the Library class implements
two static convenience methods: one method can load a Library given a file name,
the other method takes an InputStream from which it loads a Library object.

Here they are:

Expand|Select|Wrap|Line Numbers
  1. public static Library read(String name) 
  2.             throws IOException, ClassNotFoundException {
  3.  
  4.     FileInputStream fis= null;
  5.  
  6.     try {
  7.         return read(fis= new FileInputStream(name));
  8.     }
  9.     finally {
  10.         try { fis.close(); } catch (IOException ioe) { }
  11.     }
  12. }
  13.  
  14. public static Library read(InputStream is) 
  15.             throws IOException, ClassNotFoundException {
  16.  
  17.     return (Library)new ObjectInputStream(new InflaterInputStream(is)).
  18.             readObject();
  19. }
  20.  
The first method just tries to open a FileInputStream given a file name and
delegates the hard part to the second method. The second method wraps two
other InputStreams around the given InputStream. The first wrapper/decorator
inflates the input stream. The inflated input is used by the ObjectInputStream
wrapper that does the actual object construction. Both methods can throw simple
IOExceptios when some reading fails and the can throw ClassNotFoundExceptions
if the InputStream didn't make any sense.

The second method doesn't close the stream passed to it because it doesn't own
it, i.e. maybe the caller of this method has different plans for this stream,
and closing it will prohibit it. The first method had created that OutputStrem
itself, so it owns it and so it closes the stream itself.

Of course you can only save a Library object if you have one. Therefore the
write methods are not static methods. The logic of those methods don't differ
much from the static load methods. Here they are:

Expand|Select|Wrap|Line Numbers
  1. public void write(String name) throws IOException {
  2.  
  3.     FileOutputStream fos= null;
  4.  
  5.     try {
  6.         fos= new FileOutputStream(name);
  7.         write(fos);
  8.     }
  9.     finally {
  10.         try { fos.close(); } catch (IOException ioe) { }
  11.     }
  12. }
  13.  
  14. public void write(OutputStream os) throws IOException {
  15.  
  16.     DeflaterOutputStream dos;
  17.     ObjectOutputStream oos= new ObjectOutputStream(dos= new DeflaterOutputStream(os));
  18.  
  19.     oos.writeObject(this);
  20.     oos.flush();
  21.     dos.finish();
  22. }
  23.  
The first method just creates a FileOutputStream given a file name and delegates
the hard work to the second method. Compare this with the static load method.
The other method wraps a DeflaterOutputStream around the OutputStream parameter.
This DeflaterOutputStream is wrapped in an ObjectOutputStream again and the
entire Library is written.

Also observe that the second method doesn't close the stream for identical
reasons as explained for the load methods (compare the logic again).

I noticed that this article part is getting quite long again, so I see you below in
the sequel of this article part.

kind regards,

Jos
Jul 29 '07 #1
0 4058

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

Similar topics

0
by: SoftComplete Development | last post by:
AlphaTIX is a powerful, fast, scalable and easy to use Full Text Indexing and Retrieval library that will completely satisfy your application's indexing and retrieval needs. AlphaTIX indexing...
15
by: ritesh | last post by:
Hi, I'm working on a piece of code that - 1. has a list of text files 2. and needs to search for a particular expression in these files (this is being done on Linux using gcc 3.4.2) ...
0
by: JosAH | last post by:
Greetings, Introduction At the end of the last Compiler article part I stated that I wanted to write about text processing. I had no idea what exactly to talk about; until my wife commanded...
0
by: JosAH | last post by:
Greetings, Introduction Last week I started thinking about a text processing facility. I already found a substantial amount of text: a King James version of the bible. I'm going to use that...
0
by: JosAH | last post by:
Greetings, Introduction 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 ...
0
by: JosAH | last post by:
Greetings, Introduction At this moment we have a TextProcessor, a LibraryBuilder as well as the Library itself. As you read last week a Library is capable of producing pieces of text in a...
1
by: JosAH | last post by:
Greetings, Introduction This week we start building Query objects. A query can retrieve portions of text from a Library. I don't want users to build queries by themselves, because users make...
0
by: JosAH | last post by:
Greetings, Introduction Last week I was a bit too busy to cook up this part of the article series; sorry for that. This article part wraps up the Text Processing article series. The ...
0
by: JosAH | last post by:
Greetings, welcome back; above we discussed the peripherals of the Library class: loading and saving such an instantiation of it, the BookMark interface and then some. This part of the article...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.