package edu.uncc.cs.watsonsim; import org.json.simple.JSONObject; import org.apache.commons.lang3.StringEscapeUtils; public class Passage extends Phrase { // Stored Fields public final String reference; public final String engine_name; public final String title; // Mutable public Score scores = Score.empty(); /** * Create a new Passage * * @param engine_name A simple lowercase string * @param title * @param text * @param reference Specific to the engine, or a URL, for later lookup */ public Passage(String engine_name, String title, String text, String reference) { super(text); if (engine_name == null) throw new NullPointerException("Engine name cannot be null."); if (title == null) throw new NullPointerException("Title cannot be null."); if (reference == null) throw new NullPointerException("Reference cannot be null."); this.reference = reference; this.engine_name = engine_name; this.title = StringEscapeUtils.unescapeXml(title); } // Copy constructor public Passage(Passage original) { this(original.engine_name, original.title, original.text, original.reference); scores = original.scores.clone(); } /** Set the value of this Score for this passage, returning the passage. * * The intended use is something like this: * new Passage(.......).score("SKIP_BIGRAM", 9.45).score("NGRAM", -1.2) * @param name * @param value */ public Passage score(String name, double value) { scores.put(name, value); return this; } /** Return a JSON object with the same fields */ public JSONObject toJSON() { JSONObject jo = new JSONObject(); jo.put("text", text); jo.put("title", title); jo.put("reference", reference); jo.put("engine_name", engine_name); return jo; } /****************************************************** * * Autogenerated hashcode() and equals() follow * ******************************************************/ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + getTokens().hashCode(); result = prime * result + engine_name.hashCode(); result = prime * result + reference.hashCode(); result = prime * result + text.hashCode(); result = prime * result + title.hashCode(); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Passage other = (Passage) obj; if (!getTokens().equals(other.getTokens())) return false; else if (!engine_name.equals(other.engine_name)) return false; else if (!reference.equals(other.reference)) return false; else if (!text.equals(other.text)) return false; else if (!title.equals(other.title)) return false; return true; } }