From f2d65340d7ba62f4775d059fb2cfb0d101a50254 Mon Sep 17 00:00:00 2001 From: Abhigyan Kishor Date: Thu, 16 May 2024 19:02:08 -0400 Subject: [PATCH 1/2] added -wiki extension --- src/tt.go | 13 ++++++++--- src/util.go | 15 +++++++++++- src/wikitest.go | 19 +++++++++++++++ src/wk.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ src/wordtest.go | 7 ++++-- 5 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 src/wikitest.go create mode 100644 src/wk.go diff --git a/src/tt.go b/src/tt.go index aba3ada..3213da5 100644 --- a/src/tt.go +++ b/src/tt.go @@ -171,6 +171,8 @@ Modes have the following form: [{"text": "foo", attribution: "bar"}] + -wiki Generate a typing test from a featured article on + wikipedia. Defaults to 10 lines. Word Mode -n GROUPSZ Sets the number of words which constitute a group. @@ -249,6 +251,8 @@ func main() { var wordFile string var quoteFile string + var isWiki bool + var themeName string var showWpm bool var multiMode bool @@ -270,6 +274,8 @@ func main() { flag.StringVar(&wordFile, "words", "", "") flag.StringVar("eFile, "quotes", "", "") + flag.BoolVar(&isWiki, "wiki", false, "") + flag.BoolVar(&showWpm, "showwpm", false, "") flag.BoolVar(&noSkip, "noskip", false, "") flag.BoolVar(&normalCursor, "blockcursor", false, "") @@ -336,8 +342,9 @@ func main() { if err != nil { panic(err) } - testFn = generateTestFromData(b, rawMode, multiMode) + case isWiki: + testFn = generateWikiTest(g) case len(flag.Args()) > 0: path := flag.Args()[0] testFn = generateTestFromFile(path, startParagraph) @@ -383,7 +390,7 @@ func main() { typer.ShowWpm = showWpm if timeout != -1 { - timeout *= 1E9 + timeout *= 1e9 } var tests [][]segment @@ -415,7 +422,7 @@ func main() { idx-- } case TyperComplete: - cpm := int(float64(ncorrect) / (float64(t) / 60E9)) + cpm := int(float64(ncorrect) / (float64(t) / 60e9)) wpm := cpm / 5 accuracy := float64(ncorrect) / float64(nerrs+ncorrect) * 100 diff --git a/src/util.go b/src/util.go index 66f005b..89852be 100644 --- a/src/util.go +++ b/src/util.go @@ -25,7 +25,7 @@ func init() { } type cell struct { - c rune + c rune style tcell.Style } @@ -101,6 +101,19 @@ func randomText(n int, words []string) string { return strings.Replace(r, "\n", " \n", -1) } +func cleanWiki(words []string) string { + r := "" + n := len(words) + for i := 0; i < n; i++ { + r += words[i] + if i != n-1 { + r += " " + } + } + + return strings.Replace(r, "\n", " \n", -1) +} + func stringToCells(s string) []cell { a := make([]cell, len(s)) s = strings.TrimRight(s, "\n ") diff --git a/src/wikitest.go b/src/wikitest.go new file mode 100644 index 0000000..8b519a2 --- /dev/null +++ b/src/wikitest.go @@ -0,0 +1,19 @@ +package main + +import ( + "regexp" +) + +func generateWikiTest(g int) func() []segment { + + words := regexp.MustCompile("\\s+").Split(extractText(), -1) + + return func() []segment { + segments := make([]segment, g) + for i := 0; i < g; i++ { + segments[i] = segment{cleanWiki(words), ""} + } + return segments + } + +} diff --git a/src/wk.go b/src/wk.go new file mode 100644 index 0000000..99a750d --- /dev/null +++ b/src/wk.go @@ -0,0 +1,62 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "strings" +) + +func extractText() string { + url := "https://randomincategory.toolforge.org/Featured_articles?site=en.wikipedia.org" + resp, err := http.Get(url) + + if err != nil { + log.Fatal(err) + } + + defer resp.Body.Close() + + pageTitle := strings.TrimPrefix((*resp.Request.URL).String(), "https://en.wikipedia.org/wiki/") + + req, err := http.NewRequest("GET", "https://en.wikipedia.org/w/api.php", nil) + + if err != nil { + log.Fatal(err) + } + q := req.URL.Query() + q.Add("action", "query") + q.Add("format", "json") + q.Add("titles", pageTitle) + q.Add("prop", "extracts") + q.Add("exsentences", "10") + q.Add("explaintext", "true") + + req.URL.RawQuery = q.Encode() + + resp, err = http.DefaultClient.Do(req) + if err != nil { + log.Fatal(err) + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + + // var result Response + var result map[string]interface{} + + if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to go struct pointer + fmt.Println("Can not unmarshal JSON") + } + // fmt.Println(result["query"].(map[string]interface{})["pages"]) + pageJson := result["query"].(map[string]interface{})["pages"].(map[string]interface{}) + + var text string + for key := range pageJson { + text = pageJson[key].(map[string]interface{})["extract"].(string) + + } + return text +} diff --git a/src/wordtest.go b/src/wordtest.go index 9e177f0..18e2dbe 100644 --- a/src/wordtest.go +++ b/src/wordtest.go @@ -1,6 +1,9 @@ package main -import "regexp" +import ( + "regexp" + "fmt" +) func generateWordTest(name string, n int, g int) func() []segment { var b []byte @@ -16,7 +19,7 @@ func generateWordTest(name string, n int, g int) func() []segment { for i := 0; i < g; i++ { segments[i] = segment{randomText(n, words), ""} } - + fmt.Println(segments) return segments } } From 481dbffd89bfcfb04a8f33cf00bdfb71415dd371 Mon Sep 17 00:00:00 2001 From: Abhigyan Kishor Date: Thu, 16 May 2024 19:08:12 -0400 Subject: [PATCH 2/2] updated README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 439ca6a..236ae31 100644 --- a/README.md +++ b/README.md @@ -83,3 +83,8 @@ Custom themes and word lists can be defined in `~/.tt/themes` and `~/.tt/words` and used in conjunction with the `-theme` and `-words` flags. A list of preloaded themes and word lists can be found in `words/` and `themes/` and are accessible by default using the respective flags. + + +## Wikipedia tests +`tt -wiki` generates a typing test from the first 10 sentences of a random featured article from wikipedia. Currently some characters (such as hyphens) don't always type correctly :) +