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)
entries := make([]Entry, 0)
exactResults = make([]Entry, 0)
otherResults = make([]Entry, 0)
for _, jmdictEntry := range dict.Entries {
exactMatch := false
for _, kanji := range jmdictEntry.Kanji {
if kanji.Expression == query {
exactMatch = true
goto match
}
if strings.Contains(kanji.Expression, query) {
goto match
}
}
// TODO: Skip if query contains kanji
for _, reading := range jmdictEntry.Readings {
if reading.Reading == query {
if strings.Contains(reading.Reading, query) {
goto match
}
}
continue
match:
entry := ParseEntry(jmdictEntry)
entries = append(entries, entry)
if exactMatch {
exactResults = append(exactResults, entry)
} else {
otherResults = append(otherResults, entry)
}
return entries
}
return
}
func main() {
@ -108,16 +119,24 @@ func main() {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
r.ParseMultipartForm(0)
query := r.FormValue("q")
entries := Search(query)
jsonBytes, _ := json.Marshal(entries)
exactResults, otherResults := Search(query)
jsonBytes, _ := json.Marshal(append(exactResults, otherResults...))
fmt.Fprint(w, string(jsonBytes))
return false
},
func(w http.ResponseWriter, r *http.Request) any {
r.ParseMultipartForm(0)
query := r.FormValue("q")
entry := Search(query)
return entry
exactResults, otherResults := Search(query)
return struct {
ExactResults []Entry
OtherResults []Entry
Count int
}{
ExactResults: exactResults,
OtherResults: otherResults,
Count: len(exactResults) + len(otherResults),
}
},
[]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>
{{ range . -}}
<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>
<p><i>{{ if eq .Count 0 }}No results{{ else }}{{ .Count }} result{{ if ne .Count 1}}s{{ end }}{{ end }}.</i></p>
{{- range .ExactResults -}}
{{- template "word" . -}}
{{- end }}
</div>
{{ end -}}
<hr>
{{ range .OtherResults -}}
{{ template "word" . }}
{{- 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 }}