September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,91 +0,0 @@
package main
import (
"fmt"
"strings"
)
type testCase struct {
ruleSet, sample, output string
}
var testSet []testCase // initialized in separate source file
func main() {
fmt.Println("validating", len(testSet), "test cases")
var failures bool
for i, tc := range testSet {
if r := ma(tc.ruleSet, tc.sample); r != tc.output {
fmt.Println("test", i+1, "fail")
failures = true
}
}
if !failures {
fmt.Println("no failures")
}
}
type rule struct {
pat string
rep string
term bool
}
func ma(rs, s string) string {
// compile rules per task description
var rules []rule
for _, line := range strings.Split(rs, "\n") {
if line == "" || line[0] == '#' {
continue
}
a := strings.Index(line, "->")
if a == -1 {
fmt.Println("invalid rule:", line)
return ""
}
pat := line[:a]
for {
if pat == "" {
b := strings.Index(line[a+2:], "->")
if b == -1 {
fmt.Println("invalid rule:", line)
return ""
}
a += 2 + b
pat = line[:a]
continue
}
last := pat[len(pat)-1]
if last != ' ' && last != '\t' {
break
}
pat = pat[:len(pat)-1]
}
rep := line[a+2:]
for rep > "" && (rep[0] == ' ' || rep[0] == '\t') {
rep = rep[1:]
}
var term bool
if rep > "" && rep[0] == '.' {
term = true
rep = rep[1:]
}
rules = append(rules, rule{pat, rep, term})
}
// execute algorithm per WP
for r := 0; r < len(rules); {
pat := rules[r].pat
if f := strings.Index(s, pat); f == -1 {
r++
} else {
s = s[:f] + rules[r].rep + s[f+len(pat):]
if rules[r].term {
break
}
r = 0
}
}
return s
}

View file

@ -1,20 +0,0 @@
package main
func init() {
testSet = []testCase{
{
`# 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`,
"I bought a B of As from T S.",
"I bought a bag of apples from my brother."},
{
`# Slightly modified from the rules on Wikipedia
A -> apple
B -> bag
...