Skip to content

Commit f8c2cf3

Browse files
author
Talha Altinel
committed
test CleanedText
1 parent 7060d4c commit f8c2cf3

3 files changed

Lines changed: 103 additions & 6 deletions

File tree

anki/record.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,32 @@ func (r Record) RandomizedRow() []string {
6666
}
6767
}
6868

69+
const (
70+
startCloze = "{{c1::"
71+
endCloze = "}}"
72+
)
73+
6974
// CleanedText returns the cleaned text without "{{c1::}}"
7075
func (r Record) CleanedText() string {
71-
start := strings.Index(r.Text, "{{c1::")
76+
start := strings.Index(r.Text, startCloze)
7277
if start == -1 {
7378
return ""
7479
}
7580

76-
end := strings.Index(r.Text[start:], "}}")
81+
end := strings.Index(r.Text[start:], endCloze)
7782
if end == -1 {
7883
return ""
7984
}
8085

8186
// set to absolute end
8287
end = start + end
83-
// length of "{{c1::" is 6
84-
answer := r.Text[start+6 : end]
85-
// length of "}}" is 2
86-
cleanedText := r.Text[:start] + answer + r.Text[end+2:]
88+
answer := r.Text[start+len(startCloze) : end]
8789

90+
cleanedText := r.Text[:start] + answer + r.Text[end+len(endCloze):]
91+
cleanedText = strings.ReplaceAll(cleanedText, " ", " ")
92+
if strings.Contains(cleanedText, startCloze) || strings.Contains(cleanedText, endCloze) {
93+
return ""
94+
}
8895
return cleanedText
8996
}
9097

anki/record_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package anki
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
)
8+
9+
func TestRecord_CleanedText(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
text string
13+
want string
14+
}{
15+
{
16+
name: "cloze deletion at the end",
17+
text: "The capital of France is {{c1::Paris}}.",
18+
want: "The capital of France is Paris.",
19+
},
20+
{
21+
name: "cloze deletion at beginning",
22+
text: "{{c1::Hello}} world!",
23+
want: "Hello world!",
24+
},
25+
{
26+
name: "empty cloze deletion",
27+
text: "This is {{c1::}} empty.",
28+
want: "This is empty.",
29+
},
30+
{
31+
name: "cloze deletion with spaces",
32+
text: "The {{c1::quick brown fox}} jumps.",
33+
want: "The quick brown fox jumps.",
34+
},
35+
{
36+
name: "no cloze deletion marker",
37+
text: "This is plain text.",
38+
},
39+
{
40+
name: "malformed - no opening marker",
41+
text: "This is Paris}}.",
42+
},
43+
{
44+
name: "malformed - no closing marker",
45+
text: "This is {{c1::Paris.",
46+
},
47+
{
48+
name: "empty text",
49+
},
50+
{
51+
name: "only markers",
52+
text: "{{c1::}}",
53+
},
54+
{
55+
name: "multiple closing markers - takes first",
56+
text: "The {{c1::answer}} is }} here.",
57+
},
58+
{
59+
name: "nested braces in content",
60+
text: "Code: {{c1::func() { return }}}",
61+
want: "Code: func() { return }",
62+
},
63+
}
64+
65+
for _, tt := range tests {
66+
t.Run(tt.name, func(t *testing.T) {
67+
r := Record{Text: tt.text}
68+
got := r.CleanedText()
69+
if diff := cmp.Diff(tt.want, got); diff != "" {
70+
t.Errorf("CleanedText() mismatch (-want +got):\n%s", diff)
71+
}
72+
})
73+
}
74+
}

testdata/anki-example2.csv

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Text,HelperText,TextA,TextB,TextC,TextD
2+
Bạn có thể {{c1::nói}} tiếng Anh không?,Can you speak English?,nói,hiểu,viết,đọc
3+
Cuối tuần này bạn muốn đi {{c1::chơi}} đâu?,Where do you want to go out this weekend?,đi,chơi,xem,ăn
4+
Món ăn yêu thích của bạn là {{c1::gì}}?,What is your favorite food?,,đâu,,ai
5+
Bạn có thường xuyên {{c1::tập}} thể dục không?,Do you exercise often?,tập,làm,chơi,học
6+
Mấy giờ bạn {{c1::đi}} ngủ?,What time do you go to sleep?,đi,về,thức,dậy
7+
Hôm nay thời tiết {{c1::thế}} nào?,How is the weather today?,,thế,ai,đâu
8+
"Bạn có anh,chị,em {{c1::ruột}} không?",Do you have any biological siblings?,ruột,nuôi,bạn,thân
9+
Bạn làm nghề {{c1::gì}}?,What do you do for a living?,,ai,đâu,khi nào
10+
Bạn thích {{c1::đọc}} sách không?,Do you like reading books?,nghe,xem,đọc,viết
11+
Bạn {{c1::thích}} món ăn nào nhất?,Which dish do you like the most?,thích,ghét,ăn,uống
12+
Chaò anh thích môn thể thao {{c1::nào}} nhất?,Hello sir which sport do you like the most?,nào,,ai,đâu
13+
Chaò chị thấy món ăn này {{c1::thế}} nào?,Hello ma'am how do you find this dish?,,thế,,ai
14+
Món ăn này ngon {{c1::tuyệt}} vời!,This dish is absolutely wonderful!,rất,tuyệt,hơn,kém
15+
{{c1::Chaò}} chị có thể giúp tôi một chút không?,Hello ma'am can you help me a little?,Tạm biệt,Chaò,Xin lỗi,Cảm ơn
16+
Buổi tiệc {{c1::hôm}} nay thật tuyệt vời!,Today's party is truly wonderful!,ngày,hôm,tối,mai

0 commit comments

Comments
 (0)