-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnapow.go
116 lines (80 loc) · 1.88 KB
/
dnapow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package repo
import (
"encoding/json"
"fmt"
"github.com/BluntSporks/readability"
"github.com/mb-14/gomarkov"
"sync"
"io/ioutil"
"strings"
)
//not in use yet SMOG text analysis ?
func calculateDifficultyOfTarget(target string) (score float64) {
score = read.Smog(target)
return
}
type QuoteTrainingSet []Quote
type Quote struct {
quoteText string
quoteAuthor string
}
func GetTrainingSet() map[int]string {
data, err := ioutil.ReadFile("./repo/quotes.json")
if err != nil {
panic(err)
}
mapper := make(map[int]string, len(data))
splitted := strings.Split(string(data), "\n")
for i := 0; i < len(splitted); i++ {
mapper[i] = string(splitted[i])
}
return mapper
}
//training the markov chain to be able to generate sentences for pow
func TrainForPow() string {
chain, _ := buildModel()
saveModel(chain)
chain, _ = loadModel()
return generatePow(chain)
}
func buildModel() (*gomarkov.Chain, error) {
quotes := GetTrainingSet()
chain := gomarkov.NewChain(1)
var wg sync.WaitGroup
wg.Add(len(quotes))
for _, quote := range quotes {
go func() {
defer wg.Done()
chain.Add(strings.Split(quote, " "))
}()
}
wg.Wait()
return chain, nil
}
func saveModel(chain *gomarkov.Chain) {
jsonObj, _ := json.Marshal(chain)
err := ioutil.WriteFile("model.json", jsonObj, 0644)
if err != nil {
fmt.Println(err)
}
}
func loadModel() (*gomarkov.Chain, error) {
var chain gomarkov.Chain
data, err := ioutil.ReadFile("model.json")
if err != nil {
return &chain, err
}
err = json.Unmarshal(data, &chain)
if err != nil {
return &chain, err
}
return &chain, nil
}
func generatePow(chain *gomarkov.Chain) string {
tokens := []string{gomarkov.StartToken}
for tokens[len(tokens)-1] != gomarkov.EndToken {
next, _ := chain.Generate(tokens[(len(tokens) - 1):])
tokens = append(tokens, next)
}
return fmt.Sprintf(strings.Join(tokens[1:len(tokens)-1], " "))
}