3

Scala beginner here, I'm trying to find all the tweets text that contain at least one keyword in the list of keywords given.

Where a tweet:

case class Tweet(user: String, text: String, retweets: Int)

With an example Tweet("user1", "apple apple", 3)

Given that wordInTweet should return true if at least one keyword in the list keywords can be found in the tweet's text.

I tried implementing it like the following:

def wordInTweet(tweet: Tweet, keywords: List[String]): Boolean = {
    keywords.exists(tweet.text.equals(_))
}

But, it also returns true if a tweet's text is music and a given keyword's text is musica.

I'm struggling to find a way to return true ONLY if the tweets contains the exact same keyword's text.

How can I achieve this?

Thanks in advance.

1
  • 1
    It looks like the below answered worked for you, but consider List#sliding: scala> "my tweet".sliding("my".size).toList res2: List[String] = List(my, "y ", " t", tw, we, ee, et) Commented Dec 22, 2016 at 20:08

1 Answer 1

4

First, it would help if you consider the keywords as a Set, given that sets have a very efficient belongs function.

keywords: Set[String]

Then we need to test every word in the tweet, as opposed to the complete text. This means that we need to split the text into words. We find an example of that everywhere with the ubiquitous "wordCount" example.

val wordsInTweet = tweet.text.split("\\W")

Next, we put things together:

def wordInTweet(tweet: Tweet, keywords: Set[String]): Boolean = {
   val wordsInTweet = tweet.text.split("\\W")
   wordsInTweet.exists(word => keywords.contains(word))
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer @maasg. Unluckily, I cannot change the method signature, it was given to me like that, so, I can't change it to a Set[String]. And, leaving it a List[String] I get: Type mismatch, expected: (String) => Boolean, actual (String) => String.
Then use the contains function of List.

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.