Skip to content

Commit

Permalink
adding equals(), compareTo() and hashCode() to Mention class as well as
Browse files Browse the repository at this point in the history
a reference to the original CAS object
  • Loading branch information
thuber committed Oct 25, 2012
1 parent a0fe737 commit 2f72aaf
Showing 1 changed file with 71 additions and 3 deletions.
74 changes: 71 additions & 3 deletions src/main/java/de/berlin/hu/chemspot/Mention.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
package de.berlin.hu.chemspot;

import de.berlin.hu.util.Constants;

import org.apache.uima.cas.CAS;
import org.apache.uima.jcas.tcas.Annotation;
import org.u_compare.shared.semantic.NamedEntity;

public class Mention {
public class Mention implements Comparable<Object> {
private int start;
private int end;
private String text;
private String[] ids;
private String source;
private CAS cas;

/**
* Represents a chemical entity found in a text.
Expand All @@ -30,7 +34,7 @@ public class Mention {
* @param ids a string representation of an array of identifiers of the form: [0] CHID, [1] CHEB, [2] CAS, [3] PUBC, [4] PUBS, [5] INCH, [6] DRUG, [7] HMBD, [8] KEGG, [9] KEGD, [10] MESH
* @param source indicates whether found by the CRF, the dictionary or taken from goldstandard
*/
public Mention(int start, int end, String text, String ids, String source) {
public Mention(int start, int end, String text, String ids, String source, CAS cas) {
this.start = start;
this.end = end;
this.text = text;
Expand All @@ -43,6 +47,7 @@ public Mention(int start, int end, String text, String ids, String source) {
this.ids = new String[0];
}
this.source = source;
this.cas = cas;
}

public Mention(int start, int end, String text) {
Expand All @@ -58,7 +63,7 @@ public Mention(int start, int end) {
}

public Mention(NamedEntity entity) {
this(entity.getBegin(), entity.getEnd(), entity.getCoveredText(), entity.getId(), entity.getSource());
this(entity.getBegin(), entity.getEnd(), entity.getCoveredText(), entity.getId(), entity.getSource(), entity.getCAS());
}

public int getStart() {
Expand Down Expand Up @@ -139,9 +144,72 @@ private String getId(int pos) {
}
return id;
}

public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null || !(obj instanceof Mention)) {
return false;
}

Mention other = (Mention) obj;
if (getStart() != other.getStart() || getEnd() != other.getEnd()
|| (getText() == null && other.getText() != null) || (getText() != null && !getText().equals(other.getText()))) {
return false;
}

return true;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getStart();
result = prime * result + getEnd();
result = prime * result + ((text == null) ? 0 : text.hashCode());
return result;
}

@Override
public String toString() {
return start + " " + end + " " + text + " " + getCHID();
}

public int compareTo(Object o) {
if (this.equals(o)) {
return 0;
}

int otherBegin = 0;
int otherEnd = 0;

if (o instanceof Mention) {
Mention other = (Mention) o;
otherBegin = other.getStart();
otherEnd = other.getEnd();
} else if (o instanceof Annotation) {
Annotation other = (Annotation) o;
otherBegin = other.getBegin();
otherEnd = other.getEnd();
} else {
return 0;
}

if (getStart() != otherBegin) {
return getStart() - otherBegin;
} else {
return getEnd() - otherEnd;
}
}

public CAS getCas() {
return cas;
}

public void setCas(CAS cas) {
this.cas = cas;
}
}

0 comments on commit 2f72aaf

Please sign in to comment.