Implement exact results

main
Elnu 2 years ago
parent b99ebc787d
commit fb045b9276

@ -69,26 +69,37 @@ func ParseEntry(entry jmdict.JmdictEntry) Entry {
} }
} }
func Search(query string) []Entry { func Search(query string) (exactResults []Entry, otherResults []Entry) {
query = strings.TrimSpace(query) query = strings.TrimSpace(query)
entries := make([]Entry, 0) exactResults = make([]Entry, 0)
otherResults = make([]Entry, 0)
for _, jmdictEntry := range dict.Entries { for _, jmdictEntry := range dict.Entries {
exactMatch := false
for _, kanji := range jmdictEntry.Kanji { for _, kanji := range jmdictEntry.Kanji {
if kanji.Expression == query { if kanji.Expression == query {
exactMatch = true
goto match
}
if strings.Contains(kanji.Expression, query) {
goto match goto match
} }
} }
// TODO: Skip if query contains kanji
for _, reading := range jmdictEntry.Readings { for _, reading := range jmdictEntry.Readings {
if reading.Reading == query { if strings.Contains(reading.Reading, query) {
goto match goto match
} }
} }
continue continue
match: match:
entry := ParseEntry(jmdictEntry) entry := ParseEntry(jmdictEntry)
entries = append(entries, entry) if exactMatch {
exactResults = append(exactResults, entry)
} else {
otherResults = append(otherResults, entry)
}
} }
return entries return
} }
func main() { func main() {
@ -108,16 +119,24 @@ func main() {
w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Type", "application/json; charset=utf-8")
r.ParseMultipartForm(0) r.ParseMultipartForm(0)
query := r.FormValue("q") query := r.FormValue("q")
entries := Search(query) exactResults, otherResults := Search(query)
jsonBytes, _ := json.Marshal(entries) jsonBytes, _ := json.Marshal(append(exactResults, otherResults...))
fmt.Fprint(w, string(jsonBytes)) fmt.Fprint(w, string(jsonBytes))
return false return false
}, },
func(w http.ResponseWriter, r *http.Request) any { func(w http.ResponseWriter, r *http.Request) any {
r.ParseMultipartForm(0) r.ParseMultipartForm(0)
query := r.FormValue("q") query := r.FormValue("q")
entry := Search(query) exactResults, otherResults := Search(query)
return entry return struct {
ExactResults []Entry
OtherResults []Entry
Count int
}{
ExactResults: exactResults,
OtherResults: otherResults,
Count: len(exactResults) + len(otherResults),
}
}, },
[]string{http.MethodGet, http.MethodPost}, []string{http.MethodGet, http.MethodPost},
)) ))

@ -1,23 +1,8 @@
<p><i>{{ $count := (len .) }}{{ if eq $count 0 }}No results{{ else }}{{ $count }} result{{ if ne $count 1}}s{{ end }}{{ end }}.</i></p> <p><i>{{ if eq .Count 0 }}No results{{ else }}{{ .Count }} result{{ if ne .Count 1}}s{{ end }}{{ end }}.</i></p>
{{ range . -}} {{- range .ExactResults -}}
<div class="box"> {{- template "word" . -}}
<h3> {{- end }}
{{- if .Kanji -}} <hr>
<ruby>{{- .Kanji -}}<rp>(</rp><rt>{{- .Reading -}}</rt><rp>)</rp></ruby> {{ range .OtherResults -}}
{{- else -}} {{ template "word" . }}
{{- .Reading -}} {{- end -}}
{{- end -}}
</h3>
{{ if le (len .Definitions) 2 -}}
<p>{{- template "definition" (index .Definitions 0) -}}</p>
{{- else -}}
<ol>
{{- range .Definitions }}
<li>
{{ template "definition" . }}
</li>
{{- end }}
</ol>
{{- end }}
</div>
{{ end -}}

@ -0,0 +1,22 @@
{{ define "word" }}
<div class="box">
<h3>
{{- if .Kanji -}}
<ruby>{{- .Kanji -}}<rp>(</rp><rt>{{- .Reading -}}</rt><rp>)</rp></ruby>
{{- else -}}
{{- .Reading -}}
{{- end -}}
</h3>
{{ if le (len .Definitions) 2 -}}
<p>{{- template "definition" (index .Definitions 0) -}}</p>
{{- else -}}
<ol>
{{- range .Definitions }}
<li>
{{ template "definition" . }}
</li>
{{- end }}
</ol>
{{- end }}
</div>
{{ end }}