edu.stanford.nlp.ling
Class CoreLabel

java.lang.Object
  extended by edu.stanford.nlp.util.ArrayCoreMap
      extended by edu.stanford.nlp.ling.CoreLabel
All Implemented Interfaces:
HasCategory, HasContext, HasIndex, HasOffset, HasTag, HasWord, Label, CoreMap, TypesafeMap<CoreMap>, java.io.Serializable
Direct Known Subclasses:
CyclicCoreLabel

public class CoreLabel
extends ArrayCoreMap
implements Label, HasWord, HasTag, HasCategory, HasContext, HasIndex, HasOffset

A CoreLabel is a Map from keys (which are Class objects) to values, whose type is determined by the key. That is, it is a heterogeneous typesafe Map (see Josh Bloch, Effective Java, 2nd edition).

The CoreLabel class in particular bridges the gap between old-style JavaNLP Labels and the new CoreMap infrastructure. Instances of this class can be used (almost) anywhere that the now-defunct FeatureLabel family could be used. This data structure is backed by an ArrayCoreMap.

Author:
dramage, rafferty
See Also:
Serialized Form

Nested Class Summary
static interface CoreLabel.GenericAnnotation<T>
          Class that all "generic" annotations extend.
 
Nested classes/interfaces inherited from interface edu.stanford.nlp.util.TypesafeMap
TypesafeMap.Key<BASE,VALUE>
 
Field Summary
static java.util.HashMap<java.lang.String,java.lang.Class<? extends CoreLabel.GenericAnnotation>> genericKeys
           
static java.util.HashMap<java.lang.Class<? extends CoreLabel.GenericAnnotation>,java.lang.String> genericValues
           
static java.lang.String TAG_SEPARATOR
          Tag separator to use by default
 
Constructor Summary
CoreLabel()
          Default constructor, calls super()
CoreLabel(CoreLabel label)
          Returns a new CoreLabel instance based on the contents of the given CoreLabel.
CoreLabel(CoreMap label)
          Returns a new CoreLabel instance based on the contents of the given CoreMap.
CoreLabel(int capacity)
          Initializes this CoreLabel, pre-allocating arrays to hold up to capacity key,value pairs.
CoreLabel(Label label)
          Returns a new CoreLabel instance based on the contents of the given label.
CoreLabel(java.lang.String[] keys, java.lang.String[] values)
          This constructor attempts to parse the String keys into Class keys.
 
Method Summary
 java.lang.String after()
          Return the whitespace String after the word.
 java.lang.String before()
          
 int beginPosition()
          Return the beginning character offset of the label (or -1 if none).
 java.lang.String category()
          Return the category value of the label (or null if none).
 java.lang.String docID()
          
 int endPosition()
          Return the ending character offset of the label (or -1 if none).
static LabelFactory factory()
          Return a factory for this kind of label
<KEY extends TypesafeMap.Key<CoreMap,java.lang.String>>
java.lang.String
getString(java.lang.Class<KEY> key)
          Return a non-null String value for a key.
 int index()
          
 LabelFactory labelFactory()
          Returns a factory that makes labels of the exact same type as this one.
 java.lang.String lemma()
          Return the lemma of the label (or null if none).
 java.lang.String ner()
          Return the named entity class of the label (or null if none).
 java.lang.String originalText()
          Return the String which is the original character sequence of the token.
 int sentIndex()
          
 void setAfter(java.lang.String after)
          Set the whitespace String after the word.
 void setBefore(java.lang.String before)
          Set the whitespace String before the word.
 void setBeginPosition(int beginPos)
          Set the beginning character offset for the label.
 void setCategory(java.lang.String category)
          Set the category value for the label (if one is stored).
 void setDocID(java.lang.String docID)
          
 void setEndPosition(int endPos)
          Set the ending character offset of the label (or -1 if none).
 void setFromString(java.lang.String labelStr)
          Set the contents of this label to this String representing the complete contents of the label.
 void setIndex(int index)
          
 void setLemma(java.lang.String lemma)
           
 void setNER(java.lang.String ner)
           
 void setOriginalText(java.lang.String originalText)
          Set the String which is the original character sequence of the token.
 void setSentIndex(int sentIndex)
          
 void setTag(java.lang.String tag)
          Set the tag value for the label (if one is stored).
 void setValue(java.lang.String value)
          Set the value for the label (if one is stored).
 void setWord(java.lang.String word)
          Set the word value for the label (if one is stored).
 java.lang.String tag()
          Return the tag value of the label (or null if none).
 java.lang.String value()
          Return a String representation of just the "main" value of this label.
 java.lang.String word()
          Return the word value of the label (or null if none).
 
Methods inherited from class edu.stanford.nlp.util.ArrayCoreMap
compact, containsKey, equals, get, has, hashCode, keySet, remove, set, setCapacity, size, toShorterString, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface edu.stanford.nlp.ling.Label
toString
 

Field Detail

genericKeys

public static java.util.HashMap<java.lang.String,java.lang.Class<? extends CoreLabel.GenericAnnotation>> genericKeys

genericValues

public static java.util.HashMap<java.lang.Class<? extends CoreLabel.GenericAnnotation>,java.lang.String> genericValues

TAG_SEPARATOR

public static final java.lang.String TAG_SEPARATOR
Tag separator to use by default

See Also:
Constant Field Values
Constructor Detail

CoreLabel

public CoreLabel()
Default constructor, calls super()


CoreLabel

public CoreLabel(int capacity)
Initializes this CoreLabel, pre-allocating arrays to hold up to capacity key,value pairs. This array will grow if necessary.

Parameters:
capacity - Initial capacity of object in key,value pairs

CoreLabel

public CoreLabel(CoreLabel label)
Returns a new CoreLabel instance based on the contents of the given CoreLabel. It copies the contents of the other CoreLabel. Implementation note: this is a the same as the constructor that takes a CoreMap, but is needed to ensure unique most specific type inference for selecting a constructor at compile-time.

Parameters:
label - The CoreLabel to copy

CoreLabel

public CoreLabel(CoreMap label)
Returns a new CoreLabel instance based on the contents of the given CoreMap. It copies the contents of the other CoreMap.

Parameters:
label - The CoreMap to copy

CoreLabel

public CoreLabel(Label label)
Returns a new CoreLabel instance based on the contents of the given label. Warning: The behavior of this method is a bit disjunctive! If label is a CoreMap (including CoreLabel), then it's entire contents is copied into this label. But, otherwise, just the value() is copied.

Parameters:
label - Basis for this label

CoreLabel

public CoreLabel(java.lang.String[] keys,
                 java.lang.String[] values)
This constructor attempts to parse the String keys into Class keys. It's mainly useful for reading from a file. A best effort attempt is made to correctly parse the keys according to the String lookup function in CoreAnnotations.

Parameters:
keys - Array of Strings that are class names
values - Array of values (as String)
Method Detail

factory

public static LabelFactory factory()
Return a factory for this kind of label

Returns:
The label factory

labelFactory

public LabelFactory labelFactory()
Returns a factory that makes labels of the exact same type as this one. May return null if no appropriate factory is known.

Specified by:
labelFactory in interface Label
Returns:
the LabelFactory for this kind of label

getString

public <KEY extends TypesafeMap.Key<CoreMap,java.lang.String>> java.lang.String getString(java.lang.Class<KEY> key)
Return a non-null String value for a key. This method is included for backwards compatibility with AbstractMapLabel. It is guaranteed to not return null; if the key is not present or has a null value, it returns the empty string (""). It is only valid to call this method when key is paired with a value of type String.

Type Parameters:
KEY - A key type with a String value
Parameters:
key - The key to return the value of.
Returns:
"" if the key is not in the map or has the value null and the String value of the key otherwise

setFromString

public void setFromString(java.lang.String labelStr)
Set the contents of this label to this String representing the complete contents of the label. A class implementing label may throw an UnsupportedOperationException for this method (only). Typically, this method would do some appropriate decoding of the string in a way that sets multiple fields in an inverse of the toString() method.

Specified by:
setFromString in interface Label
Parameters:
labelStr - the String that translates into the content of the label

setValue

public final void setValue(java.lang.String value)
Set the value for the label (if one is stored).

Specified by:
setValue in interface Label
Parameters:
value - - the value for the label

value

public final java.lang.String value()
Return a String representation of just the "main" value of this label.

Specified by:
value in interface Label
Returns:
the "value" of the label

setWord

public void setWord(java.lang.String word)
Set the word value for the label (if one is stored).

Specified by:
setWord in interface HasWord
Parameters:
word - The word value for the label

word

public java.lang.String word()
Return the word value of the label (or null if none).

Specified by:
word in interface HasWord
Returns:
String the word value for the label

setTag

public void setTag(java.lang.String tag)
Set the tag value for the label (if one is stored).

Specified by:
setTag in interface HasTag
Parameters:
tag - The tag value for the label

tag

public java.lang.String tag()
Return the tag value of the label (or null if none).

Specified by:
tag in interface HasTag
Returns:
String the tag value for the label

setCategory

public void setCategory(java.lang.String category)
Set the category value for the label (if one is stored).

Specified by:
setCategory in interface HasCategory
Parameters:
category - The category value for the label

category

public java.lang.String category()
Return the category value of the label (or null if none).

Specified by:
category in interface HasCategory
Returns:
String the category value for the label

setAfter

public void setAfter(java.lang.String after)
Set the whitespace String after the word.

Specified by:
setAfter in interface HasContext
Parameters:
after - The whitespace String after the word

after

public java.lang.String after()
Return the whitespace String after the word.

Specified by:
after in interface HasContext
Returns:
The whitespace String after the word

setBefore

public void setBefore(java.lang.String before)
Set the whitespace String before the word.

Specified by:
setBefore in interface HasContext
Parameters:
before - the whitespace String before the word

before

public java.lang.String before()

Specified by:
before in interface HasContext
Returns:
the String before the word

setOriginalText

public void setOriginalText(java.lang.String originalText)
Set the String which is the original character sequence of the token.

Specified by:
setOriginalText in interface HasContext
Parameters:
originalText - The original character sequence of the token

originalText

public java.lang.String originalText()
Return the String which is the original character sequence of the token.

Specified by:
originalText in interface HasContext
Returns:
The original character sequence of the token

docID

public java.lang.String docID()

Specified by:
docID in interface HasIndex

setDocID

public void setDocID(java.lang.String docID)

Specified by:
setDocID in interface HasIndex

ner

public java.lang.String ner()
Return the named entity class of the label (or null if none).

Returns:
String the word value for the label

setNER

public void setNER(java.lang.String ner)

lemma

public java.lang.String lemma()
Return the lemma of the label (or null if none).

Returns:
String the word value for the label

setLemma

public void setLemma(java.lang.String lemma)

index

public int index()

Specified by:
index in interface HasIndex

setIndex

public void setIndex(int index)

Specified by:
setIndex in interface HasIndex

sentIndex

public int sentIndex()

Specified by:
sentIndex in interface HasIndex

setSentIndex

public void setSentIndex(int sentIndex)

Specified by:
setSentIndex in interface HasIndex

beginPosition

public int beginPosition()
Description copied from interface: HasOffset
Return the beginning character offset of the label (or -1 if none).

Specified by:
beginPosition in interface HasOffset
Returns:
the beginning position for the label

endPosition

public int endPosition()
Description copied from interface: HasOffset
Return the ending character offset of the label (or -1 if none).

Specified by:
endPosition in interface HasOffset
Returns:
the end position for the label

setBeginPosition

public void setBeginPosition(int beginPos)
Description copied from interface: HasOffset
Set the beginning character offset for the label. Setting this key to "-1" can be used to indicate no valid value.

Specified by:
setBeginPosition in interface HasOffset
Parameters:
beginPos - The beginning position

setEndPosition

public void setEndPosition(int endPos)
Description copied from interface: HasOffset
Set the ending character offset of the label (or -1 if none).

Specified by:
setEndPosition in interface HasOffset
Parameters:
endPos - The end character offset for the label


Stanford NLP Group