Compsci 100, Fall 2009, Recitation, Sept 4

name: ___________________________________

name: ___________________________________

name: ___________________________________

pretty printed code, browsable and snarfable using the site in Eclipse.

Background on KWIC

A key word in context or KWIC index provides a list of all the key words in a document in the context in which the word occur. The list is alphabetized by key word. The program you'll look at here is pretty small, but in classic paper on software engineering from 1972 David Parnas writes about KWIC as a way of illustrating modularization and says:
This is a small system ... such a system could be produced by a good programmer within a week or two.

Today, with more memory and better programming abstractions it doesn't take a week to do. As an example of a KWIC, some occurrences of love and hate from Shakespeare's Romeo and Juliet are shown below, the formatting is meant to make it easier to see the context and the key word --- there are two key words: love and hate.

                       talk of peace? I hate the word As I 
                          the word As I hate hell, all Montagues, and 
                  better ended by their hate Than death prorogued, wanting 
                 lives, By doing damned hate upon thyself? Why railest 
                  But thankful even for hate that is meant love. 

            nature. For this drivelling love is like a great 
                   my love? Nurse. Your love says, like an honest 
             oddly thou repliest! 'Your love says, like an honest 
                      by the which your love Must climb a bird's 

You'll be looking at code in a new class ContextModel that finds key words in context. The general outline of how the context is generated includes these steps:

How the View communicates with the Model

Using an MVC or model-view-controller architecture we connect the view (which is what the user interacts with) with the model, which is what does the computation relevant to the task at hand. The model then communicates the results of the computation back to the view to display to the user.

TODO Questions

  1. The user enters a word in the GUI and the word is passed to the model's process method. Why is the map's size used to determine if the map should be filled? When does the map need to be filled and when is the step of filling the map unnecessary?
  2. If the code shown in process here is removed what will happen when the user types a word that doesn't appear in the file (or has too few letters)? if (!myMap.containsKey(word)) { this.showViewsError("word " + word + " not found"); return; }
  3. What is the purpose of the continue statement in building the context string? When will it be executed? What happens if the if/continue is removed? for (int i = index - CONTEXT_SIZE; i <= index + CONTEXT_SIZE; i++) { if (i < 0 || i >= myWords.length) continue; build.append(myWords[i] + " "); }

  4. In the process method a StringBuilder object is used with the method append. A String could be used instead, either with append or with the overloaded concatenation/append operator + to append strings. Using a String generates exactly the same output as a StringBuilder -- what's a possible/likely reason for using StringBuilder rather than String?
  5. Explain the purpose of the code below in the private method fillMap. if (all == null){ all = new ArrayList<Integer>(); myMap.put(s, all); } all.add(k);
  6. How would you make the keyword appear in uppercase in the GUI by modifying code in the model?
  7. How would you change the model to treat the keywords "Love" and "love" as the same, i.e., for the code to be case-insensitive when examining keywords?
  8. If you want to print a list of every keyword in context, you could modify the code in the model to iterate over the keys in the map -- i.e., to find the context for every keyword. Where would you write this code to see if it works? What would the modifications be? Why would using a TreeMap in the constructor of ContextModel be better than using a HashMap in printing every key word in its context?
  9. In the current program a key word that occurs in its own context causes a viewing bug in the output. The text below is generated for the key word they. The fourth and fifth lines below show what happens when they occurs as part of its context. The fifth line isn't displayed properly because it's aligned on the first occurrence of the key word rather than properly on the second occurrence.

                No.-Wall-street. At one end they looked upon the white 
                    the Directory. In truth they were nicknames, mutually conferred 
                     and Post Office. Also, they sent Ginger Nut very 
                         these cakes, as if they were mere wafers-indeed they 
                                            they were mere wafers-indeed they sell them at the 
                   scriveners in an office, they assist each other in 
                      grin. ``You hear what they say,'' said I, turning 
                      are so called because they contain ginger as one 
    Explain what part of the code causes the bug to happen. Identify and describe the code that causes this problem. Describe an idea of how to fix the problem at a high-level. You don't need to write the code that fixes the problem, you need to describe what the code is, where it fits, etc. using descriptions rather than code.