“Have you seen my wig around?”

Jane Says

This week’s A2Z assignment was as follows:

Make a program that creatively transforms or performs analysis on a text using regular expressions. The program should take its input from the keyboard and send its to the screen (or redirect to/from a file). Your program might (a) filter lines from the input, based on whether they match a pattern; (b) match and display certain portions of each line; (c) replace certain portions of each line with new text; or (d) any combination of the above.

Sample ideas: Replace all words in a text of a certain length with a random word; find telephone numbers or e-mail addresses in a text; locate words within a word list that will have a certain score in Scrabble; etc.

Bonus challenge 1: Use one or more features of regular expression syntax that we didn’t discuss in class. Reference here.

Bonus challenge 2: Use one or more features of the Pattern or Matcher class that we didn’t discuss in class. Of particular interest: regex flags (CASE_INSENSITIVE, MULTILINE), “back references” in replaceAll. Matcher class reference here.

So, what I made is a program that tries to find the proper names in the input text (my preferred input being the first scene of Pride and Prejudice) and replace them with names of people in our class. For my purposes, a proper name is any capitalized word that (a) follows an honorific, such as Mr., Mrs., Sir, Lady, or Miss, or (b) does not immediately follow a carriage return or chunk of terminal punctuation (i.e., a period, exclamation point, or question mark, with or without a closing quotation mark thereafter), and (c) is neither a day of the week, a month of the year, nor the name of a holiday (though the only holiday it’s really looking for is Michaelmas, as that’s what appears in my P&P extract).

I tried for hours to do this in a compact way, by looking for the absence of certain words or characters (terminal punctuation, honorifics, days of the week, holidays) combined with the presence of other patterns, but it just would not fly. So the result requires a lot of intermediate steps, to avoid changing words that have been identified as not likely to be names. In a vain attempt to make testing my regular expressions quicker, I worked them out in BBEdit first, using the grep option in the search and replace panel. Saved me from having to recompile the thing fifty thousand times. And the expressions worked in BBEdit, for the most part. They do not work so well translated into Java, unfortunately—the success rate at finding names is much lower, and after ten straight hours, I just didn’t have the patience to troubleshoot it any more. But these glitches just make the results more funny, which is, of course, the point.

So, here is the code:

import java.util.regex.*;
import com.decontextualize.a2z.TextFilter;

public class RegexNames extends TextFilter 
{
  public static void main(String[] args)
  {
    new RegexNames().run();
  } // end main(String[] args)

  private String search1  = "(Mr\\.|Mrs\\.|Lord|Lady|Sir) ([A-Z]\\w+)";
  private String replace1 = "##NAME##";

  private String search2  = "(\\.” |\\?” |!” |\\. |\\? |! |\\r|\\r“)([A-Z]\\w+)";

  private String search3  = "( |“|\\r)(Mr\\.|Mrs\\.|Lord|Lady|Sir|Miss|Monday|Tuesday|Wednesday|Friday|Saturday|Sunday|January|February|March|April|May|June|July|August|September|October|November|December|Michaelmas)";

  private String search4  = "( |“)([A-Z][a-z]+)";

  private String [] class_names = new String [] { 
    "Adam", "Alejandro", "Andrew", "Bryan", "Caroline", "Dimitris", "Jonathan", "Joseph", "Martin", "Michael", "Ozge", "Sanjay", "Steven"       };
  public void begin() 
  {
    println("* * *");
  }

  public void eachLine(String line)
  {
    String line_new;

    Pattern p1 = Pattern.compile(search1);
    Matcher m1 = p1.matcher(line);
    if (m1.find()) 
    {
      line = line.replaceAll(m1.group(2), replace1);
    }

    line_new = line;

    Pattern p2 = Pattern.compile(search2);
    Matcher m2 = p2.matcher(line_new);
    if (m2.find()) 
    {
      line_new = line_new.replaceAll(m2.group(2), "##" + m2.group(2) + "##");
    }

    Pattern p3 = Pattern.compile(search3);
    Matcher m3 = p3.matcher(line_new);
    if (m3.find()) 
    {
      line_new = line_new.replaceAll(m3.group(2), "##" + m3.group(2) + "##");
    }

    Pattern p4 = Pattern.compile(search4);
    Matcher m4 = p4.matcher(line_new);
    if (m4.find()) 
    {
      line_new = line_new.replaceAll(m4.group(2), "##NAME##");
    }

    line_new = line_new.replace( "##NAME##", class_names[(int)(Math.random() * 13)] );

    line_new = line_new.replace( "##", "" );

    println("\t" + line_new);
  } // end eachLine(String line)

  public void end() 
  {
    println("* * *");
  }

} // end RegexNames extends TextFilter

And here is the output (you can find the original text at Project Gutenberg):

* * *
     It is a truth universally acknowledged, that a single man in possession of a large fortune must be in want of a wife.
     However little known the feelings or views of such a man may be on his first entering a neighbourhood, this truth is so well fixed in the minds of the surrounding families, that he is considered the rightful property of someone or other of their daughters.
     “Adam dear Mr. Adam,” said his lady to him one day, “have you heard that Netherfield Park is let at last?”
     Mr. Bryan replied that he had not.
     “Steven it is, returned she; ”for Mrs. Steven has just been here, and she told me all about it.
     Mr. Michael made no answer.
     “Dimitris you not want to know who has taken it?” cried his wife impatiently.
     “YOU want to tell me, and I have no objection to hearing it.”
     This was invitation enough.
     “Steven, my dear, you must know, Mrs. Steven says that Netherfield is taken by a young man of large fortune from the north of England; that he came down on Monday in a chaise and four to see the place, and was so much delighted with it, that he agreed with Mr. Morris immediately; that he is to take possession before Michaelmas, and some of his servants are to be in the house by the end of next week.”
     “Joseph is his name?”
     “Adam.”
     “Dimitris he married or single?”
     “Bryan! Single, my dear, to be sure! A single man of large fortune; four or five thousand a year. What a fine thing for our girls!”
     “How so? How can it affect them?”
     “Bryan dear Mr. Bryan,” replied his wife, “how can you be so tiresome! You must know that I am thinking of his marrying one of them.”
     “Ozge that his design in settling here?”
     “Caroline! Nonsense, how can you talk so! But it is very likely that he MAY fall in love with one of them, and therefore you must visit him as soon as he comes.”
     “I see no occasion for that. You and the girls may go, or you may send them by themselves, which perhaps will be still better, for as you are as handsome as any of them, Mr. Michael may like you the best of the party.”
     “Michael dear, you flatter me. I certainly HAVE had my share of beauty, but I do not pretend to be anything extraordinary now. When a woman has five grown-up daughters, she ought to give over thinking of her own beauty.”
     “Jonathan such cases, a woman has not often much beauty to think of.”
     “Michael, my dear, you must indeed go and see Mr. Michael when he comes into the neighbourhood.”
     “Steven is more than I engage for, I assure you.”
     “Jonathan consider your daughters. Only think what an establishment it would be for one of them. Sir Jonathan and Lady Lucas are determined to go, merely on that account, for in general, you know, they visit no newcomers. Indeed you must go, for it will be impossible for US to visit him if you do not.”
     “Sanjay are over-scrupulous, surely. I dare say Mr. Sanjay will be very glad to see you; and I will send a few lines by you to assure him of my hearty consent to his marrying whichever he chooses of the girls; though I must throw in a good word for my little Lizzy.”
     “I desire you will do no such thing. Lizzy is not a bit better than the others; and I am sure she is not half so handsome as Michael, nor half so good-humoured as Lydia. But you are always giving HER the preference.”
     “Andrew have none of them much to recommend them,” replied he; “they are all silly and ignorant like other girls; but Lizzy has something more of quickness than her sisters.”
     “Mr. Bryan, how CAN you abuse your own children in such a way? You take delight in vexing me. You have no compassion for my poor nerves.”
     “Caroline mistake me, my dear. I have a high respect for your nerves. They are my old friends. I have heard you mention them with consideration these last twenty years at least.”
     Mr. Dimitris was so odd a mixture of quick parts, sarcastic humour, reserve, and caprice, that the experience of three-and- twenty years had been insufficient to make his wife understand his character. HER mind was less difficult to develop. Dimitris was a woman of mean understanding, little information, and uncertain temper. When she was discontented, she fancied herself nervous. The business of her life was to get her daughters married; its solace was visiting and news.
* * *

Leave a Reply

Your email address will not be published. Required fields are marked *