Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,102 questions

55,976 answers

573 users

How to find the N most frequent non‑stopwords in a text in Scala

2 Answers

0 votes
/*
    This program finds the N most frequently appearing words in a text
    after removing stopwords. It demonstrates clean structure, clear
    comments, and efficient use of Scala collections and sorting.
*/

object TopWordsScala {

  // ---------------------------------------------------------------
  // Tokenize text into words (simple whitespace split)
  // ---------------------------------------------------------------
  def tokenize(text: String): List[String] = {
    text
      .split("\\s+")
      .toList
      .map { w =>
        // Remove punctuation at the edges using regex
        val trimmed = w.replaceAll("^\\p{Punct}+|\\p{Punct}+$", "")
        trimmed.toLowerCase
      }
      .filter(_.nonEmpty)
  }
  // ---------------------------------------------------------------
  // Count word frequencies, skipping stopwords
  // ---------------------------------------------------------------
  def countWordsFrequencies(
      words: List[String],
      stopwords: Set[String]
  ): Map[String, Int] = {
    words
      .filterNot(stopwords.contains)
      .groupBy(identity)
      .view
      .mapValues(_.size)
      .toMap
  }

  // ---------------------------------------------------------------
  // Extract the top N most frequent words
  // ---------------------------------------------------------------
  def topN(freq: Map[String, Int], n: Int): List[(String, Int)] = {
    freq.toList
      .sortBy { case (word, count) => (-count, word) }
      .take(n)
  }

  // ---------------------------------------------------------------
  // Main
  // ---------------------------------------------------------------
  def main(args: Array[String]): Unit = {

    val text =
      "C is a general-purpose programming language created in 1972 by " +
      "Dennis Ritchie. C gives programmers direct access to the features " +
      "of CPU. It has been and continues to be used to implement " +
      "operating systems (especially kernels) and device " +
      "drivers. C programming language used on computers ranging from " +
      "supercomputers to microcontrollers and embedded systems."

    val stopwords: Set[String] = Set(
      "the","is","a","to","how","after","but","this","for","by","in",
      "and","can","content","be","you","yes","no","next","about","used",
      "access","been","continues"
    )

    // Tokenize
    val words = tokenize(text)

    // Count frequencies
    val freq = countWordsFrequencies(words, stopwords)

    // Get top n
    val n = 7
    val topn = topN(freq, n)

    // Print results
    println(s"Top $n most frequent non-stopwords:")
    topn.foreach { case (word, count) =>
      println(s"$word : $count")
    }
  }
}


/*
run:

Top 7 most frequent non-stopwords:
c : 3
language : 2
programming : 2
systems : 2
1972 : 1
computers : 1
cpu : 1

*/

 



answered Aug 31 by avibootz
0 votes
/*
    This program finds the N most frequently appearing words in a text
    after removing stopwords. It demonstrates clean structure, clear
    comments, and efficient use of Scala collections and sorting.
*/

object TopWordsScala {

  // ---------------------------------------------------------------
  // Tokenize text into words (simple whitespace split)
  // ---------------------------------------------------------------
  def isPunct(c: Char): Boolean =
    Character.getType(c) == Character.CONNECTOR_PUNCTUATION ||
    Character.getType(c) == Character.DASH_PUNCTUATION ||
    Character.getType(c) == Character.START_PUNCTUATION ||
    Character.getType(c) == Character.END_PUNCTUATION ||
    Character.getType(c) == Character.OTHER_PUNCTUATION

  def tokenize(text: String): List[String] = {
    text
      .split("\\s+")
      .toList
      .map { w =>
        val trimmedLeft  = w.dropWhile(isPunct)
        val trimmedBoth  = trimmedLeft.reverse.dropWhile(isPunct).reverse
        trimmedBoth.toLowerCase
      }
      .filter(_.nonEmpty)
  }

  // ---------------------------------------------------------------
  // Count word frequencies, skipping stopwords
  // ---------------------------------------------------------------
  def countWordsFrequencies(
      words: List[String],
      stopwords: Set[String]
  ): Map[String, Int] = {
    words
      .filterNot(stopwords.contains)
      .groupBy(identity)
      .view
      .mapValues(_.size)
      .toMap
  }

  // ---------------------------------------------------------------
  // Extract the top N most frequent words
  // ---------------------------------------------------------------
  def topN(freq: Map[String, Int], n: Int): List[(String, Int)] = {
    freq.toList
      .sortBy { case (word, count) => (-count, word) }
      .take(n)
  }

  // ---------------------------------------------------------------
  // Main
  // ---------------------------------------------------------------
  def main(args: Array[String]): Unit = {

    val text =
      "C is a general-purpose programming language created in 1972 by " +
      "Dennis Ritchie. C gives programmers direct access to the features " +
      "of CPU. It has been and continues to be used to implement " +
      "operating systems (especially kernels) and device " +
      "drivers. C programming language used on computers ranging from " +
      "supercomputers to microcontrollers and embedded systems."

    val stopwords: Set[String] = Set(
      "the","is","a","to","how","after","but","this","for","by","in",
      "and","can","content","be","you","yes","no","next","about","used",
      "access","been","continues"
    )

    // Tokenize
    val words = tokenize(text)

    // Count frequencies
    val freq = countWordsFrequencies(words, stopwords)

    // Get top n
    val n = 7
    val topn = topN(freq, n)

    // Print results
    println(s"Top $n most frequent non-stopwords:")
    topn.foreach { case (word, count) =>
      println(s"$word : $count")
    }
  }
}


/*
run:

Top 7 most frequent non-stopwords:
c : 3
language : 2
programming : 2
systems : 2
1972 : 1
computers : 1
cpu : 1

*/

 



answered Aug 31 by avibootz
...