Execute a Markov algorithm: Difference between revisions

Content added Content deleted
(→‎Rulesets: More than three, currently.)
(→‎{{header|Python}}: Comment on flexibility and addition of latest ruleset.)
Line 533: Line 533:
The example uses a regexp to parse the syntax of the grammar. This regexp is multi-line and verbose, and uses named groups to aid in understanding the regexp and to allow more meaningful group names to be used when extracting the replacement data from the grammars in function <code>extractreplacements</code>.
The example uses a regexp to parse the syntax of the grammar. This regexp is multi-line and verbose, and uses named groups to aid in understanding the regexp and to allow more meaningful group names to be used when extracting the replacement data from the grammars in function <code>extractreplacements</code>.


The example gains flexibility by not being tied to specific files. The functions may be imported into other programs which then can provide textual input from their sources without the need to pass 'file handles' around.
<lang python>import re
<lang python>import re

def extractreplacements(grammar):
return [ (matchobj.group('pat'), matchobj.group('repl'), bool(matchobj.group('term')))
for matchobj in re.finditer(syntaxre, grammar)
if matchobj.group('rule')]
def replace(text, replacements):
while True:
for pat, repl, term in replacements:
if pat in text:
text = text.replace(pat, repl, 1)
if term:
return text
break
else:
return text


syntaxre = r"""(?mx)
syntaxre = r"""(?mx)
Line 542: Line 559:
)$
)$
"""
"""

grammar1 = """\
grammar1 = """\
# This rules file is extracted from Wikipedia:
# This rules file is extracted from Wikipedia:
Line 553: Line 570:
a never used -> .terminating rule
a never used -> .terminating rule
"""
"""

grammar2 = '''\
grammar2 = '''\
# Slightly modified from the rules on Wikipedia
# Slightly modified from the rules on Wikipedia
Line 563: Line 580:
a never used -> .terminating rule
a never used -> .terminating rule
'''
'''

grammar3 = '''\
grammar3 = '''\
# BNF Syntax testing rules
# BNF Syntax testing rules
Line 607: Line 624:
1+_ -> 1
1+_ -> 1
_+_ ->
_+_ ->
'''

grammar5 = '''\
# Turing machine: three-state busy beaver
#
# state A, symbol 0 => write 1, move right, new state B
A0 -> 1B
# state A, symbol 1 => write 1, move left, new state C
0A1 -> C01
1A1 -> C11
# state B, symbol 0 => write 1, move left, new state A
0B0 -> A01
1B0 -> A11
# state B, symbol 1 => write 1, move right, new state B
B1 -> 1B
# state C, symbol 0 => write 1, move left, new state B
0C0 -> B01
1C0 -> B11
# state C, symbol 1 => write 1, move left, halt
0C1 -> H01
1C1 -> H11
'''
'''


text1 = "I bought a B of As from T S."
text1 = "I bought a B of As from T S."

text2 = "I bought a B of As W my Bgage from T S."
text2 = "I bought a B of As W my Bgage from T S."


text3 = '_1111*11111_'
text3 = '_1111*11111_'


text4 = '000000A000000'
def extractreplacements(grammar):
return [ (matchobj.group('pat'), matchobj.group('repl'), bool(matchobj.group('term')))
for matchobj in re.finditer(syntaxre, grammar)
if matchobj.group('rule')]

def replace(text, replacements):
while True:
for pat, repl, term in replacements:
if pat in text:
text = text.replace(pat, repl, 1)
if term:
return text
break
else:
return text


if __name__ == '__main__':
if __name__ == '__main__':
assert replace(text1, extractreplacements(grammar1)) \
assert replace(text1, extractreplacements(grammar1)) \
Line 636: Line 661:
assert replace(text1, extractreplacements(grammar2)) \
assert replace(text1, extractreplacements(grammar2)) \
== 'I bought a bag of apples from T shop.'
== 'I bought a bag of apples from T shop.'
# Stretch goals
assert replace(text2, extractreplacements(grammar3)) \
assert replace(text2, extractreplacements(grammar3)) \
== 'I bought a bag of apples with my money from T shop.'
== 'I bought a bag of apples with my money from T shop.'
assert replace(text3, extractreplacements(grammar4)) \
assert replace(text3, extractreplacements(grammar4)) \
== '11111111111111111111'
== '11111111111111111111'
assert replace(text4, extractreplacements(grammar5)) \
</lang>
== '00011H1111000'</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==