2022-01-21 01:00:18 +05:30
---
id: 59e09e6d412c5939baa02d16
2022-01-22 20:38:20 +05:30
title: マルコフアルゴリズム
2022-01-21 01:00:18 +05:30
challengeType: 5
forumTopicId: 302260
dashedName: execute-a-markov-algorithm
---
# --description--
2022-01-22 20:38:20 +05:30
[マルコフアルゴリズム ](https://en.wikipedia.org/wiki/Markov algorithm "wp: Markov algorithm" ) のインタプリタを作成します。
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
ルールの構文は以下のとおりです。
2022-01-21 01:00:18 +05:30
< pre > [ruleset] ::= (([comment] | [rule]) [newline]+)*
[comment] ::= # {[any character]}
[rule] ::= [pattern] [whitespace] -> [whitespace] [.] [replacement]
[whitespace] ::= ([tab] | [space]) [[whitespace]]
< / pre >
2022-01-22 20:38:20 +05:30
1行に1つのルールがあります。
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
\[replacement] の前に`.` (ピリオド) がある場合、インタプリタが演算を停止する規則があります。
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
ルールセットは、任意のコメントを含む一連のルールで構成されます。
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
ルールセット
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
以下のテストを入力に使用します。
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
**ルールセット1:**
2022-01-21 01:00:18 +05:30
< pre > # This rules file is extracted from Wikipedia:
# <code>http://en.wikipedia.org/wiki/Markov_Algorithm</code>
A -> apple
B -> bag
S -> shop
T -> the
the shop -> my brother
a never used -> .terminating rule
< / pre >
2022-01-22 20:38:20 +05:30
`I bought a B of As from T S.` というサンプルテキストで、 `I bought a bag of apples from my brother.` が出力されます。
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
**ルールセット2:**
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
停止規則のテスト
2022-01-21 01:00:18 +05:30
< pre > # Slightly modified from the rules on Wikipedia
A -> apple
B -> bag
S -> .shop
T -> the
the shop -> my brother
a never used -> .terminating rule
< / pre >
2022-01-22 20:38:20 +05:30
`I bought a B of As from T S.` というサンプルテキストで、 `I bought a bag of apples from T shop.` が出力されます。
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
**ルールセット3:**
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
これは正しい置換順序をテストします。特別な正規表現文字がエスケープされない場合、単純な正規表現ベースの置換ルーチンをトラップする場合があります。
2022-01-21 01:00:18 +05:30
< pre > # 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
< / pre >
2022-01-22 20:38:20 +05:30
`I bought a B of As W my Bgage from T S.` というサンプルテキストで、 `I bought a bag of apples with my money from T shop.` が出力されます。
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
**ルールセット4:**
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
これはルールスキャンの正しい順序をテストし、誤った順序でスキャンする置換ルーチンをトラップする場合があります。 一般的な単項乗算エンジンを実装します。 (この実装では、入力式をアンダースコア内に置くことに注意してください。)
2022-01-21 01:00:18 +05:30
< pre > ### 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
_+_ ->
< / pre >
2022-01-22 20:38:20 +05:30
`_1111*11111_` というサンプルテキストで、`11111111111111111111` が出力されます。
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
**ルールセット5:**
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
シンプルな [チューリングマシン ](http://en.wikipedia.org/wiki/Turing_machine "link: http://en.wikipedia.org/wiki/Turing_machine" )は、3-状態 [ビジービーバー ](http://en.wikipedia.org/wiki/Busy_beaver "link: http://en.wikipedia.org/wiki/Busy_beaver" )を実装します。
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
このテープは `0` と`1` からなり、状態は `A` 、`B` 、`C` および`H` (`H` altの場合) であり、先頭の位置は先頭文字の前に状態文字を書くことにより示されます。 マシンが操作する元のテープをすべて入力する必要あります。
2022-01-21 01:00:18 +05:30
2022-01-22 20:38:20 +05:30
マルコフアルゴリズムがチューリング完全であることを示すだけでなく、 最初の4つのルールセットでは見つからなかったC++実装のバグを検出することができました。
2022-01-21 01:00:18 +05:30
< pre > # 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
< / pre >
2022-01-22 20:38:20 +05:30
このルールセットで `000000A000000` が `00011H1111000` に変更されます。
2022-01-21 01:00:18 +05:30
# --hints--
2022-01-22 20:38:20 +05:30
`markov` という関数です。
2022-01-21 01:00:18 +05:30
```js
assert(typeof markov === 'function');
```
2022-01-22 20:38:20 +05:30
`markov(["A -> apple","B -> bag","S -> shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],"I bought a B of As from T S.")` は「I bought a bag of apples from my brother.」を返します。
2022-01-21 01:00:18 +05:30
```js
assert.deepEqual(markov(rules[0], tests[0]), outputs[0]);
```
2022-01-22 20:38:20 +05:30
`markov(["A -> apple","B -> bag","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],"I bought a B of As from T S.")` は「I bought a bag of apples from T shop.」を返します。
2022-01-21 01:00:18 +05:30
```js
assert.deepEqual(markov(rules[1], tests[1]), outputs[1]);
```
2022-01-22 20:38:20 +05:30
`markov(["A -> apple","WWWW -> with","Bgage -> ->.*","B -> bag","->.* -> money","W -> WW","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],"I bought a B of As W my Bgage from T S.")` は「I bought a bag of apples with my money from T shop.」を返します。
2022-01-21 01:00:18 +05:30
```js
assert.deepEqual(markov(rules[2], tests[2]), outputs[2]);
```
2022-01-31 22:43:48 +05:30
`markov(["_+1 -> _1+","1+1 -> 11+","1! -> !1",",! -> !+","_! -> _","1*1 -> x,@y","1x -> xX","X, -> 1,1","X1 -> 1X","_x -> _X",",x -> ,X","y1 -> 1y","y_ -> _","1@1 -> x,@y","1@_ -> @_",",@_ -> !_","++ -> +","_1 -> 1","1+_ -> 1","_+_ -> "],"_1111*11111_")` は「11111111111111111111」を返します。
2022-01-21 01:00:18 +05:30
```js
assert.deepEqual(markov(rules[3], tests[3]), outputs[3]);
```
2022-01-22 20:38:20 +05:30
`markov(["A0 -> 1B","0A1 -> C01","1A1 -> C11","0B0 -> A01","1B0 -> A11","B1 -> 1B","0C0 -> B01","1C0 -> B11","0C1 -> H01","1C1 -> H11"],"")` は「00011H1111000」を返します。
2022-01-21 01:00:18 +05:30
```js
assert.deepEqual(markov(rules[4], tests[4]), outputs[4]);
```
# --seed--
## --after-user-code--
```js
let rules=[["A -> apple","B -> bag","S -> shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
["A -> apple","B -> bag","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
["A -> apple","WWWW -> with","Bgage -> ->.*","B -> bag","->.* -> money","W -> WW","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
["_+1 -> _1+","1+1 -> 11+","1! -> !1",",! -> !+","_ ! -> _","1*1 -> x,@y ","1x -> xX","X, -> 1,1","X1 -> 1X","_x -> _X",",x -> ,X","y1 -> 1y","y_ -> _","1@1 -> x,@y","1@_ -> @_ ",",@_ -> !_","++ -> +","_1 -> 1","1+_ -> 1","_+_ -> "],
["A0 -> 1B","0A1 -> C01","1A1 -> C11","0B0 -> A01","1B0 -> A11","B1 -> 1B","0C0 -> B01","1C0 -> B11","0C1 -> H01","1C1 -> H11"]];
let tests=["I bought a B of As from T S.",
"I bought a B of As from T S.",
"I bought a B of As W my Bgage from T S.",
"_1111*11111_",
"000000A000000"];
let outputs=["I bought a bag of apples from my brother.",
"I bought a bag of apples from T shop.",
"I bought a bag of apples with my money from T shop.",
"11111111111111111111",
"00011H1111000"]
```
## --seed-contents--
```js
function markov(rules,test) {
}
```
# --solutions--
```js
function markov(rules,test) {
let pattern = new RegExp("^([^#]*?)\\s+->\\s+(\\.?)(.*)");
let origTest = test;
let captures = [];
rules.forEach(function(rule){
let m = pattern.exec(rule);
for (let j = 0; j < m.length ; j + + )
m[j] = m[j + 1];
captures.push(m);
});
test = origTest;
let copy = test;
for (let j = 0; j < captures.length ; j + + ) {
let c = captures[j];
test = test.replace(c[0], c[2]);
if (c[1]==".")
break;
if (test!=copy) {
j = -1;
copy = test;
}
}
return test;
}
```