Execute a Markov algorithm: Difference between revisions

Content added Content deleted
(→‎{{header|D}}: never mind)
(→‎{{header|Java}}: added Java)
Line 1,809: Line 1,809:
</lang>
</lang>
'''Discussion''': The J implementation correctly processes all the rulesets. More details are available on the [[Talk:Markov Algorithm#explicit_vs_tacit|the talk page]].
'''Discussion''': The J implementation correctly processes all the rulesets. More details are available on the [[Talk:Markov Algorithm#explicit_vs_tacit|the talk page]].

=={{header|Java}}==
{{trans|D}}
{{works with| Java 7}}
<lang java>import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Markov {

public static void main(String[] args) throws IOException {

List<String[]> rules = readRules("markov_rules.txt");
List<String> tests = readTests("markov_tests.txt");

Pattern pattern = Pattern.compile("^([^#]*?)\\s+->\\s+(\\.?)(.*)");

for (int i = 0; i < tests.size(); i++) {
String origTest = tests.get(i);

List<String[]> captures = new ArrayList<>();
for (String rule : rules.get(i)) {
Matcher m = pattern.matcher(rule);
if (m.find()) {
String[] groups = new String[m.groupCount()];
for (int j = 0; j < groups.length; j++)
groups[j] = m.group(j + 1);
captures.add(groups);
}
}

String test = origTest;
String copy = test;
for (int j = 0; j < captures.size(); j++) {
String[] c = captures.get(j);
test = test.replace(c[0], c[2]);
if (c[1].equals("."))
break;
if (!test.equals(copy)) {
j = -1; // redo loop
copy = test;
}
}
System.out.printf("%s\n%s\n\n", origTest, test);
}
}

private static List<String> readTests(String path)
throws IOException {
return Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8);
}

private static List<String[]> readRules(String path)
throws IOException {
String ls = System.lineSeparator();
String lines = new String(Files.readAllBytes(Paths.get(path)), "UTF-8");
List<String[]> rules = new ArrayList<>();
for (String line : lines.split(ls + ls))
rules.add(line.split(ls));
return rules;
}
}</lang>

Output:

<pre>I bought a B of As from T S.
I bought a bag of apples from my brother.

I bought a B of As from T S.
I bought a bag of apples from T shop.

I bought a B of As W my Bgage from T S.
I bought a bag of apples with my money from T shop.

_1111*11111_
11111111111111111111

000000A000000
00011H1111000</pre>


=={{header|Lua}}==
=={{header|Lua}}==