Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :)

13 changes: 10 additions & 3 deletions src/tt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -249,6 +251,8 @@ func main() {
var wordFile string
var quoteFile string

var isWiki bool

var themeName string
var showWpm bool
var multiMode bool
Expand All @@ -270,6 +274,8 @@ func main() {
flag.StringVar(&wordFile, "words", "", "")
flag.StringVar(&quoteFile, "quotes", "", "")

flag.BoolVar(&isWiki, "wiki", false, "")

flag.BoolVar(&showWpm, "showwpm", false, "")
flag.BoolVar(&noSkip, "noskip", false, "")
flag.BoolVar(&normalCursor, "blockcursor", false, "")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -383,7 +390,7 @@ func main() {
typer.ShowWpm = showWpm

if timeout != -1 {
timeout *= 1E9
timeout *= 1e9
}

var tests [][]segment
Expand Down Expand Up @@ -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

Expand Down
15 changes: 14 additions & 1 deletion src/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func init() {
}

type cell struct {
c rune
c rune
style tcell.Style
}

Expand Down Expand Up @@ -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 ")
Expand Down
19 changes: 19 additions & 0 deletions src/wikitest.go
Original file line number Diff line number Diff line change
@@ -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
}

}
62 changes: 62 additions & 0 deletions src/wk.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 5 additions & 2 deletions src/wordtest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "regexp"
import (
"regexp"
"fmt"
)

func generateWordTest(name string, n int, g int) func() []segment {
var b []byte
Expand All @@ -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
}
}