I want to implement Markov's algorithm found here, but I haven't been able to. As the wiki explains it's a recursive function that replaces patterns within a language. For example
- "A" -> "apple"
- "B" -> "bag"
- "S" -> "shop"
- "T" -> "the"
- "the shop" -> "my brother"
- "a never used" -> ."terminating rule"
These rules must be implemented on the following text:
"I bought a B of As from T S."
Rules:
- Check the Rules in order from top to bottom to see whether any of the patterns can be found in the input string.
- If none is found, the algorithm stops.
- If one (or more) is found, use the first of them to replace the leftmost occurrence of matched text in the input string with its replacement.
- If the rule just applied was a terminating one, the algorithm stops.
- Go to step 1.
I thought about creating two classes Rule and RuleContainer.
Rule has 3 attributes: String from, String To and Boolean terminating
RuleContainer has a dynamic list containing the active rules and the logic function [The one I' trying to create].
I already took into consideration the String.replace() function and I tried to implement it into a recursive function.
What's the best way to implement Markov's algorithm?