0

I'm trying to use this javascript library from my scala.js app (simplified runnable example).

I can successfully use some parts of the api, but for other parts I'm having trouble determining the correct type signatures for my Scala facade.

For example, if a javascript function returns { text: 'June 5th 1998', ... } I can define scala.js classes to represent the function and the following succeeds:

class Value extends js.Object {
  def date(): js.Dictionary[Int] = js.native
}

object nlp extends js.Object {
  def sentences(text: String): js.Array[Sentence] = js.native
  def value(text: String): Value = js.native
}

nlp.value("I married April on June 6th 1998.").date()

However I have less luck if the javascript returns an array of the same (e.g.[{ text: ...}, { text: ...}]), or even if it returns a simple String (e.g."June 5th and June 6th" as the following compiles but fails at runtime with Uncaught TypeError: arg1$4.text is not a function:

class Sentence extends js.Object {
  def text(): String = js.native
  def values(): js.Array[Value] = js.native
}
val sentences = nlp.sentences(splittableText)
sentences.map( sentence => sentence.values() )
// Or `sentences.map( sentence => sentence.text() )`

How can I use this javascript api from scala.js?

Thanks very much for taking a look.

Julian Peeters
  • 853
  • 1
  • 6
  • 19

1 Answers1

1

text is a property (or field) in the objects returned by sentences. It's not a method. So you have to declare it as a def without ():

def text: String = js.native
sjrd
  • 21,805
  • 2
  • 61
  • 91
  • After making the change, I seem to get: `Uncaught scala.scalajs.runtime.UndefinedBehaviorError: An undefined behavior was detected: undefined is not an instance of java.lang.String` – Julian Peeters Nov 15 '15 at 20:38
  • Then that means it doesn't have a `text` field/method at all. Check their API. – sjrd Nov 15 '15 at 21:04
  • Thanks for the help with this. The `text` field should be there, it's the 2nd example in [the docs](https://github.com/spencermountain/nlp_compromise#sentence-methods), and it is there in the `Sentence` [definition](https://github.com/spencermountain/nlp_compromise/blob/master/src/sentence.js#L202). I wasn't sure how to represent the javascript `Sentence` function with a Scala `FunctionXX`, and so I represented it with a `Class` -- could that be playing a role here? – Julian Peeters Nov 15 '15 at 21:18
  • Your code above does not correspond to the example you link to. That example shows to access it as `nlp.pos(...).sentences[0]`, which means that *pos* (not *sentences*) returns a `js.Array[Sentence]`. – sjrd Nov 15 '15 at 21:23
  • Apparently one can [get sentences from `nlp`](https://github.com/spencermountain/nlp_compromise#sentence-segmentation) as well, but rewriting to use `pos` gave a different error: `Uncaught scala.scalajs.runtime.UndefinedBehaviorError: An undefined behavior was detected: function (){return b.tokens.map(function(a){return a.text}).join(" ")} is not an instance of java.lang.String` – Julian Peeters Nov 15 '15 at 21:59