- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Get this java type signature working in scala
Mon, 2011-12-19, 00:37
Hi,
I was about to test the StanfordCoreNLP lib within a Scala project and
failed at my first attempt to translate the following code:
...
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Annotation document = new Annotation("this is a nice sentence.");
pipeline.annotate(document);
List sentences = document.get(SentencesAnnotation.class);
My translation:
...
val pipeline = new StanfordCoreNLP(props);
val document = new Annotation("this is a nice sentence.");
pipeline.annotate(document);
val list:List[CoreMap] = document.get(classOf[TokensAnnotation])
and some variations of it do not work out. The signature can be found
here http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/util/TypesafeMap.html#get(java.lang.Class)
hopefully someone has an idea how to solve this.
Regards,
Tobias
Mon, 2011-12-19, 01:11
#2
Re: Get this java type signature working in scala
Hi Jason,
sorry I forgot to mention that I did import java.util.List whichshould
override the implicit scala.List import. But now I also triedit with
the alias and it still does not work. The error message is
thefollowing:
inferred type arguments[Nothing,edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation]
donot conform to method get's type parameter bounds [VALUE,KEY
<:edu.stanford.nlp.util.TypesafeMap.Key[edu.stanford.nlp.util.CoreMap,VALUE]]
On Mon, Dec 19, 2011 at 12:43 AM, Jason Zaugg wrote:
> On Mon, Dec 19, 2011 at 9:37 AM, Tobias Neef wrote:
>>
>> Hi,
>>
>> I was about to test the StanfordCoreNLP lib within a Scala project and
>> failed at my first attempt to translate the following code:
>>
>> ...
>> StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
>> Annotation document = new Annotation("this is a nice sentence.");
>> pipeline.annotate(document);
>> List sentences = document.get(SentencesAnnotation.class);
>>
>> My translation:
>> ...
>> val pipeline = new StanfordCoreNLP(props);
>> val document = new Annotation("this is a nice sentence.");
>> pipeline.annotate(document);
>> val list:List[CoreMap] = document.get(classOf[TokensAnnotation])
>>
>> and some variations of it do not work out. The signature can be found
>> here
>> http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/util/TypesafeMap.html#get(java.lang.Class)
>
>
> `List` is `scala.List`. This isn't the same as `java.util.List`.
>
> You can import it with an alias:
>
> import java.util.{List => JList}
> val list: JList[CoreMap] = ...
>
> You might like to convert this to a Scala collection; if so read the section
> "Converting between Java and Scala Collections" in the collections guide:
>
>
> http://docs.scala-lang.org/overviews/collections/conversions-between-jav...
>
> -jason
Mon, 2011-12-19, 18:41
#3
Re: Get this java type signature working in scala
Hi Tobias, if you look at the JavaDoc for methods that return CoreMap,
you will notice that something funny is going on. The following is
copied out of some working code. The map step goes beyond your
question, but it's useful as another example.
val document = new Annotation(text);
corenlp.annotate(document);
val tokens: Array[Array[CoreLabel]] = document.get[
java.util.List[CoreMap],
SentencesAnnotation
] (classOf[SentencesAnnotation]).map {sentence =>
sentence.get[
java.util.List[CoreLabel],
TokensAnnotation
](classOf[TokensAnnotation]).toList.toArray
}.toArray
Note the type parameters to document.get. In Java these are not
necessary as you noted in your example. I would love to know why type
parameters are required in Scala but not in Java.
Peace. Michael
On Sun, Dec 18, 2011 at 4:05 PM, Tobias Neef wrote:
> Hi Jason,
> sorry I forgot to mention that I did import java.util.List whichshould
> override the implicit scala.List import. But now I also triedit with
> the alias and it still does not work. The error message is
> thefollowing:
> inferred type arguments[Nothing,edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation]
> donot conform to method get's type parameter bounds [VALUE,KEY
> <:edu.stanford.nlp.util.TypesafeMap.Key[edu.stanford.nlp.util.CoreMap,VALUE]]
> On Mon, Dec 19, 2011 at 12:43 AM, Jason Zaugg wrote:
>> On Mon, Dec 19, 2011 at 9:37 AM, Tobias Neef wrote:
>>>
>>> Hi,
>>>
>>> I was about to test the StanfordCoreNLP lib within a Scala project and
>>> failed at my first attempt to translate the following code:
>>>
>>> ...
>>> StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
>>> Annotation document = new Annotation("this is a nice sentence.");
>>> pipeline.annotate(document);
>>> List sentences = document.get(SentencesAnnotation.class);
>>>
>>> My translation:
>>> ...
>>> val pipeline = new StanfordCoreNLP(props);
>>> val document = new Annotation("this is a nice sentence.");
>>> pipeline.annotate(document);
>>> val list:List[CoreMap] = document.get(classOf[TokensAnnotation])
>>>
>>> and some variations of it do not work out. The signature can be found
>>> here
>>> http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/util/TypesafeMap.html#get(java.lang.Class)
>>
>>
>> `List` is `scala.List`. This isn't the same as `java.util.List`.
>>
>> You can import it with an alias:
>>
>> import java.util.{List => JList}
>> val list: JList[CoreMap] = ...
>>
>> You might like to convert this to a Scala collection; if so read the section
>> "Converting between Java and Scala Collections" in the collections guide:
>>
>>
>> http://docs.scala-lang.org/overviews/collections/conversions-between-jav...
>>
>> -jason
Mon, 2011-12-19, 19:21
#4
Re: Get this java type signature working in scala
Hi Michael,
thx for your help. I wonder why the compiler can not figure out the
type parameters from the variable definition. But it works like you
described you can even remove the variable type spec. when having he
type parameters.
val tokens = document.get[
java.util.List[CoreMap],
SentencesAnnotation
] (classOf[SentencesAnnotation])
Regards,
Tobias
On Dec 19, 6:39 pm, Michael Schmitz wrote:
> Hi Tobias, if you look at the JavaDoc for methods that return CoreMap,
> you will notice that something funny is going on. The following is
> copied out of some working code. The map step goes beyond your
> question, but it's useful as another example.
>
> val document = new Annotation(text);
>
> corenlp.annotate(document);
>
> val tokens: Array[Array[CoreLabel]] = document.get[
> java.util.List[CoreMap],
> SentencesAnnotation
> ] (classOf[SentencesAnnotation]).map {sentence =>
> sentence.get[
> java.util.List[CoreLabel],
> TokensAnnotation
> ](classOf[TokensAnnotation]).toList.toArray
> }.toArray
>
> Note the type parameters to document.get. In Java these are not
> necessary as you noted in your example. I would love to know why type
> parameters are required in Scala but not in Java.
>
> Peace. Michael
>
>
>
>
>
>
>
> On Sun, Dec 18, 2011 at 4:05 PM, Tobias Neef wrote:
> > Hi Jason,
> > sorry I forgot to mention that I did import java.util.List whichshould
> > override the implicit scala.List import. But now I also triedit with
> > the alias and it still does not work. The error message is
> > thefollowing:
> > inferred type arguments[Nothing,edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation ]
> > donot conform to method get's type parameter bounds [VALUE,KEY
> > <:edu.stanford.nlp.util.TypesafeMap.Key[edu.stanford.nlp.util.CoreMap,VALUE ]]
> > On Mon, Dec 19, 2011 at 12:43 AM, Jason Zaugg wrote:
> >> On Mon, Dec 19, 2011 at 9:37 AM, Tobias Neef wrote:
>
> >>> Hi,
>
> >>> I was about to test the StanfordCoreNLP lib within a Scala project and
> >>> failed at my first attempt to translate the following code:
>
> >>> ...
> >>> StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
> >>> Annotation document = new Annotation("this is a nice sentence.");
> >>> pipeline.annotate(document);
> >>> List sentences = document.get(SentencesAnnotation.class);
>
> >>> My translation:
> >>> ...
> >>> val pipeline = new StanfordCoreNLP(props);
> >>> val document = new Annotation("this is a nice sentence.");
> >>> pipeline.annotate(document);
> >>> val list:List[CoreMap] = document.get(classOf[TokensAnnotation])
>
> >>> and some variations of it do not work out. The signature can be found
> >>> here
> >>>http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/util/Typ...)
>
> >> `List` is `scala.List`. This isn't the same as `java.util.List`.
>
> >> You can import it with an alias:
>
> >> import java.util.{List => JList}
> >> val list: JList[CoreMap] = ...
>
> >> You might like to convert this to a Scala collection; if so read the section
> >> "Converting between Java and Scala Collections" in the collections guide:
>
> >>http://docs.scala-lang.org/overviews/collections/conversions-between-...
>
> >> -jason
Mon, 2011-12-19, 19:31
#5
Re: Re: Get this java type signature working in scala
Yeah, I explicitly specify the types with CoreMap because otherwise it
gets too confusing...
On Mon, Dec 19, 2011 at 10:10 AM, Tobias Neef wrote:
> Hi Michael,
>
> thx for your help. I wonder why the compiler can not figure out the
> type parameters from the variable definition. But it works like you
> described you can even remove the variable type spec. when having he
> type parameters.
>
> val tokens = document.get[
> java.util.List[CoreMap],
> SentencesAnnotation
> ] (classOf[SentencesAnnotation])
>
> Regards,
> Tobias
>
> On Dec 19, 6:39 pm, Michael Schmitz wrote:
>> Hi Tobias, if you look at the JavaDoc for methods that return CoreMap,
>> you will notice that something funny is going on. The following is
>> copied out of some working code. The map step goes beyond your
>> question, but it's useful as another example.
>>
>> val document = new Annotation(text);
>>
>> corenlp.annotate(document);
>>
>> val tokens: Array[Array[CoreLabel]] = document.get[
>> java.util.List[CoreMap],
>> SentencesAnnotation
>> ] (classOf[SentencesAnnotation]).map {sentence =>
>> sentence.get[
>> java.util.List[CoreLabel],
>> TokensAnnotation
>> ](classOf[TokensAnnotation]).toList.toArray
>> }.toArray
>>
>> Note the type parameters to document.get. In Java these are not
>> necessary as you noted in your example. I would love to know why type
>> parameters are required in Scala but not in Java.
>>
>> Peace. Michael
>>
>>
>>
>>
>>
>>
>>
>> On Sun, Dec 18, 2011 at 4:05 PM, Tobias Neef wrote:
>> > Hi Jason,
>> > sorry I forgot to mention that I did import java.util.List whichshould
>> > override the implicit scala.List import. But now I also triedit with
>> > the alias and it still does not work. The error message is
>> > thefollowing:
>> > inferred type arguments[Nothing,edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation ]
>> > donot conform to method get's type parameter bounds [VALUE,KEY
>> > <:edu.stanford.nlp.util.TypesafeMap.Key[edu.stanford.nlp.util.CoreMap,VALUE ]]
>> > On Mon, Dec 19, 2011 at 12:43 AM, Jason Zaugg wrote:
>> >> On Mon, Dec 19, 2011 at 9:37 AM, Tobias Neef wrote:
>>
>> >>> Hi,
>>
>> >>> I was about to test the StanfordCoreNLP lib within a Scala project and
>> >>> failed at my first attempt to translate the following code:
>>
>> >>> ...
>> >>> StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
>> >>> Annotation document = new Annotation("this is a nice sentence.");
>> >>> pipeline.annotate(document);
>> >>> List sentences = document.get(SentencesAnnotation.class);
>>
>> >>> My translation:
>> >>> ...
>> >>> val pipeline = new StanfordCoreNLP(props);
>> >>> val document = new Annotation("this is a nice sentence.");
>> >>> pipeline.annotate(document);
>> >>> val list:List[CoreMap] = document.get(classOf[TokensAnnotation])
>>
>> >>> and some variations of it do not work out. The signature can be found
>> >>> here
>> >>>http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/util/Typ...)
>>
>> >> `List` is `scala.List`. This isn't the same as `java.util.List`.
>>
>> >> You can import it with an alias:
>>
>> >> import java.util.{List => JList}
>> >> val list: JList[CoreMap] = ...
>>
>> >> You might like to convert this to a Scala collection; if so read the section
>> >> "Converting between Java and Scala Collections" in the collections guide:
>>
>> >>http://docs.scala-lang.org/overviews/collections/conversions-between-...
>>
>> >> -jason
Mon, 2011-12-19, 20:11
#6
Re: Get this java type signature working in scala
sure thing. Additionally not only a normal developer is overwhelmed
also the (presentation) compiler at least thats what I see in the
current eclipse 2.0 RC4 :/
On Dec 19, 7:15 pm, Michael Schmitz wrote:
> Yeah, I explicitly specify the types with CoreMap because otherwise it
> gets too confusing...
>
>
>
>
>
>
>
> On Mon, Dec 19, 2011 at 10:10 AM, Tobias Neef wrote:
> > Hi Michael,
>
> > thx for your help. I wonder why the compiler can not figure out the
> > type parameters from the variable definition. But it works like you
> > described you can even remove the variable type spec. when having he
> > type parameters.
>
> > val tokens = document.get[
> > java.util.List[CoreMap],
> > SentencesAnnotation
> > ] (classOf[SentencesAnnotation])
>
> > Regards,
> > Tobias
>
> > On Dec 19, 6:39 pm, Michael Schmitz wrote:
> >> Hi Tobias, if you look at the JavaDoc for methods that return CoreMap,
> >> you will notice that something funny is going on. The following is
> >> copied out of some working code. The map step goes beyond your
> >> question, but it's useful as another example.
>
> >> val document = new Annotation(text);
>
> >> corenlp.annotate(document);
>
> >> val tokens: Array[Array[CoreLabel]] = document.get[
> >> java.util.List[CoreMap],
> >> SentencesAnnotation
> >> ] (classOf[SentencesAnnotation]).map {sentence =>
> >> sentence.get[
> >> java.util.List[CoreLabel],
> >> TokensAnnotation
> >> ](classOf[TokensAnnotation]).toList.toArray
> >> }.toArray
>
> >> Note the type parameters to document.get. In Java these are not
> >> necessary as you noted in your example. I would love to know why type
> >> parameters are required in Scala but not in Java.
>
> >> Peace. Michael
>
> >> On Sun, Dec 18, 2011 at 4:05 PM, Tobias Neef wrote:
> >> > Hi Jason,
> >> > sorry I forgot to mention that I did import java.util.List whichshould
> >> > override the implicit scala.List import. But now I also triedit with
> >> > the alias and it still does not work. The error message is
> >> > thefollowing:
> >> > inferred type arguments[Nothing,edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation ]
> >> > donot conform to method get's type parameter bounds [VALUE,KEY
> >> > <:edu.stanford.nlp.util.TypesafeMap.Key[edu.stanford.nlp.util.CoreMap,VALUE ]]
> >> > On Mon, Dec 19, 2011 at 12:43 AM, Jason Zaugg wrote:
> >> >> On Mon, Dec 19, 2011 at 9:37 AM, Tobias Neef wrote:
>
> >> >>> Hi,
>
> >> >>> I was about to test the StanfordCoreNLP lib within a Scala project and
> >> >>> failed at my first attempt to translate the following code:
>
> >> >>> ...
> >> >>> StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
> >> >>> Annotation document = new Annotation("this is a nice sentence.");
> >> >>> pipeline.annotate(document);
> >> >>> List sentences = document.get(SentencesAnnotation.class);
>
> >> >>> My translation:
> >> >>> ...
> >> >>> val pipeline = new StanfordCoreNLP(props);
> >> >>> val document = new Annotation("this is a nice sentence.");
> >> >>> pipeline.annotate(document);
> >> >>> val list:List[CoreMap] = document.get(classOf[TokensAnnotation])
>
> >> >>> and some variations of it do not work out. The signature can be found
> >> >>> here
> >> >>>http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/util/Typ...)
>
> >> >> `List` is `scala.List`. This isn't the same as `java.util.List`.
>
> >> >> You can import it with an alias:
>
> >> >> import java.util.{List => JList}
> >> >> val list: JList[CoreMap] = ...
>
> >> >> You might like to convert this to a Scala collection; if so read the section
> >> >> "Converting between Java and Scala Collections" in the collections guide:
>
> >> >>http://docs.scala-lang.org/overviews/collections/conversions-between-...
>
> >> >> -jason
`List` is `scala.List`. This isn't the same as `java.util.List`.
You can import it with an alias:
import java.util.{List => JList}val list: JList[CoreMap] = ...
You might like to convert this to a Scala collection; if so read the section "Converting between Java and Scala Collections" in the collections guide:
http://docs.scala-lang.org/overviews/collections/conversions-between-java-and-scala-collections.html
-jason