Parv bali
Parv bali

Reputation: 157

obtaining Array[Boolean] in place of Array[int] in scala

When I am executing this code, I must get Array[Int] consisting of 1 and 0. But the output I am obtaining is Array[Boolean] consisting of true and false. This is creating trouble for further execution. I want to get 0 and 1 only what should i do?

val reviewSenti = extracted_reviews.map(reviewText => {
  val reviewWordsSentiment = reviewText.getString(0).split(" ").map(word => {

    var senti: Int = 0;

    if (AFINN.lookup(word.toLowerCase()).length > 0) {

      senti = AFINN.lookup(word.toLowerCase())(0)

    };

    senti;

  });

  val reviewSentiment = reviewWordsSentiment.sum

  (reviewSentiment)

  if (reviewSentiment >= 3){
    reviewSentiment == 1;
  }
  else {
    reviewSentiment == 0;
  }
})

reviewSenti: Array[Boolean] = Array(false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, ...

Upvotes: 1

Views: 646

Answers (2)

Marko Švaljek
Marko Švaljek

Reputation: 2101

Not sure about some of the context of your code. So I might be wrong in some of the assumptions. In the first part of your code you might want to avoid double calling of AFINN.lookup(word.toLowerCase()) so basically you could map with something like:

  val afinnLookup = AFINN.lookup(word.toLowerCase())

  if (afinnLookup.length > 0)
    afinnLookup(0)
  else
    0

then again I'm not sure about what you get back from AFFIN but you could use pattern matching to avoid looking into length and writing it with if:

affinLookup match {
  case head :: tail ⇒ head
  case _ ⇒ 0
}

or you could even do this with headOption: Then you would return an Option[Int] but then again it might follow the intention of the library i.e. there is no affinity or something like that. Basically it could be as short as:

val reviewWordsSentiment = reviewText.getString(0).split(" ").map(
    word => AFINN.lookup(word.toLowerCase()).headOption)

This would return you Option[Int] this may or may not complicate things for you. If you decide to go down that path here is a very nice guide: http://danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html

Or you can avoid Option[Int] by using fold(0){identity} or getOrElse(0) after headOption by something like:

val reviewWordsSentiment = reviewText.getString(0).split(" ").map(
    word => AFINN.lookup(word.toLowerCase()).headOption.getOrElse(0))

This will enable you to do sum later and you can even skip assigning it a value you might just need something like at the end.

if (reviewWordsSentiment.sum >= 3) 1 else 0

Again this is with relatively a lot of assumptions about the code and problem that you provided. I hope this helps or provides some guidelines.

Upvotes: 1

Vasilisck
Vasilisck

Reputation: 105

try to make reviewSentiment == 1; and another one to just 1 and 0. This will probably help you.

just like

if (reviewSentiment >= 3) 1 else 0

Upvotes: 2

Related Questions