Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6838479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:36:08+00:00 2026-05-26T23:36:08+00:00

object NGram{ def main(args: Array[String]) { //args(0) = textfile //args(1) = size of n-grams

  • 0
object NGram{
    def main(args: Array[String]) {
      //args(0) = textfile //args(1) = size of n-grams //args(2) = the number of words to generate
        val F = scala.io.Source.fromFile(args(0)) // take from args[0]
        for (line <- F.getLines()){
        val words = line.split("[ ,:;.?!-]+") map (_.toLowerCase)
        var ngram : Set[String] = Set()
        //make n-gram
        for(i <- 0 to words.size - args(1)) {
          // first make sequence by args(1)
          for(j <- i until i + args(1)){
            ngram = ngram + words(j) // not works it is my problem stage
          }


          }
        }
    }
}

I made n-gram algorithm by using scala. at first

  1. make string sequence, and check it is in original string.
  2. and It is efficiently works.

I want n string sequence not duplicated (because it must work efficiently)

How to make n string sequence by map?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:36:08+00:00Added an answer on May 26, 2026 at 11:36 pm

    Am I correct that :

    • you have a sequence of words (not sure from your code whether it should be a single line or the full file)
    • an n-gram is a sequence of n words consecutive in the original sequence
    • you want a certain number of distinct n-gram.

    There is a routine that will give you n-grams, it is sliding.
    with

    val words = Seq("the", "brown", "fox", "jumps", "over", "the", "lazy", "dog")
    val trigrams = words.sliding(3).toSeq
    foreach(triGram in triGrams) println(triGram.mkString(" "))
    
    the brown fox
    brown fox jumps
    fox jumps over
    jumps over the
    over the lazy
    the lazy dog
    

    There is a caveat, if you have only p words and want n-grams, with n > p, sliding will return one p-gram (not an n-gram obviously) rather than none. So you have to check for that.

    You can do toSet rather than toSeq to eliminate duplicates.

    There is the last point, you want only a certain number of n-grams (your last argument). You did not specify how you want to select them. The simple way would be a take. To avoid to go through the whole list of words, and take the count first distinct one, that would be

    words.sliding(n).toStream.distinct.take(count)
    

    If you want to take them at random position, that is a different story and maybe sliding is not the way to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

object NoSense { def main(args: Array[String]) { val value = true match { case
object TestClass { def main (args: Array[String]) { println(Hello World); val c = List
object JDWhileLoop { def main(args: Array[String]) { var index:Int = 0 while( index<=10) {
object(stdClass)[1] public 'inbox' => array 0 => object(stdClass)[2] public 'from' => string '55512351' (length=8)
object(SimpleXMLElement)#1 (3) { [@attributes]=> array(1) { [responsecode]=> string(3) 200 } [nextpage]=> object(SimpleXMLElement)#2 (0) {
Some object property/array manipulation. Is there better syntax to accomplish part 2 and 3?
object obj = Hello; string str1 = (string)obj; string str2 = obj.ToString(); What is
object o; Is there any difference between o.ToString() and (string) o ?
object combinedrows = from dt1 in DsResults.Tables[0].AsEnumerable() join dt2 in DsResults.Tables[1].AsEnumerable() on dt1.Field<string>(MethodName) equals
object(stdClass)#25 (7) { [store_id]=> string(2) 27 [account_id]=> string(1) 5 [store_date_created]=> string(19) 2011-01-31 02:40:38 [options]=>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.