Add readings

This commit is contained in:
Alex Yatskov 2024-03-30 21:14:00 -07:00
parent 91e6c7438a
commit e2d85e441e
4 changed files with 67 additions and 3 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
*.json
*.txt
*.xml
kanji-merge*
rtk-merge*

2
go.mod
View File

@ -1,3 +1,5 @@
module git.foosoft.net/alex/rtk-merge
go 1.21.8
require github.com/themoeway/jmdict-go v0.0.0-20230321060422-fa8f5d54f364 // indirect

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/themoeway/jmdict-go v0.0.0-20230321060422-fa8f5d54f364 h1:JSSM5osTN5FCoj7uErWoMVY+IbDh+OG0aRJnKsxgngc=
github.com/themoeway/jmdict-go v0.0.0-20230321060422-fa8f5d54f364/go.mod h1:M9O+lJXOwrOqpN4VUv8Qkn1gsZ/SZaSAiF7nj0m1NZk=

64
main.go
View File

@ -2,11 +2,15 @@ package main
import (
"encoding/csv"
"errors"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/themoeway/jmdict-go"
)
type (
@ -36,6 +40,59 @@ func injectStories(table Table, path string) error {
}
func injectKanjidic(table Table, path string) error {
fp, err := os.Open(path)
if err != nil {
return err
}
defer fp.Close()
kd, err := jmdict.LoadKanjidic(fp)
if err != nil {
return err
}
for i, row := range table {
var found bool
for _, character := range kd.Characters {
if character.Literal != row[0] {
continue
}
var (
meanings []string
kunyomi []string
onyomi []string
)
for _, reading := range character.ReadingMeaning.Readings {
switch reading.Type {
case "ja_on":
onyomi = append(onyomi, reading.Value)
case "ja_kun":
kunyomi = append(kunyomi, reading.Value)
}
}
for _, meaning := range character.ReadingMeaning.Meanings {
if meaning.Language == nil {
meanings = append(meanings, meaning.Meaning)
}
}
const seperator = " "
row = append(row, strings.Join(meanings, seperator))
row = append(row, strings.Join(kunyomi, seperator))
row = append(row, strings.Join(onyomi, seperator))
table[i] = row
found = true
}
if !found {
return errors.New("character not found")
}
}
return nil
}
@ -57,6 +114,9 @@ func loadTable(path string) (Table, error) {
for i := range table {
table[i] = table[i][:3]
if len(table[i]) != 3 {
return nil, errors.New("unexpected column count")
}
}
return table, nil
@ -97,7 +157,7 @@ func main() {
os.Exit(2)
}
table, err := loadTable(os.Args[1])
table, err := loadTable(args[0])
if err != nil {
log.Fatal(err)
}
@ -114,7 +174,7 @@ func main() {
}
}
if err := saveTable(os.Args[2], table); err != nil {
if err := saveTable(args[1], table); err != nil {
log.Fatal(err)
}
}