val sentences : List[CoreMap] = document.get(classOf[SentencesAnnotation])
document is an edu.stanford.nlp.pipeline.Annotation
The original Java code from Stanford [1] looked like this:
List<CoreMap> sentences = document.get(SentencesAnnotation.class);
Two other Java imports of note:
import edu.stanford.nlp.util.CoreMap
import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation
The Scala error message I get is:
inferred type arguments [Nothing,edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation] do not conform to method get's type parameter bounds [VALUE,KEY <: edu.stanford.nlp.util.TypesafeMap.Key[edu.stanford.nlp.util.CoreMap,VALUE]]
The Javadoc for the get method says:
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.
Is what I'm doing wrong obvious to anyone?
Geoff
The easiest way to verify what is your problem is to let the type
inferer find the correct type for sentences:
val sentences= document.get(classOf[SentencesAnnotation])
If the type inferer cannot guess them, you'll have to specify them,
but again they will be two
val sentences:List[ValueType,KeyType] = ....
Best Regards
Edmondo
2012/4/23 Geoffrey S. Knauth <ge...@knauth.org>:
On Apr 23, 2012 8:46 AM, "Geoffrey S. Knauth" <ge...@knauth.org> wrote:
> val sentences : List[CoreMap] = document.get(classOf[SentencesAnnotation])
>
> List<CoreMap> sentences = document.get(SentencesAnnotation.class);
>
> public <VALUE,KEY extends TypesafeMap.Key<CoreMap,VALUE>> VALUE get(Class<KEY> key)
>
> Is what I'm doing wrong obvious to anyone?
List means scala.collections.immutable.List in the Scala code.
Try what Edmondo said about leaving off the type of your val; it should be able to infer the right type here.
--
Stephen Compall
Greetings from sunny Appleton!
> Try what Edmondo said about leaving off the type of your val; it should be able to infer the right type here.
Thanks for your responses. I did leave off the type and got the same error. In fact that's what I had to begin with; I just added the type to make the first email clearer. What puzzles me is how this could have worked in Java.
If the type inferer can't guess it, in most of the cases this is the
sign of a problem in the API.
In fact, if we look together to :
public <VALUE,KEY extends TypesafeMap.Key<CoreMap,VALUE>> VALUE
get(Class<KEY> key)
and we try to assign to a variable its result, what type its result should be?
Best Regards
Edmondo
2012/4/23 Geoffrey S. Knauth <ge...@knauth.org>:
> In fact, if we look together to :
> public <VALUE,KEY extends TypesafeMap.Key<CoreMap,VALUE>> VALUE
> get(Class<KEY> key)
> and we try to assign to a variable its result, what type its result should be?
The return type makes my brain hurt. But I don't think the Scala compiler is complaining about the return type. I think it's complaining about the argument to get(). In all the Java code I see, the argument to get() is something like SentencesAnnotation.class. One argument, a class. But when I do that in Scala, I get an error message that makes my brain hurt again. The message seems related to get's return type so at least it's the same headache.
Below, document isa edu.stanford.nlp.pipeline.Annotation.
var sentences = document.get(classOf[SentencesAnnotation])
has been giving me this error:
inferred type arguments [Nothing,edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation]
do not conform to method get's type parameter bounds
[VALUE,KEY <: edu.stanford.nlp.util.TypesafeMap.Key[edu.stanford.nlp.util.CoreMap,VALUE]]
Out of curiosity, I changed get's argument to: 42
var sentences = document.get(42)
type mismatch; found : Int(42) required: java.lang.Class[?]
which makes me wonder why classOf[SentencesAnnotation] was not good
enough, as it was in the original Java code
(SentencesAnnotation.class) ?
----------------------------------------------------------------------
JavaDoc for:
edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation
All Implemented Interfaces:
CoreAnnotation<List<CoreMap>>, TypesafeMap.Key<CoreMap,List<CoreMap>>
Enclosing class:
CoreAnnotations
"What has changed?" analysis begs to differ; the annotated var type in
your original Scala sample is not the same as that in your Java sample.
Stephen writes:
> List means scala.collections.immutable.List in the Scala code.
or maybe scala.collection..., either way.
and the supertype of SentencesAnnotation:
> TypesafeMap.Key<CoreMap,List<CoreMap>>
--
Stephen Compall
^aCollection allSatisfy: [:each|aCondition]: less is better
> "What has changed?" analysis begs to differ; the annotated var type in
> your original Scala sample is not the same as that in your Java sample.
I removed the annotated var type from the original Scala sample. This is the minimal set of code lines that represents my problem. The last line does not compile. Ensime highlights `document.get': it doesn't like the argument I gave, but Java was happy with SentencesAnnotation.class. If I could figure out how to make the last line compile, all my current problems would be solved.
import java.util.Properties
import edu.stanford.nlp.pipeline.StanfordCoreNLP
import edu.stanford.nlp.pipeline.Annotation
import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation
object Tiny {
def minimal() = {
// see: http://nlp.stanford.edu/software/corenlp.shtml
val props = new Properties
props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref")
val pipeline = new StanfordCoreNLP(props)
val document = new Annotation("The rain in Spain falls mainly in the plain.")
pipeline.annotate(document)
val sentences = document.get(classOf[SentencesAnnotation])
}
}
For everyone else, this was the fix:
< document.get(classOf[SentencesAnnotation])
--
> document.get[java.util.List[CoreMap], SentencesAnnotation](classOf[SentencesAnnotation])
http://www.scala-lang.org/node/11972
(with regard to someone else's experience using Scala with StanfordNLP's Java code)