-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage.go
117 lines (109 loc) · 3.01 KB
/
usage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"database/sql"
"fmt"
"strings"
)
// Usage is struct that is able to extract usage examples from the tatoeba
// datasets.
type UsageFetcher struct {
db *sql.DB
}
type sentence struct {
text string
lang string
}
// NewUsageFetcher creates a new usage fetcher.
func NewUsageFetcher(dbPath string) (*UsageFetcher, error) {
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return nil, err
}
// Schema for the db can be found in migrate/load.go
return &UsageFetcher{
db: db,
}, nil
}
type UsageExample struct {
Text string
Translations []string
}
// FIXME: Too many parameters
// language is a langugage of the word in ISO 639-3 format.
func (u *UsageFetcher) FetchExamples(word, language string, translationLanguages map[string]bool) ([]*UsageExample, error) {
var tls []interface{}
for k, v := range translationLanguages {
if v {
tls = append(tls, k)
}
}
// We use Sprintf only to insert variable number of ?, so it cannot cause
// SQL injection.
q := fmt.Sprintf(`
SELECT DISTINCT s.text, ts.text
FROM
Words
INNER JOIN
Sentences s ON Words.sentence_id = s.id
LEFT JOIN
Translations ON Words.sentence_id = Translations.id
LEFT JOIN
Sentences ts ON Translations.translation_id = ts.id
WHERE
Words.word = ?
AND s.lang = ?
AND (ts.lang IS NULL OR ts.lang IN (?%s))
-- If possible get definitions with translations first.
ORDER BY CASE WHEN ts.text IS NULL THEN 1 ELSE 0 END
LIMIT 3;`, strings.Repeat(", ?", len(tls)-1))
args := append([]interface{}{
word, language,
}, tls...)
rows, err := u.db.Query(q, args...)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("no sentences match %s", word)
}
if err != nil {
return nil, err
}
defer rows.Close()
var ex []*UsageExample
for rows.Next() {
var (
e string
t sql.NullString
)
if err := rows.Scan(&e, &t); err != nil {
return nil, err
}
var tr []string
if t.Valid {
tr = append(tr, t.String)
}
ex = append(ex, &UsageExample{
Text: e,
Translations: tr,
})
}
// TODO: rank examples by complexity and extract the simplest ones:
// 1) for each word calculate it's complexity by the number of sentences it's
// used in (more sentences -> simpler words)
// 2) the sentence is simpler if it contains simpler words. Maybe average
// word simplicity to not disqualify long sentences.
//
// TODO: Prioritize using sentences with the most translations.
return ex, nil
}