/* * TextSearcher.java * * Created on January 13, 2003, 11:14 PM */ package project1; import java.io.*; import java.util.LinkedList; import java.util.Iterator; import java.net.URL; /** The oracle works by consulting a sacred text (the Sonnets of William Shakespeare). * If the request is found in the text, as a string (case-insensitive), the oracle * returns an omen which tells the line number when the first match was found, * and also quotes the full line in its descriptions. * The earlier the match is found, the more favorable it is considered. * Line numbers start at 1. * Every element in the array of strings is considered a "line", regardless of * how long it is. * * @author dickey */ public class TextSearcher implements IOracle { /** Holds the complete text to be searched, one line per array entry. * Some lines may be empty, but none will be null. */ private String[] theText; /** Creates a new instance of TextSearcher */ public TextSearcher() { theText = TextSearcherUtils.readTextIntoArray(); } /** See if some data is suitable to be presented to the oracle. * For example, the God of Luck required initials (2 upper case characters) * as request data. * @param requestData the data, as a string; exact format and requirements * are oracle-dependent. * @return true iff the requestData is acceptable (can be processed without * error) by the oracle. * * For this particular oracle, any non-empty string is acceptable. */ public boolean checkRequestData(String requestData) { if (requestData != null && requestData.length() > 0) { return true; } else { return false; } } /** Tell the name of this oracle. * @return A short string with the name of the oracle; there should be * no leading or trailing whitespace. */ public String getName() { return "Text Searcher"; } /** Search the entrails and return the omen found. * It should never go into an infinite loop. * @param requestData The information (typically a name) about which the oracle * is searching for information (for an omen). * @return an omen, or null if there is no omen at all. * * For this oracle, the text is searched line by line, case-insensitive. * Matches are looked for only within lines, not across lines. * For example, if two lines of the file are * cse * 143 * Then this matches, among other things, "cse", "CSE", "C", "1", "43", etc. * but does not match "cse143" or "se143", etc. */ public IOmen searchEntrailsForOmen(String requestData) { if (theText == null) { //for whatever reason, there is no text to search. return null; } String thisLine = null; String requestUpperCase = requestData.toUpperCase(); //for case-insensitivity int lineNum = 1; //Note that lines are numbered from one, not zero. while (lineNum <= theText.length) { thisLine = theText[lineNum-1]; String thisLineUpperCase = thisLine.toUpperCase(); //for case-insensitivity int position = thisLineUpperCase.indexOf(requestUpperCase); if (position >= 0) { break; } lineNum++; } //while loop ended -- why? if (lineNum > theText.length) { System.out.println(requestData + " not found."); return null; } else { assert thisLine != null; System.out.println(thisLine); LuckTesterOmen retOmen = new LuckTesterOmen(lineNum, thisLine); return retOmen; } //end method } /** Just for testing this class. Not a Project 1 requirement.*/ public static void test(String[] args) { //String[] result = Utilities.readTextIntoArray("ws-sonnets.txt"); IOracle oracle = new TextSearcher(); IOmen oresult = oracle.searchEntrailsForOmen("love"); } //end TextSearcher }