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 7544699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:44:24+00:00 2026-05-30T08:44:24+00:00

Folks, I’ve been writing some code in Scala lately to teach myself the language

  • 0

Folks,
I’ve been writing some code in Scala lately to teach myself the language and in some recent experiments, I’ve used an NLP library to produce a set of part-of-speech tagged words from a user’s input.

I want to write a function that gives me the first verb in the sentence. If there are no verbs, then I want to assume that the first word in the set is the verb (e.g. if the player just typed “who” or “uptime” , those are considered verbs by my game).

The following is a block of code so ugly only a mother could love, and it stinks of imperative programming and I want to refactor it into something more like idiomatic Scala, ideally something that doesn’t have a single “if” statement in it.

def firstVerb = {
    if (words.size == 1)
        words.head.value
    else {
        val outWords = words.filter( word => word.pos == Verb)
        if (outWords == Set.empty) 
            words.head.value
        else
            outWords.head.value 
    }
}

The “words” variable is of type ListBuffer[EnrichedWord], where EnrichedWord is my class that contains a part of speech (pos, contains case objects like Verb, Noun, etc) and the original word (value).

Any guidance you Scala geniuses can provide in refactoring this butt-ugly code would be fantastic.

  • 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-30T08:44:26+00:00Added an answer on May 30, 2026 at 8:44 am

    This additionally handles the case when words is empty, try it:

    words.find(_.pos == Verb).orElse(words.headOption).map(_.value).getOrElse("")
    

    If you are sure words will never be an empty Set, this one is simpler:

    words.find(_.pos == Verb).getOrElse(words.head).value
    

    BTW if you are using HashSet the notion of some element being first doesn’t really make sense. If each element represents a word in a sentece, it should have been a List or a Seq.

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

Sidebar

Related Questions

Folks, We have been using OpenXml APIs found in System.IO.Packaging for creating a package
Folks, I really like the -Wshadow option since it helps to spot some possible
Folks, I have a problem. I am a writing a script in python which
Folks, I'm trying to open a youtube video in fancybox using the following code:
Folks, I have some HTML generated from my WSDL and XSD files for a
Folks, Apologies if this has been covered in another thread, but I have searched
Folks, I had a quick question about Amazon EC2, I have been recently using
Folks, Used to be that when you hit a servlet/jsp, the app server would
Folks, I am completely new to jQuery and JqGrid both. Usually I take some
Folks, I've a large legacy .Net code base, and I'm trying to introduce Unit

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.