Code:
public class PerChance {/*
* extend me and add sentences and lists for parameters in the sentences in the
* sub classes c'tor.
replicate speech paterns, generate movie scripts or books and enjoy
*/
protected ArrayList<String> sentences = new ArrayList<String>();
protected Hashtable<String, UniqueItemSizeLimitedPriorityQueue> wordToList = new Hashtable<>();
protected Random rand = new Random();
private RegexUtil regexUtil = new RegexUtil();
public PerChance() {
super();
}
public String generateJoke() {
int x = rand.nextInt(sentences.size());
String result = sentences.get(x);
return clearRecursion(result);
}
private String clearRecursion(String result) {
int x;
ArrayList<String> params = new ArrayList<String>();
params = regexUtil.extractAllRegexes("(\\w+)(?= #)", result);
for (String strI : params) {
UniqueItemSizeLimitedPriorityQueue temp = wordToList.get(strI);
String s1 = temp.getRNDElement();
result = result.replace(strI + " #", s1);
}
if (!result.contains("#")) {
return result;
} else {
return clearRecursion(result);
}
}
public void addParam(String category, String value){
if(wordToList.containsKey(category)){
wordToList.get(category).add(value);
}
}
public void addParam(AXKeyValuePair kv){
if(wordToList.containsKey(kv.getKey())){
wordToList.get(kv.getKey()).add(kv.getValue());
}
}
}
Code:
public class PerChanceTest extends PerChance{
public PerChanceTest() {
super();
sentences.add("here is a salad vegi1 #, vegi2 # and herb #.");
sentences.add("how about this salad vegi1 #, vegi2 # and herb #. it goes well with tuna fish");
UniqueItemSizeLimitedPriorityQueue temp = new UniqueItemSizeLimitedPriorityQueue();
temp.setLimit(3);
wordToList.put("vegi1", temp);
temp = new UniqueItemSizeLimitedPriorityQueue();
temp.setLimit(3);
wordToList.put("vegi2", temp);
temp = new UniqueItemSizeLimitedPriorityQueue();
temp.setLimit(3);
wordToList.put("herb", temp);
}
}
main
Code:
PerChanceTest pct = new PerChanceTest();
pct.addParam("vegi1", "tomato");
pct.addParam("vegi1", "cucmber");
pct.addParam("vegi1", "lettuce");
pct.addParam("vegi2", "onion");
pct.addParam("vegi2", "pickle");
pct.addParam("vegi2", "purple onion");
pct.addParam("herb", "parcely");
pct.addParam("herb", "cpriander");
pct.addParam("herb", "irit");
for (int i = 0; i < 10; i++) {
System.out.println(pct.generateJoke());
}
output:
here is a salad cucmber, purple onion and cpriander.
here is a salad tomato, purple onion and irit.
here is a salad lettuce, pickle and cpriander.
how about this salad tomato, purple onion and cpriander. it goes well with tuna fish
how about this salad lettuce, pickle and irit. it goes well with tuna fish
how about this salad lettuce, pickle and parcely. it goes well with tuna fish
how about this salad tomato, onion and cpriander. it goes well with tuna fish
how about this salad cucmber, pickle and parcely. it goes well with tuna fish
here is a salad cucmber, purple onion and parcely.
here is a salad cucmber, purple onion and irit.
Process finished with exit code 0
new params can be learned during run time (hence the input filter to make it even simpler)