chore: added babbler
6552e8fd
5 file(s) · +76 −7
| 1 | + | gargantua |
|
| 2 | + | moby.txt |
| 1 | + | package main |
|
| 2 | + | ||
| 3 | + | import ( |
|
| 4 | + | "log" |
|
| 5 | + | "math/rand" |
|
| 6 | + | "os" |
|
| 7 | + | "strings" |
|
| 8 | + | ) |
|
| 9 | + | ||
| 10 | + | const sourceText = "moby.txt" |
|
| 11 | + | ||
| 12 | + | type MarkovChain struct { |
|
| 13 | + | chain map[string][]string |
|
| 14 | + | } |
|
| 15 | + | ||
| 16 | + | func NewBabbler() *MarkovChain { |
|
| 17 | + | text, err := os.ReadFile(sourceText) |
|
| 18 | + | if err != nil { |
|
| 19 | + | log.Fatal("Could not read source text for babbler", err) |
|
| 20 | + | } |
|
| 21 | + | words := strings.Fields(string(text)) |
|
| 22 | + | chain := make(map[string][]string) |
|
| 23 | + | for i := 0; i < len(words)-1; i++ { |
|
| 24 | + | chain[words[i]] = append(chain[words[i]], words[i+1]) |
|
| 25 | + | } |
|
| 26 | + | return &MarkovChain{chain: chain} |
|
| 27 | + | } |
|
| 28 | + | ||
| 29 | + | func (m *MarkovChain) Generate(maxWords int) string { |
|
| 30 | + | if len(m.chain) == 0 { |
|
| 31 | + | return "" |
|
| 32 | + | } |
|
| 33 | + | ||
| 34 | + | keys := make([]string, 0, len(m.chain)) |
|
| 35 | + | for k := range m.chain { |
|
| 36 | + | keys = append(keys, k) |
|
| 37 | + | } |
|
| 38 | + | word := keys[rand.Intn(len(keys))] |
|
| 39 | + | if _, ok := m.chain[word]; !ok { |
|
| 40 | + | keys := make([]string, 0, len(m.chain)) |
|
| 41 | + | for k := range m.chain { |
|
| 42 | + | keys = append(keys, k) |
|
| 43 | + | } |
|
| 44 | + | word = keys[rand.Intn(len(keys))] |
|
| 45 | + | } |
|
| 46 | + | ||
| 47 | + | var result []string |
|
| 48 | + | result = append(result, word) |
|
| 49 | + | ||
| 50 | + | for i := 1; i < maxWords; i++ { |
|
| 51 | + | next, ok := m.chain[word] |
|
| 52 | + | if !ok || len(next) == 0 { |
|
| 53 | + | break |
|
| 54 | + | } |
|
| 55 | + | word = next[rand.Intn(len(next))] |
|
| 56 | + | result = append(result, word) |
|
| 57 | + | } |
|
| 58 | + | ||
| 59 | + | return strings.Join(result, " ") |
|
| 60 | + | } |
Binary file — no preview.
| 3 | 3 | import ( |
|
| 4 | 4 | "embed" |
|
| 5 | 5 | "fmt" |
|
| 6 | - | "log" |
|
| 7 | 6 | "html/template" |
|
| 7 | + | "log" |
|
| 8 | 8 | "net/http" |
|
| 9 | 9 | "os" |
|
| 10 | 10 | ) |
|
| 25 | 25 | var robotsTxt string |
|
| 26 | 26 | ||
| 27 | 27 | type PageData struct { |
|
| 28 | - | Title string |
|
| 29 | - | Items []string |
|
| 28 | + | Title string |
|
| 29 | + | Content string |
|
| 30 | + | Items []string |
|
| 30 | 31 | } |
|
| 31 | 32 | ||
| 32 | - | func robotsHandler(writer http.ResponseWriter, req *http.Request){ |
|
| 33 | + | func robotsHandler(writer http.ResponseWriter, req *http.Request) { |
|
| 33 | 34 | writer.Header().Set("Content-Type", "text/plain; charset=utf-8") |
|
| 34 | 35 | fmt.Fprint(writer, robotsTxt) |
|
| 35 | 36 | } |
|
| 36 | 37 | ||
| 37 | 38 | func indexHandler(writer http.ResponseWriter, req *http.Request) { |
|
| 39 | + | babbler := NewBabbler() |
|
| 40 | + | title := babbler.Generate(2) |
|
| 41 | + | content := babbler.Generate(500) |
|
| 38 | 42 | data := PageData{ |
|
| 39 | - | Title: "Babbler", |
|
| 40 | - | Items: []string{"First link", "second link", "third link"}, |
|
| 43 | + | Title: title, |
|
| 44 | + | Content: content, |
|
| 45 | + | Items: []string{"First link", "second link", "third link"}, |
|
| 41 | 46 | } |
|
| 42 | 47 | if err := tmpl.ExecuteTemplate(writer, "index.html", data); err != nil { |
|
| 43 | 48 | http.Error(writer, "Internal server error", http.StatusInternalServerError) |
|
| 46 | 51 | ||
| 47 | 52 | func requestMiddleware(handler http.Handler) http.Handler { |
|
| 48 | 53 | return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) { |
|
| 49 | - | log.Printf("%s %s %s\n", req.Method, req.URL, req.RemoteAddr) |
|
| 54 | + | log.Printf("%s %s %s\n", req.Method, req.URL, req.UserAgent()) |
|
| 50 | 55 | writer.Header().Set("X-Robots-Tag", "noindex, nofollow") |
|
| 51 | 56 | handler.ServeHTTP(writer, req) |
|
| 52 | 57 | }) |
|
| 6 | 6 | <title>{{.Title}}</title> |
|
| 7 | 7 | </head> |
|
| 8 | 8 | <body> |
|
| 9 | + | <h1>{{.Title}}</title> |
|
| 10 | + | <p>{{.Content}}</p> |
|
| 9 | 11 | {{range .Items}}<div>{{ . }}</div>{{else}}<div><strong>no rows</strong></div>{{end}} |
|
| 10 | 12 | </body> |
|
| 11 | 13 | </html> |