edu.stanford.nlp.ling
Class CoreLabel

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

public class CoreLabel
extends Object
implements CoreMap, Label, HasWord, HasTag, HasCategory, HasContext, HasIndex, Serializable

A class that bridges the gap between old-style JavaNLP objects and the new core object infrastructure. Instances of this class can be used (almost) anywhere that 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 This allows you to read in arbitrary values from a file as features, for example.
 
Nested classes/interfaces inherited from interface edu.stanford.nlp.util.TypesafeMap
TypesafeMap.Key<BASE,VALUE>
 
Field Summary
static HashMap<String,Class<? extends CoreLabel.GenericAnnotation>> genericKeys
           
static HashMap<Class<? extends CoreLabel.GenericAnnotation>,String> genericValues
           
protected  ArrayCoreMap map
          The CoreMap backing this label.
 
Constructor Summary
CoreLabel()
          Default constructor, calls super()
CoreLabel(Label label)
          Returns a new CoreLabel instance based on the contents of the given label.
CoreLabel(String[] keys, String[] values)
          This constructor attempts to parse the String keys into Class keys.
 
Method Summary
 String after()
          Return the String after the word.
 void appendAfter(String after)
          Append this String to the current after String
 String before()
          
 String category()
          Return the category value of the label (or null if none).
<VALUE,KEY extends TypesafeMap.Key<CoreMap,VALUE>>
boolean
containsKey(Class<KEY> key)
          Returns true if contains the given key.
 String current()
          Return the String which is the unmangled word.
 String docID()
          
 boolean equals(Object other)
           
static LabelFactory factory()
          
static CoreLabel fromCoreMap(ArrayCoreMap map)
          Wraps the given CoreMap as a CoreLabel.
<VALUE,KEY extends TypesafeMap.Key<CoreMap,VALUE>>
VALUE
get(Class<KEY> key)
          Returns the value associated with the given key or null if none is provided.
<KEY extends TypesafeMap.Key<CoreMap,String>>
String
getString(Class<KEY> key)
          This method included for backwards compatibility with AbstractMapLabel.
<VALUE,KEY extends TypesafeMap.Key<CoreMap,VALUE>>
boolean
has(Class<KEY> key)
          Returns true if the map contains the given key.
 int hashCode()
           
 int index()
          
 Set<Class<?>> keySet()
          Collection of keys currently held in this map.
 LabelFactory labelFactory()
          Returns a factory that makes labels of the exact same type as this one.
 String lemma()
          
 String ner()
          
 void prependBefore(String before)
          Prepend this String to the before String.
<VALUE,KEY extends TypesafeMap.Key<CoreMap,VALUE>>
VALUE
remove(Class<KEY> key)
          Removes the given key from the map, returning the value removed.
 int sentIndex()
          
<VALUEBASE,VALUE extends VALUEBASE,KEY extends TypesafeMap.Key<CoreMap,VALUEBASE>>
VALUE
set(Class<KEY> key, VALUE value)
          Associates the given value with the given type for future calls to get.
 void setAfter(String after)
          Set the String after the word.
 void setBefore(String before)
          Set the String before the word.
 void setCategory(String category)
          Set the category value for the label (if one is stored).
 void setCurrent(String current)
          Set the String which is the unmangled word.
 void setDocID(String docID)
          
 void setFromString(String labelStr)
          Set the contents of this label to this String representing the complete contents of the label.
 void setIndex(int index)
          
 void setLemma(String lemma)
           
 void setNER(String ner)
           
 void setSentIndex(int sentIndex)
          
 void setTag(String tag)
          Set the tag value for the label (if one is stored).
 void setValue(String value)
          Set the value for the label (if one is stored).
 void setWord(String word)
          Set the word value for the label (if one is stored).
 int size()
          Returns the number of keys in the map.
 String tag()
          Return the tag value of the label (or null if none).
 String toString()
          Return a String representation of the label.
 String value()
          Return a String representation of just the "main" value of this label.
 String word()
          Return the word value of the label (or null if none).
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

map

protected ArrayCoreMap map
The CoreMap backing this label.


genericKeys

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

genericValues

public static HashMap<Class<? extends CoreLabel.GenericAnnotation>,String> genericValues
Constructor Detail

CoreLabel

public CoreLabel()
Default constructor, calls super()


CoreLabel

public CoreLabel(Label label)
Returns a new CoreLabel instance based on the contents of the given label. Provides custom initialization if label implements CoreLabel (default copy constructor) or if it implements MapLabel (copies map values).


CoreLabel

public CoreLabel(String[] keys,
                 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 -
values -
Method Detail

fromCoreMap

public static CoreLabel fromCoreMap(ArrayCoreMap map)
Wraps the given CoreMap as a CoreLabel.


factory

public static LabelFactory 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,String>> String getString(Class<KEY> key)
This method included for backwards compatibility with AbstractMapLabel. It is guaranteed to not return null; if the key is not present, it returns the empty String. *However* it is only valid to call this method when key is paired with a value of type String.

Type Parameters:
KEY -
Parameters:
key -
Returns:
"" if the key is not in the map, the String value of the key otherwise

size

public int size()
Returns the number of keys in the map.

Specified by:
size in interface TypesafeMap<CoreMap>

setFromString

public void setFromString(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 void setValue(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 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(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 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(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 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(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 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(String after)
Set the String after the word.

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

appendAfter

public void appendAfter(String after)
Append this String to the current after String

Specified by:
appendAfter in interface HasContext
Parameters:
after - The String to be prepended

after

public String after()
Return the String after the word.

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

setBefore

public void setBefore(String before)
Set the String before the word.

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

prependBefore

public void prependBefore(String before)
Prepend this String to the before String.

Specified by:
prependBefore in interface HasContext
Parameters:
before - the String to be prepended

before

public String before()

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

setCurrent

public void setCurrent(String current)
Set the String which is the unmangled word.

Specified by:
setCurrent in interface HasContext
Parameters:
current - the unmangled word

current

public String current()
Return the String which is the unmangled word.

Specified by:
current in interface HasContext
Returns:
the unmangled word

docID

public String docID()

Specified by:
docID in interface HasIndex

setDocID

public void setDocID(String docID)

Specified by:
setDocID in interface HasIndex

ner

public String ner()


setNER

public void setNER(String ner)

lemma

public String lemma()


setLemma

public void setLemma(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

get

public <VALUE,KEY extends TypesafeMap.Key<CoreMap,VALUE>> VALUE get(Class<KEY> key)
Returns the value associated with the given key or null if none is provided.

Specified by:
get in interface TypesafeMap<CoreMap>

has

public <VALUE,KEY extends TypesafeMap.Key<CoreMap,VALUE>> boolean has(Class<KEY> key)
Returns true if the map contains the given key.

Specified by:
has in interface TypesafeMap<CoreMap>

keySet

public Set<Class<?>> keySet()
Collection of keys currently held in this map. Some implementations may have the returned set be immutable. As of Java 1.6 Java's generics type inference is broken enough that the proper signature for this method - Set<Class<? extends Key<BASE,?>>> - cannot be implemented by subclasses, so the return signature is not typed as strongly as it could be.

Specified by:
keySet in interface TypesafeMap<CoreMap>

remove

public <VALUE,KEY extends TypesafeMap.Key<CoreMap,VALUE>> VALUE remove(Class<KEY> key)
Removes the given key from the map, returning the value removed.

Specified by:
remove in interface TypesafeMap<CoreMap>

set

public <VALUEBASE,VALUE extends VALUEBASE,KEY extends TypesafeMap.Key<CoreMap,VALUEBASE>> VALUE set(Class<KEY> key,
                                                                                                    VALUE value)
Associates the given value with the given type for future calls to get. Returns the value removed or null if no value was present.

Specified by:
set in interface TypesafeMap<CoreMap>

containsKey

public <VALUE,KEY extends TypesafeMap.Key<CoreMap,VALUE>> boolean containsKey(Class<KEY> key)
Returns true if contains the given key.

Specified by:
containsKey in interface TypesafeMap<CoreMap>

toString

public String toString()
Description copied from interface: Label
Return a String representation of the label. For a multipart label, this will return all parts. The toString() method causes a label to spill its guts. It should always return an empty string rather than null if there is no value.

Specified by:
toString in interface Label
Overrides:
toString in class Object
Returns:
a text representation of the full label contents

equals

public boolean equals(Object other)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object


Stanford NLP Group