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.

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

5 Comments

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
Then that means it doesn't have a text field/method at all. Check their API.
Thanks for the help with this. The text field should be there, it's the 2nd example in the docs, and it is there in the Sentence definition. 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?
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].
Apparently one can get sentences from nlp 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.