I've been trying to implement Markov's algorithm, but I've only had partial success. The algorithm is fairly simple and can be found here.
However, my project has an added difficulty, I have to use rules that include markers and variables.
A variable represents any letter in the alphabet and a marker is simply a character that is used as a reference to move the variables around (It doesn't have a real value).
This example duplicates every character in a string:
Alphabet: {a,b,c}
Markers: {M}
Variables: {x}
Rule 1: Mx -> xxM
Rule 2: xM -> x
Rule 3: x -> Mx
input: abc
abc //We apply rule 3
Mabc //We apply rule 1
aaMbc //We apply rule 1
aabbMc //We apply rule 1
aabbccM //We apply rule 2
aabbcc
This is my recursive function that implements a markov algorithm that only works with string inputs for example: Rule 1: "apple" -> "orange", Input: "apple".
public static String markov(String input, LinkedList<Rule> rules) {
for (Rule rule : rules) {
if (!input.equals(input.replace(rule.getFrom(), rule.getTo()))) { //If the rule matches a substring
if (rule.isTerminating()) { //If the rule is terminating
input = input.replaceFirst(Pattern.quote(rule.getFrom()), rule.getTo());
System.out.println(input); //Replace the first instance
return input; //return and end the cycle
} else {
input = input.replaceFirst(Pattern.quote(rule.getFrom()), rule.getTo());
System.out.println(input);
return markov(input, rules); //Start looking again for matching rules
}
}
}
return input;
}
I can't figure out how to implement variables and markers into this logic so perhaps someone can educate me on the best way to implement this logic? any advice is welcome.
If the question doesn't comply with SO guidelines please let me know why in the comments so I don't repeat the mistake.
Thank You!