WIP
This commit is contained in:
parent
603486f8a4
commit
300f838a95
75
jmdict.go
75
jmdict.go
@ -24,7 +24,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/FooSoft/jmdict"
|
"github.com/FooSoft/jmdict"
|
||||||
)
|
)
|
||||||
@ -52,6 +51,52 @@ func findString(needle string, haystack []string) int {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func convertEnamdictEntry(enamdictEntry jmdict.EnamdictEntry) []dictEntry {
|
||||||
|
var entries []dictEntry
|
||||||
|
|
||||||
|
convert := func(reading jmdict.EnamdictReading, kanji *jmdict.EnamdictKanji) {
|
||||||
|
if kanji != nil && findString(kanji.Expression, reading.Restrictions) != -1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := dictEntry{Tags: make(map[string]bool)}
|
||||||
|
|
||||||
|
if kanji == nil {
|
||||||
|
entry.Expression = reading.Reading
|
||||||
|
} else {
|
||||||
|
entry.Expression = kanji.Expression
|
||||||
|
entry.Reading = reading.Reading
|
||||||
|
|
||||||
|
entry.addTags(kanji.Information)
|
||||||
|
entry.addTags(kanji.Priorities)
|
||||||
|
}
|
||||||
|
|
||||||
|
entry.addTags(reading.Information)
|
||||||
|
entry.addTags(reading.Priorities)
|
||||||
|
|
||||||
|
for _, trans := range enamdictEntry.Translations {
|
||||||
|
entry.Glossary = append(entry.Glossary, trans.Translations...)
|
||||||
|
entry.addTags(trans.NameTypes)
|
||||||
|
}
|
||||||
|
|
||||||
|
entries = append(entries, entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(enamdictEntry.Kanji) > 0 {
|
||||||
|
for _, kanji := range enamdictEntry.Kanji {
|
||||||
|
for _, reading := range enamdictEntry.Readings {
|
||||||
|
convert(reading, &kanji)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, reading := range enamdictEntry.Readings {
|
||||||
|
convert(reading, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return entries
|
||||||
|
}
|
||||||
|
|
||||||
func convertEdictEntry(edictEntry jmdict.EdictEntry) []dictEntry {
|
func convertEdictEntry(edictEntry jmdict.EdictEntry) []dictEntry {
|
||||||
var entries []dictEntry
|
var entries []dictEntry
|
||||||
|
|
||||||
@ -69,11 +114,11 @@ func convertEdictEntry(edictEntry jmdict.EdictEntry) []dictEntry {
|
|||||||
entry.Reading = reading.Reading
|
entry.Reading = reading.Reading
|
||||||
|
|
||||||
entry.addTags(kanji.Information)
|
entry.addTags(kanji.Information)
|
||||||
entry.addTags(kanji.Priority)
|
entry.addTags(kanji.Priorities)
|
||||||
}
|
}
|
||||||
|
|
||||||
entry.addTags(reading.Information)
|
entry.addTags(reading.Information)
|
||||||
entry.addTags(reading.Priority)
|
entry.addTags(reading.Priorities)
|
||||||
|
|
||||||
for _, sense := range edictEntry.Sense {
|
for _, sense := range edictEntry.Sense {
|
||||||
if findString(reading.Reading, sense.RestrictedReadings) != -1 {
|
if findString(reading.Reading, sense.RestrictedReadings) != -1 {
|
||||||
@ -88,10 +133,10 @@ func convertEdictEntry(edictEntry jmdict.EdictEntry) []dictEntry {
|
|||||||
entry.Glossary = append(entry.Glossary, glossary.Content)
|
entry.Glossary = append(entry.Glossary, glossary.Content)
|
||||||
}
|
}
|
||||||
|
|
||||||
entry.addTags(sense.PartOfSpeech)
|
entry.addTags(sense.PartsOfSpeech)
|
||||||
entry.addTags(sense.Fields)
|
entry.addTags(sense.Fields)
|
||||||
entry.addTags(sense.Misc)
|
entry.addTags(sense.Misc)
|
||||||
entry.addTags(sense.Dialect)
|
entry.addTags(sense.Dialects)
|
||||||
}
|
}
|
||||||
|
|
||||||
entries = append(entries, entry)
|
entries = append(entries, entry)
|
||||||
@ -99,12 +144,12 @@ func convertEdictEntry(edictEntry jmdict.EdictEntry) []dictEntry {
|
|||||||
|
|
||||||
if len(edictEntry.Kanji) > 0 {
|
if len(edictEntry.Kanji) > 0 {
|
||||||
for _, kanji := range edictEntry.Kanji {
|
for _, kanji := range edictEntry.Kanji {
|
||||||
for _, reading := range edictEntry.Reading {
|
for _, reading := range edictEntry.Readings {
|
||||||
convert(reading, &kanji)
|
convert(reading, &kanji)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, reading := range edictEntry.Reading {
|
for _, reading := range edictEntry.Readings {
|
||||||
convert(reading, nil)
|
convert(reading, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -112,6 +157,20 @@ func convertEdictEntry(edictEntry jmdict.EdictEntry) []dictEntry {
|
|||||||
return entries
|
return entries
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func processEnamdict(reader io.Reader, writer io.Writer) error {
|
||||||
|
enamdictEntries, _, err := jmdict.LoadEnamdict(reader, false)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var entries []dictEntry
|
||||||
|
for _, enamdictEntry := range enamdictEntries {
|
||||||
|
entries = append(entries, convertEnamdictEntry(enamdictEntry)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func processEdict(reader io.Reader, writer io.Writer) error {
|
func processEdict(reader io.Reader, writer io.Writer) error {
|
||||||
edictEntries, _, err := jmdict.LoadEdict(reader, false)
|
edictEntries, _, err := jmdict.LoadEdict(reader, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -123,7 +182,5 @@ func processEdict(reader io.Reader, writer io.Writer) error {
|
|||||||
entries = append(entries, convertEdictEntry(edictEntry)...)
|
entries = append(entries, convertEdictEntry(edictEntry)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Print(entries[42])
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
1
main.go
1
main.go
@ -38,6 +38,7 @@ func usage() {
|
|||||||
func process(fileFormat, inputFile, outputFile string) error {
|
func process(fileFormat, inputFile, outputFile string) error {
|
||||||
handlers := map[string]func(io.Reader, io.Writer) error{
|
handlers := map[string]func(io.Reader, io.Writer) error{
|
||||||
"edict": processEdict,
|
"edict": processEdict,
|
||||||
|
"enamdict": processEnamdict,
|
||||||
}
|
}
|
||||||
|
|
||||||
handler, ok := handlers[fileFormat]
|
handler, ok := handlers[fileFormat]
|
||||||
|
Loading…
Reference in New Issue
Block a user