Example #1
0
  /**
   * Loads the cached features for a given document
   *
   * @param document
   * @param documentFile The cache file for the document.
   * @return the cached features if possible. Null if a cache doesn't exist or it fails to get them.
   * @throws Exception
   */
  private List<EventSet> getCachedFeatures(Document document, File documentFile) {
    List<EventSet> generatedEvents = null;
    BufferedReader reader = null;

    if (documentFile.exists() && !documentFile.isDirectory() && documentFile.canRead()) {
      try {
        reader = new BufferedReader(new FileReader(documentFile));
      } catch (FileNotFoundException e) {
        // shouldn't ever get here.. just put this here so I can keep track
        // of exceptions below.
        e.printStackTrace();
      }
    } else {
      return null;
    }

    try {
      // cachedPath is the path to the document that was used when the cache for that
      // document was created. cachedLastModified is the last modified time stamp on the
      // document that was cached.
      String cachedPath = reader.readLine();
      long cachedLastModified = Long.parseLong(reader.readLine());

      String path = document.getFilePath();
      File currDoc = new File(path);
      long lastModified = currDoc.lastModified();

      if (!(currDoc.getCanonicalPath().equals(cachedPath) && lastModified == cachedLastModified)) {
        // cache is invalid
        reader.close();
        return null;
      }
      String line = null;
      generatedEvents = new ArrayList<EventSet>();
      while ((line = reader.readLine()) != null) {
        if (line.isEmpty()) continue;
        EventSet es = new EventSet();
        es.setAuthor(document.getAuthor());
        es.setDocumentName(document.getTitle());
        es.setEventSetID(line);

        String event = null;
        while ((event = reader.readLine()) != null) {
          if (line.isEmpty()) continue;
          if (event.equals(",")) // delimiter for event sets
          break;
          es.addEvent(new Event(event));
        }

        generatedEvents.add(es);
      }
      reader.close();
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }

    return generatedEvents;
  }
Example #2
0
 /**
  * Construct new Event set (with empty vector) of known author
  *
  * @param Author the name of the author
  */
 public EventSet(String Author) {
   events = new ArrayList<Event>();
   setAuthor(Author);
   setNewEventSetID(Author);
 }
Example #3
0
 /** Creates a new list of events given a previously created list of events * */
 public EventSet(List<Event> evts) {
   events = new ArrayList<Event>(evts);
   setAuthor("default");
   setNewEventSetID("default");
 }
Example #4
0
 /**
  * Creates a empty list of event of the specified size
  *
  * @param size
  */
 public EventSet(int size) {
   events = new ArrayList<Event>(size);
   setAuthor("default");
   setNewEventSetID("default");
 }
  @Override
  public EventSet createEventSet(Document ds) throws EventGenerationException {
    String param;
    HashMap<String, String> transform = new HashMap<String, String>();
    boolean whitelist = false;

    String line;
    String[] words;

    if (!(param = (getParameter("underlyingEvents"))).equals("")) {
      try {
        underlyingEvents = EventDriverFactory.getEventDriver(param);
      } catch (Exception e) {
        System.out.println("Error: cannot create EventDriver " + param);
        System.out.println(" -- Using NaiveWordEventSet");
        underlyingEvents = new NaiveWordEventDriver();
      }
    } else { // no underlyingEventsParameter, use NaiveWordEventSet
      underlyingEvents = new NaiveWordEventDriver();
    }

    if (!(param = (getParameter("filename"))).equals("")) {
      filename = param;
    } else { // no underlyingfilename,
      filename = null;
    }

    if (!(param = (getParameter("implicitWhiteList"))).equals("")) {
      if (param.equalsIgnoreCase("true")) {
        whitelist = true;
      }
    } else { // no underlyingfilename,
      whitelist = false;
    }

    EventSet es = underlyingEvents.createEventSet(ds);

    EventSet newEs = new EventSet();
    newEs.setAuthor(es.getAuthor());
    newEs.setNewEventSetID(es.getAuthor());

    BufferedReader br = null;

    if (filename != null) {
      try {
        FileInputStream fis = new FileInputStream(filename);
        br = new BufferedReader(new InputStreamReader(fis));

        while ((line = br.readLine()) != null) {
          if (line.length() > 0) {
            String sep = line.substring(0, 1);
            words = line.substring(1).split(sep, -1);
            if (words.length > 1) {
              transform.put(words[0], words[1]);
              System.out.println("Adding \"" + words[0] + "\" : \"" + words[1] + "\"");
            }
          }
        }

      } catch (IOException e) {
        // catch io errors from FileInputStream or readLine()
        System.out.println("Cannot open/read " + filename);
        System.out.println("IOException error! " + e.getMessage());
        transform = null;
      } finally {
        // if the file opened okay, make sure we close it
        if (br != null) {
          try {
            br.close();
          } catch (IOException ioe) {
          }
        }
      }
    } else {
      transform = null;
    }

    for (Event e : es) {
      String s = e.toString();
      if (transform == null) {
        newEs.addEvent(e);
      } else if (transform.containsKey(s)) {
        String newS = transform.get(s);
        if (newS.length() > 0) {
          newEs.addEvent(new Event(newS));
        }
      } else // s is not in transformation list
      if (whitelist == false) {
        // add only if no implicit whitelisting
        newEs.addEvent(e);
      } // otherwise add nothing
    }
    return newEs;
  }