Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -18,11 +18,7 @@
|
|||
[
|
||||
|
||||
markov-parse:
|
||||
]
|
||||
while dup input:
|
||||
pass
|
||||
[
|
||||
(markov-parse) (remove-comments)
|
||||
(markov-parse) (remove-comments) split !decode!utf-8 !read!stdin "\n"
|
||||
|
||||
(markov-tick) rules start:
|
||||
for rule in copy rules:
|
||||
|
|
@ -44,4 +40,4 @@ markov rules:
|
|||
while:
|
||||
not (markov-tick) rules
|
||||
|
||||
markov markov-parse
|
||||
!. markov markov-parse get-from !args 1
|
||||
|
|
|
|||
|
|
@ -0,0 +1,88 @@
|
|||
:- module('markov.pl', [markov/3, apply_markov/3]).
|
||||
|
||||
:- use_module(library(lambda)).
|
||||
|
||||
apply_markov(Rules, Sentence, Replacement) :-
|
||||
maplist(\X^Y^(atom_chars(X, Ch), phrase(markov(Y), Ch, [])), Rules, TmpRules),
|
||||
% comments produce empty rules
|
||||
exclude(=([]), TmpRules, LstRules),
|
||||
|
||||
atom_chars(Sentence, L),
|
||||
apply_rules(L, LstRules, R),
|
||||
atom_chars(Replacement, R).
|
||||
|
||||
apply_rules(In, Rules, Out ) :-
|
||||
apply_one_rule(In, Rules, Out1, Keep_On),
|
||||
( Keep_On = false
|
||||
-> Out = Out1
|
||||
; apply_rules(Out1, Rules, Out)).
|
||||
|
||||
|
||||
apply_one_rule(In, [Rule | Rules], Out, Keep_On) :-
|
||||
extract(Rule, In, Out1, KeepOn),
|
||||
( KeepOn = false
|
||||
-> Out = Out1, Keep_On = KeepOn
|
||||
; (KeepOn = stop
|
||||
-> Out = Out1,
|
||||
Keep_On = true
|
||||
; apply_one_rule(Out1, Rules, Out, Keep_On))).
|
||||
|
||||
apply_one_rule(In, [], In, false) .
|
||||
|
||||
|
||||
extract([Pattern, Replace], In, Out, Keep_On) :-
|
||||
( Replace = [.|Rest]
|
||||
-> R = Rest
|
||||
; R = Replace),
|
||||
( (append(Pattern, End, T), append(Deb, T, In))
|
||||
-> extract([Pattern, Replace], End, NewEnd, _Keep_On),
|
||||
append_3(Deb, R, NewEnd, Out),
|
||||
Keep_On = stop
|
||||
; Out = In,
|
||||
( R = Replace
|
||||
-> Keep_On = true
|
||||
; Keep_On = false)).
|
||||
|
||||
|
||||
append_3(A, B, C, D) :-
|
||||
append(A, B, T),
|
||||
append(T, C, D).
|
||||
|
||||
% creation of the rules
|
||||
markov(A) --> line(A).
|
||||
|
||||
line(A) --> text(A), newline.
|
||||
|
||||
|
||||
newline --> ['\n'], newline.
|
||||
newline --> [].
|
||||
|
||||
text([]) --> comment([]).
|
||||
text(A) --> rule(A).
|
||||
|
||||
comment([]) --> ['#'], anything.
|
||||
|
||||
anything --> [X], {X \= '\n'}, anything.
|
||||
anything --> ['\n'].
|
||||
anything --> [].
|
||||
|
||||
rule([A,B]) -->
|
||||
pattern(A), whitespaces, ['-', '>'], whitespaces, end_rule(B).
|
||||
|
||||
pattern([X | R]) --> [X], {X \= '\n'}, pattern(R).
|
||||
pattern([]) --> [].
|
||||
|
||||
whitespaces --> ['\t'], whitespace.
|
||||
whitespaces --> [' '], whitespace.
|
||||
|
||||
whitespace --> whitespaces.
|
||||
whitespace --> [].
|
||||
|
||||
end_rule([.| A]) --> [.], rest_of_rule(A).
|
||||
end_rule(A) --> rest_of_rule(A).
|
||||
end_rule([]) --> [].
|
||||
|
||||
rest_of_rule(A) --> replacement(A).
|
||||
|
||||
replacement([X | R]) --> [X], {X \= '\n'}, replacement(R).
|
||||
replacement([]) --> [].
|
||||
|
|
@ -0,0 +1,117 @@
|
|||
:- use_module('markov.pl').
|
||||
:- use_module(library(lambda)).
|
||||
|
||||
markov :-
|
||||
maplist(\X^(call(X), nl,nl), [markov_1, markov_2, markov_3, markov_4, markov_5]).
|
||||
|
||||
markov_1 :-
|
||||
A = ['# This rules file is extracted from Wikipedia:',
|
||||
'# http://en.wikipedia.org/wiki/Markov_Algorithm',
|
||||
'A -> apple',
|
||||
'B -> bag',
|
||||
'S -> shop',
|
||||
'T -> the',
|
||||
'the shop -> my brother',
|
||||
'a never used -> .terminating rule'],
|
||||
B = 'I bought a B of As from T S.',
|
||||
apply_markov(A, B, R),
|
||||
writeln(B),
|
||||
writeln(R).
|
||||
|
||||
|
||||
markov_2 :-
|
||||
A = ['# Slightly modified from the rules on Wikipedia',
|
||||
'A -> apple',
|
||||
'B -> bag',
|
||||
'S -> .shop',
|
||||
'T -> the',
|
||||
'the shop -> my brother',
|
||||
'a never used -> .terminating rule'],
|
||||
|
||||
B = 'I bought a B of As from T S.',
|
||||
|
||||
apply_markov(A, B, R),
|
||||
writeln(B),
|
||||
writeln(R).
|
||||
|
||||
|
||||
markov_3 :-
|
||||
A = ['# BNF Syntax testing rules',
|
||||
'A -> apple',
|
||||
'WWWW -> with',
|
||||
'Bgage -> ->.*',
|
||||
'B -> bag',
|
||||
'->.* -> money',
|
||||
'W -> WW',
|
||||
'S -> .shop',
|
||||
'T -> the',
|
||||
'the shop -> my brother',
|
||||
'a never used -> .terminating rule'],
|
||||
|
||||
B = 'I bought a B of As W my Bgage from T S.',
|
||||
|
||||
apply_markov(A, B, R),
|
||||
writeln(B),
|
||||
writeln(R).
|
||||
|
||||
|
||||
markov_4 :-
|
||||
A = ['### Unary Multiplication Engine, for testing Markov Algorithm implementations',
|
||||
'### By Donal Fellows.',
|
||||
'# Unary addition engine',
|
||||
'_+1 -> _1+',
|
||||
'1+1 -> 11+',
|
||||
'# Pass for converting from the splitting of multiplication into ordinary',
|
||||
'# addition',
|
||||
'1! -> !1',
|
||||
',! -> !+',
|
||||
'_! -> _',
|
||||
'# Unary multiplication by duplicating left side, right side times',
|
||||
'1*1 -> x,@y',
|
||||
'1x -> xX',
|
||||
'X, -> 1,1',
|
||||
'X1 -> 1X',
|
||||
'_x -> _X',
|
||||
',x -> ,X',
|
||||
'y1 -> 1y',
|
||||
'y_ -> _',
|
||||
'# Next phase of applying',
|
||||
'1@1 -> x,@y',
|
||||
'1@_ -> @_',
|
||||
',@_ -> !_',
|
||||
'++ -> +',
|
||||
'# Termination cleanup for addition',
|
||||
'_1 -> 1',
|
||||
'1+_ -> 1',
|
||||
'_+_ -> '],
|
||||
|
||||
B = '_1111*11111_',
|
||||
|
||||
apply_markov(A, B, R),
|
||||
writeln(B),
|
||||
writeln(R).
|
||||
|
||||
markov_5 :-
|
||||
A = ['# 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'],
|
||||
|
||||
B = '000000A000000',
|
||||
apply_markov(A, B, R),
|
||||
writeln(B),
|
||||
writeln(R).
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
def setup(ruleset)
|
||||
ruleset.each_line.inject([]) do |rules, line|
|
||||
if line =~ /^\s*#/
|
||||
rules
|
||||
elsif line =~ /^(.+)\s+->\s+(\.?)(.*)$/
|
||||
rules << [$1, $3, $2 != ""]
|
||||
else
|
||||
raise "Syntax error: #{line}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def morcov(ruleset, input_data)
|
||||
rules = setup(ruleset)
|
||||
while (matched = rules.find { |match, replace, term|
|
||||
input_data[match] and input_data.sub!(match, replace)
|
||||
}) and !matched[2]
|
||||
end
|
||||
input_data
|
||||
end
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
ruleset1 = <<EOS
|
||||
# This rules file is extracted from Wikipedia:
|
||||
# http://en.wikipedia.org/wiki/Markov_Algorithm
|
||||
A -> apple
|
||||
B -> bag
|
||||
S -> shop
|
||||
T -> the
|
||||
the shop -> my brother
|
||||
a never used -> .terminating rule
|
||||
EOS
|
||||
|
||||
puts morcov(ruleset1, "I bought a B of As from T S.")
|
||||
|
||||
ruleset2 = <<EOS
|
||||
# Slightly modified from the rules on Wikipedia
|
||||
A -> apple
|
||||
B -> bag
|
||||
S -> .shop
|
||||
T -> the
|
||||
the shop -> my brother
|
||||
a never used -> .terminating rule
|
||||
EOS
|
||||
|
||||
puts morcov(ruleset2, "I bought a B of As from T S.")
|
||||
|
||||
ruleset3 = <<EOS
|
||||
# BNF Syntax testing rules
|
||||
A -> apple
|
||||
WWWW -> with
|
||||
Bgage -> ->.*
|
||||
B -> bag
|
||||
->.* -> money
|
||||
W -> WW
|
||||
S -> .shop
|
||||
T -> the
|
||||
the shop -> my brother
|
||||
a never used -> .terminating rule
|
||||
EOS
|
||||
|
||||
puts morcov(ruleset3, "I bought a B of As W my Bgage from T S.")
|
||||
|
||||
ruleset4 = <<EOS
|
||||
### Unary Multiplication Engine, for testing Markov Algorithm implementations
|
||||
### By Donal Fellows.
|
||||
# Unary addition engine
|
||||
_+1 -> _1+
|
||||
1+1 -> 11+
|
||||
# Pass for converting from the splitting of multiplication into ordinary
|
||||
# addition
|
||||
1! -> !1
|
||||
,! -> !+
|
||||
_! -> _
|
||||
# Unary multiplication by duplicating left side, right side times
|
||||
1*1 -> x,@y
|
||||
1x -> xX
|
||||
X, -> 1,1
|
||||
X1 -> 1X
|
||||
_x -> _X
|
||||
,x -> ,X
|
||||
y1 -> 1y
|
||||
y_ -> _
|
||||
# Next phase of applying
|
||||
1@1 -> x,@y
|
||||
1@_ -> @_
|
||||
,@_ -> !_
|
||||
++ -> +
|
||||
# Termination cleanup for addition
|
||||
_1 -> 1
|
||||
1+_ -> 1
|
||||
_+_ ->
|
||||
EOS
|
||||
|
||||
puts morcov(ruleset4, "_1111*11111_")
|
||||
|
||||
ruleset5 = <<EOS
|
||||
# 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
|
||||
EOS
|
||||
|
||||
puts morcov(ruleset5, "000000A000000")
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
raise "Please input an input code file, an input data file, and an output file." if ARGV.size < 3
|
||||
|
||||
rules = File.readlines(ARGV[0]).inject([]) do |rules, line|
|
||||
if line =~ /^\s*#/
|
||||
rules
|
||||
elsif line =~ /^(.+)\s+->\s+(\.?)(.*)$/
|
||||
rules << [$1, $3, $2 != ""]
|
||||
else
|
||||
raise "Syntax error: #{line}"
|
||||
end
|
||||
end
|
||||
|
||||
File.open(ARGV[2], "w") do |file|
|
||||
file.write(File.read(ARGV[1]).tap { |input_data|
|
||||
while (matched = rules.find { |match, replace, term|
|
||||
input_data[match] and input_data.sub!(match, replace)
|
||||
}) and !matched[2]
|
||||
end
|
||||
})
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue