Commit ea3088ea authored by Milan Bartky's avatar Milan Bartky

Added golang version of steve

parent 854d3389
# Default ignored files
/workspace.xml
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/golang.iml" filepath="$PROJECT_DIR$/.idea/golang.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
\ No newline at end of file
This diff is collapsed.
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func main() {
fmt.Println("Reading File...")
file, err := os.Open("exampleData.txt")
if err != nil {
panic(err)
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
panic(err)
}
fmt.Println("Normalizing text...")
text := normalizeBefore(string(b))
fmt.Println("Generating Table...")
markov := MarkovGenerator{}
markov.generateTable(text, 6)
fmt.Println("Generating Text...")
fmt.Println(markov.generateText(500))
}
func normalizeBefore(text string) string {
spaceAfter := []string{".", ",", "?", "!", ":", ";"}
spaceBefore := []string{">"}
for _, el := range spaceAfter {
text = strings.ReplaceAll(text, el, el + " ")
}
for _, el := range spaceBefore {
text = strings.ReplaceAll(text, el, " " + el)
}
return text
}
\ No newline at end of file
package main
import (
"math"
"math/rand"
"regexp"
"strings"
"time"
)
type MarkovGenerator struct {
Table map[string]map[string]int
Words []string
LookForward int
}
func (m *MarkovGenerator) generateTable(text string, lookForward int) {
m.Table = make(map[string]map[string]int, 0)
var re = regexp.MustCompile(`(?m)\s+`)
m.Words = _regSplit(text, re)
m.LookForward = lookForward
for i:=0;i<len(m.Words)-lookForward;i++ {
char := strings.Join(m.Words[i:i+lookForward], " ")
if _, ok := m.Table[char]; ok {
continue
}
m.Table[char] = make(map[string]int, 0)
}
for i:=0;i<len(m.Words)-(lookForward*2);i++ {
charIndex := strings.Join(m.Words[i:i+lookForward], " ")
charCount := strings.Join(m.Words[i+lookForward:i+(lookForward*2)], " ")
if _, ok := m.Table[charIndex][charCount]; ok {
m.Table[charIndex][charCount]++
} else {
m.Table[charIndex][charCount] = 1
}
}
loopLast := strings.Join(m.Words[len(m.Words)-(lookForward+1):], " ")
loopFirst := strings.Join(m.Words[0:lookForward], " ")
if _, ok := m.Table[loopLast]; ok {} else {
m.Table[loopLast] = make(map[string]int, 0)
}
m.Table[loopLast][loopFirst] = 1
}
func (m *MarkovGenerator) generateText(length int) string {
rand.Seed(time.Now().UTC().UnixNano())
randomStart := rand.Intn(len(m.Words) - m.LookForward)
char := strings.Join(m.Words[randomStart:randomStart+m.LookForward], " ")
o := []string{char}
max := math.Floor(float64(length / m.LookForward))
for i:=0.0;i<max;i++ {
newChar := m._weightedChar(char)
if newChar != "" {
char = newChar
o = append(o, newChar)
} else {
randomStart := rand.Intn(len(m.Words) - m.LookForward)
char = strings.Join(m.Words[randomStart:randomStart+m.LookForward], " ")
}
}
return normalizeAfter(strings.Join(o, " "))
}
func (m *MarkovGenerator) _weightedChar(index string) string {
if array, ok := m.Table[index]; ok {
total := 0
for _, v := range array {
total += v
}
random := 1
if total > 1 {
random = rand.Intn(total-1) + 1
}
for item, weight := range array {
if random <= weight {
return item
}
}
return ""
} else {
return ""
}
}
func _regSplit(text string, reg *regexp.Regexp) []string {
indexes := reg.FindAllStringIndex(text, -1)
laststart := 0
result := make([]string, len(indexes) + 1)
for i, element := range indexes {
result[i] = text[laststart:element[0]]
laststart = element[1]
}
result[len(indexes)] = text[laststart:]
return result
}
func normalizeAfter(text string) string {
spaceAfter := []string{".", "?", "!"}
spaceBefore := []string{">"}
for _, el := range spaceAfter {
text = strings.ReplaceAll(text, el, el + "\n")
}
for _, el := range spaceBefore {
text = strings.ReplaceAll(text, el, "\n" + el)
}
text = strings.ReplaceAll(text, "\n ", "\n")
var re = regexp.MustCompile(`(?mU)\n{2,}`)
return re.ReplaceAllString(text, "\n")
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment