|
1
|
package main |
|
2
|
|
|
3
|
import ( |
|
4
|
"fmt" |
|
5
|
"io" |
|
6
|
"regexp" |
|
7
|
"strings" |
|
8
|
) |
|
9
|
|
|
10
|
func toTSV(entry *Entry) string { |
|
11
|
if len(entry.POSBlocks) == 0 || len(entry.POSBlocks[0].Senses) == 0 { |
|
12
|
return "" |
|
13
|
} |
|
14
|
block := entry.POSBlocks[0] |
|
15
|
sense := block.Senses[0] |
|
16
|
|
|
17
|
col1 := "" |
|
18
|
if len(sense.Examples) > 0 { |
|
19
|
col1 = boldWord(sense.Examples[0], entry.Word, entry.RedirectWord) |
|
20
|
} else { |
|
21
|
col1 = fmt.Sprintf("<b>%s</b>", entry.Word) |
|
22
|
} |
|
23
|
col2 := sense.Definition |
|
24
|
col3 := block.IPA |
|
25
|
if col3 != "" && !strings.HasPrefix(col3, "/") { |
|
26
|
col3 = "/" + col3 + "/" |
|
27
|
} |
|
28
|
return fmt.Sprintf("%s\t%s\t%s", col1, col2, col3) |
|
29
|
} |
|
30
|
|
|
31
|
func boldWord(s, word string, extra ...string) string { |
|
32
|
words := append([]string{word}, extra...) |
|
33
|
seen := map[string]bool{} |
|
34
|
for _, w := range words { |
|
35
|
if w == "" || seen[w] { |
|
36
|
continue |
|
37
|
} |
|
38
|
seen[w] = true |
|
39
|
stem := w |
|
40
|
for _, suffix := range []string{"ing", "ed", "es", "d", "s"} { |
|
41
|
if len(w) > len(suffix) && strings.HasSuffix(w, suffix) { |
|
42
|
stem = strings.TrimSuffix(w, suffix) |
|
43
|
break |
|
44
|
} |
|
45
|
} |
|
46
|
if len(stem) < 3 { |
|
47
|
stem = w |
|
48
|
} |
|
49
|
re := regexp.MustCompile(`(?i)\b` + regexp.QuoteMeta(stem) + `\w*\b`) |
|
50
|
if result := re.ReplaceAllString(s, `<b>$0</b>`); result != s { |
|
51
|
return result |
|
52
|
} |
|
53
|
} |
|
54
|
return s |
|
55
|
} |
|
56
|
|
|
57
|
func writeAll(entry *Entry, w io.Writer) { |
|
58
|
for pi, block := range entry.POSBlocks { |
|
59
|
if pi > 0 { |
|
60
|
_, _ = fmt.Fprintln(w) |
|
61
|
} |
|
62
|
_, _ = fmt.Fprintf(w, "[%s] /%s/\n", block.POS, block.IPA) |
|
63
|
for si, sense := range block.Senses { |
|
64
|
if si > 0 { |
|
65
|
_, _ = fmt.Fprintln(w) |
|
66
|
} |
|
67
|
_, _ = fmt.Fprintf(w, " %d. %s\n", si+1, sense.Definition) |
|
68
|
for _, ex := range sense.Examples { |
|
69
|
_, _ = fmt.Fprintf(w, " - %s\n", ex) |
|
70
|
} |
|
71
|
} |
|
72
|
} |
|
73
|
} |