1. Overview

String manipulation is a common task in programming. Whether we’re working on text analysis, log filtering, or user input validation, checking if a given string contains specific keywords is a frequent requirement. Consider the following Scala code snippet:

val text = "May the Force be with you"
val keys = List("Force", "Wakanda", "Hobbit")

Here, we have a string and a list of keywords. The goal is to check if the text contains any of the words in keys. In Scala, we can leverage its rich set of built-in functions instead of rolling our code. In this tutorial, we’ll delve into how to efficiently perform keyword matching in strings using Scala’s standard library.

2. The Brute-Force Approach

A straightforward way to check this in an imperative approach would be to split the text and loop through the words and keywords:

val a = "May the Force be with you"
val keys = List("Force", "Wakanda", "Hobbit")

val words = a.split(" ")
var containsKeyword = false

for (word <- words) {
  for (key <- keys) {
    if (word == key) {
      containsKeyword = true

The time complexity is O(n×m) because of the nested loops over the words O(n) and keys O(m).

3. Improving with Scala’s Built-in Methods

While the brute-force approach is straightforward, Scala provides built-in methods that make the code more idiomatic and concise. One such method is exists, which can simplify the nested loops:

val text = "May the Force be with you"
val keys = List("Force", "Wakanda", "Hobbit")

val containsKeyword = keys.exists(key => text.split(" ").contains(key))

Here, we use exists to iterate through the keys list and contains to check if each keyword exists in the split text. This eliminates the need for explicit loops and makes the code more readable.

The code is more idiomatic, but the time complexity remains O(n×m) because the methods are internally looping over the same data.

3.1. Avoiding Splitting the Text Explicitly

Scala also offers the containsSlice method:

val containsKeyword = keys.exists(key => text.containsSlice(key))

While containsSlice offers a more concise way to check for the presence of a substring, it’s worth noting that the time complexity remains O(n×m), similar to the brute-force approach.

4. Functional Programming Techniques

Scala’s functional programming support offers various techniques to make our code more idiomatic and concise. While these techniques may not improve the time complexity for this specific problem, they can make the code more readable and maintainable.

4.1. Using foldLeft

Instead of exists and contains, we can use foldLeft to accumulate results:

val text = "May the Force be with you"
val keys = List("Force", "Wakanda", "Hobbit")

val words = text.split(" ")
val containsKeyword = keys.foldLeft(false) { (acc, key) =>
  acc || words.contains(key)

In this example, foldLeft starts with an initial false value and iterates through the keys list. The accumulator acc becomes true if any keys are found in the word array.

4.2. Using reduce

If our list of keys is non-empty, we can also use reduce to achieve the same result:

val containsKeyword = keys.map(key => words.contains(key)).reduce(_ || _)

Here, we transform the keys list into a Boolean value list that indicates whether each key is contained in the words array. Then, we use reduce to combine these Boolean values using the logical OR operator.

4.3. Time Complexity

While these functional programming techniques make the code more idiomatic and concise, it’s important to note that they do not improve the time complexity, which remains O(n×m).

Another disadvantage is that foldRight and reduce won’t short-circuit, making them less efficient in real-life applications.

4.4. Using foldLeft for Keyword Counting

Let’s say we’re dealing with a more complex requirement, and instead of just checking for the existence of keywords, we want to count their occurrences in the text:

val text = "May the Force be with you, Force again"
val keys = List("Force", "Wakanda", "Hobbit")

val words = text.split(" ")

val keywordCounts = keys.foldLeft(Map.empty[String, Int]) { (acc, key) =>
  val count = words.count(_ == key)
  acc + (key -> count)

The functional approach shines when we need to extend functionality. With a few lines of code, we could transition from checking keyword existence to counting keyword occurrences, all while keeping the code concise and readable.

4.5. Note on foldRight

We might wonder if foldRight could offer any advantages in this context. In functional programming, foldRight is often discussed for its utility with lazy evaluation and infinite data structures. However, in this specific problem, neither the data (List and Array are eagerly evaluated in Scala) nor the operations (checking for string existence) are lazy.

Therefore, using foldRight would offer no benefits regarding time complexity or result and is potentially less efficient due to its standard implementation’s lack of tail recursion.

5. Parallel Processing

Scala’s rich standard library and compatibility with Java’s concurrency libraries make it a strong candidate for parallel processing tasks. In the context of our keyword-matching problem, parallel processing can be beneficial when dealing with large texts or keyword lists.

Scala provides an easy way to parallelize collections using the par method. Let’s see how we could use it to parallelize the keyword counting:

val url = "https://www.gutenberg.org/files/1342/1342-0.txt"
val text = Source.fromURL(url).mkString

val keywords = List("Darcy", "Bennet", "Pemberley", "Bingley", "Wickham")

// Split the text into words and transform it into a parallel collection
val words = text.split("\\W+").par

// Count occurrences of each keyword
val counts = keywords.par.map { keyword =>
  keyword -> words.count(_ == keyword)
}.seq // Convert back to a sequential map for consistent output

counts.foreach { case (keyword, count) =>
  println(s"$keyword: $count")

Scala will automatically parallelize the map operation by simply converting the keys list to a parallel collection with par.

Using par converts the keywords into a parallel collection, ensuring the map operations are executed in parallel. Many keywords could be counted simultaneously, depending on the number of available cores.

5.1. When to Use Parallel Processing

The time complexity remains O(n×m), but the actual time taken can be significantly reduced, depending on the number of processor cores available.

Parallel processing introduces overheads, such as thread management and context switching, which might make it less suitable for smaller datasets. Therefore, it is most effective with a large dataset and multiple processor cores because it

Parallel processing becomes especially beneficial for tasks like keyword counting in large datasets. Counting occurrences of each keyword in the text is independent of the others, making it an “embarrassingly parallel” problem. This means you can achieve near-linear speedup by dividing the task among multiple processor cores. This is why parallel keyword counting can significantly boost performance: Each term appearance can be performed independently.

6. Optimizing with Scala Collections

Scala’s collections library offers data structures that can help us improve the efficiency of our code. One such data structure is Set, which allows for constant-time lookups.

val text = "May the Force be with you"
val keys = List("Force", "Wakanda", "Hobbit").toSet

val words = text.split(" ").toSet
val containsKeyword = keys.exists(key => words.contains(key))

By converting both the keys list and the words array to Sets, we can take advantage of constant-time lookups, making the exists method more efficient.

6.1. Time Complexity

The time complexity for this approach is O(n+m), where O(n) is the time to convert the text to a Set of words, and O(m) is the time to convert the keys list to a Set. The exists method performs in O(m) time, but each lookup is O(1) thanks to the Set data structure.

This is a significant improvement over the previous O(n×m) time complexity.

7. Conclusion

This article navigated through multiple Scala techniques for checking if a string contains keywords from a list. We started with a brute-force method, transitioned to idiomatic Scala solutions, and even touched on the benefits of functional programming.

Scala’s Set data structure optimized our time complexity to O(n+m). Additionally, we explored how parallel processing can significantly speed up tasks like keyword counting in large datasets. These approaches offer a balanced mix of performance and readability for Scala projects.

As always, the full source code for the examples is available over on GitHub.

Comments are closed on this article!