Arya
Arya

Reputation: 8985

select a word from a section of string?

I'm trying to find out if there are any methods in Java which would me achieve the following.

I want to pass a method a parameter like below

"(hi|hello) my name is (Bob|Robert). Today is a (good|great|wonderful) day."

I want the method to select one of the words inside the parenthesis separated by '|' and return the full string with one of the words randomly selected. Does Java have any methods for this or would I have to code this myself using character by character checks in loops?

Upvotes: 4

Views: 445

Answers (6)

Mayank
Mayank

Reputation: 816

Java does not provide any readymade method for this. You can use either Regex as described by Penartur or create your own java method to split Strings and store random words. StringTokenizer class can help you if following second approach.

Upvotes: 0

Shalini
Shalini

Reputation: 1733

May be it helps,

Pass three strings("hi|hello"),(Bob|Robert) and (good|great|wonderful) as arguments to the method.

Inside method split the string into array by, firststringarray[]=thatstring.split("|"); use this for other two.

and Use this to use random string selection.

Upvotes: 5

Kinjal Dixit
Kinjal Dixit

Reputation: 7935

There is no direct support for this. And you should ideally not try a low level solution.

You should search for 'random sentence generator'. The way you are writing

`(Hi|Hello)`

etc. is called a grammar. You have to write a parser for the grammar. Again there are many solutions for writing parsers. There are standard ways to specify grammar. Look for BNF.

The parser and generator problems have been solved many time over, and the interesting part of your problem will be writing the grammar.

Upvotes: 0

penartur
penartur

Reputation: 9912

You can parse it by regexes.

The regex would be \(\w+(\|\w+)*\); in the replacement you just split the argument on the '|' and return the random word.

Something like

import java.util.regex.*;

public final class Replacer {

  //aText: "(hi|hello) my name is (Bob|Robert). Today is a (good|great|wonderful) day."
  //returns: "hello my name is Bob. Today is a wonderful day."
  public static String getEditedText(String aText){
    StringBuffer result = new StringBuffer();
    Matcher matcher = fINITIAL_A.matcher(aText);
    while ( matcher.find() ) {
      matcher.appendReplacement(result, getReplacement(matcher));
    }
    matcher.appendTail(result);
    return result.toString();
  }

  private static final Pattern fINITIAL_A = Pattern.compile(
    "\\\((\\\w+(\\\|\w+)*)\\\)",
    Pattern.CASE_INSENSITIVE
  );

  //aMatcher.group(1): "hi|hello"
  //words: ["hi", "hello"]
  //returns: "hello"
  private static String getReplacement(Matcher aMatcher){
    var words = aMatcher.group(1).split('|');
    var index = randomNumber(0, words.length);
    return words[index];
  }

} 

(Note that this code is written just to illustrate an idea and probably won't compile)

Upvotes: 6

Turix
Turix

Reputation: 4490

I don't think Java has anything that will do what you want directly. Personally, instead of doing things based on regexps or characters, I would make a method something like:

String madLib(Set<String> greetings, Set<String> names, Set<String> dispositions)
{
    // pick randomly from each of the sets and insert into your background string
}

Upvotes: 0

Pramod Kumar
Pramod Kumar

Reputation: 8014

As per my knowledge java don't have any method to do it directly.

I have to write code for it or regexe

Upvotes: 1

Related Questions