Class StringTiler

java.lang.Object
  extended by StringTiler

public class StringTiler
extends java.lang.Object

Helper class for QuestionAnswerer.

A StringTiler "tiles" a string: decomposing it into longer and longer sequences of words. For example, the tiles in the string "actually mauna kea in hawaii" are "actually", "actually mauna", "actually mauna kea", "actually mauna kea in", and "actually mauna kea in hawaii".

Note that the use of a TextCleaner makes the StringTiler MUCH simpler to implement. Once the input text is cleaned, each word is separated from the next by exactly one space character ' '.


Field Summary
private  java.lang.String myText
          The text to operate on.
private static TextCleaner ourCleaner
          A TextCleaner to use for putting input text into a simple, canonical form.
 
Constructor Summary
StringTiler(java.lang.String fullText)
          Create a StringTiler that uses as its text the given text.
StringTiler(java.lang.String originalText, java.lang.String targetText)
          Create a StringTiler that uses as its text everything AFTER the given targetText within the given originalText.
 
Method Summary
 int getNumTiles()
          Get the number of tiles (equal to the number of words) in this tiler's text.
 java.lang.String getTileOfWordLength(int wordLength)
          Construct a tile with the given number of words in it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

myText

private java.lang.String myText
The text to operate on.


ourCleaner

private static final TextCleaner ourCleaner
A TextCleaner to use for putting input text into a simple, canonical form.

Constructor Detail

StringTiler

public StringTiler(java.lang.String fullText)
Create a StringTiler that uses as its text the given text. (Note that the fullText parameter is cleaned before use.)

Parameters:
fullText - the text to tile

StringTiler

public StringTiler(java.lang.String originalText,
                   java.lang.String targetText)
Create a StringTiler that uses as its text everything AFTER the given targetText within the given originalText.

If the target text is not found, the StringTiler uses the empty string as its text (and therefore returns no tiles). If the target text IS found, the StringTiler uses everything in the original text after the target text as its text.

For example, if the text is "question anyway the tallest mountain in the world is mount everest in the himalayas" and the targetText is "the tallest mountain in the world is", the StringTiler would take as its text "mount everest in the himalayas". (Note that any space after the target text should be dropped; recleaning with the text cleaner will accomplish this.)

(Note that both text parameters are run through a TextCleaner before use.)

Parameters:
originalText - the entire text to look through (non-null)
targetText - the targetText after which to start building tiles (non-null)
Method Detail

getNumTiles

public int getNumTiles()
Get the number of tiles (equal to the number of words) in this tiler's text.

Hints: First, handle the empty string as a separate case. It has zero tiles in it. Second, try calculating by hand the difference between the lengths of "actually mauna kea" and "actually mauna kea" with all of its spaces removed. How does that value relate to the number of words in the string? Does your formula work for text with just one word? With five words? With any number of words?

Returns:
the number of tiles in the text

getTileOfWordLength

public java.lang.String getTileOfWordLength(int wordLength)
Construct a tile with the given number of words in it. For example, if the text of this tiler is "actually mauna kea in hawaii", the tile of length 3 is "actually mauna kea".

Hint: First, handle a request for a tile whose length is the same as getNumTiles specially (it's the whole text!). Second, if the tile has 2 words, it must stretch from the start of the string past the first space character (which separates the first two words) and up to the second space character (which separates the second and third words). Can you use a loop to find the right space at which to cut off the tile?

Parameters:
wordLength - the length (in words) of the tile to construct (must be between 1 and getNumTiles).
Returns:
a tile of the given length