1
This commit is contained in:
Alex Yatskov 2021-01-01 14:31:58 -08:00
parent 4751a786b9
commit 795af5caa1
19 changed files with 135 additions and 150 deletions

4
.gitignore vendored
View File

@ -1,2 +1,2 @@
yomichan-import*
!yomichan-import.bat
yomichan-gtk/yomichan-gtk*
yomichan/yomichan*

View File

@ -20,7 +20,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"archive/zip"
@ -33,6 +33,14 @@ import (
"strings"
)
const (
DefaultFormat = ""
DefaultLanguage = ""
DefaultPretty = false
DefaultStride = 10000
DefaultTitle = ""
)
const databaseFormat = 3
type dbRecord []interface{}
@ -302,3 +310,29 @@ func detectFormat(path *string) (string, error) {
return "", errors.New("unrecognized dictionary format")
}
func ExportDb(inputPath, outputPath, format, language, title string, stride int, pretty bool) error {
handlers := map[string]func(string, string, string, string, int, bool) error{
"edict": jmdictExportDb,
"enamdict": jmnedictExportDb,
"epwing": epwingExportDb,
"kanjidic": kanjidicExportDb,
"rikai": rikaiExportDb,
"kanjifreq": frequencyKanjiExportDb,
"termfreq": frequencyTermsExportDb,
}
var err error
if format == DefaultFormat {
if format, err = detectFormat(&inputPath); err != nil {
return err
}
}
handler, ok := handlers[strings.ToLower(format)]
if !ok {
return errors.New("unrecognized dictionary format")
}
return handler(inputPath, outputPath, strings.ToLower(language), title, stride, pretty)
}

View File

@ -20,7 +20,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"regexp"

View File

@ -20,7 +20,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"regexp"

View File

@ -20,7 +20,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"os"

View File

@ -20,7 +20,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"os"

View File

@ -20,11 +20,10 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"fmt"
"log"
"regexp"
"strconv"
"strings"
@ -69,8 +68,6 @@ func epwingExportDb(inputPath, outputPath, language, title string, stride int, p
sequence int
)
log.Println("formatting dictionary data...")
for _, subbook := range book.Subbooks {
if extractor, ok := epwingExtractors[subbook.Title]; ok {
fontNarrow := extractor.getFontNarrow()

View File

@ -20,7 +20,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"bufio"

View File

@ -20,7 +20,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"regexp"

View File

@ -20,7 +20,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"os"

View File

@ -20,7 +20,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"regexp"

View File

@ -20,7 +20,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"regexp"

118
main.go
View File

@ -1,118 +0,0 @@
/*
* Copyright (c) 2016 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
)
const (
defaultStride = 10000
defaultLanguage = "english"
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] input-path output-path\n", path.Base(os.Args[0]))
fmt.Fprint(os.Stderr, "https://foosoft.net/projects/yomichan-import/\n\n")
fmt.Fprint(os.Stderr, "Parameters:\n")
flag.PrintDefaults()
}
func exportDb(inputPath, outputPath, format, language, title string, stride int, pretty bool) error {
handlers := map[string]func(string, string, string, string, int, bool) error{
"edict": jmdictExportDb,
"enamdict": jmnedictExportDb,
"epwing": epwingExportDb,
"kanjidic": kanjidicExportDb,
"rikai": rikaiExportDb,
"kanjifreq": frequencyKanjiExportDb,
"termfreq": frequencyTermsExportDb,
}
handler, ok := handlers[strings.ToLower(format)]
if !ok {
return errors.New("unrecognized dictionary format")
}
log.Printf("converting '%s' to '%s' in '%s' format...", inputPath, outputPath, format)
if err := handler(inputPath, outputPath, strings.ToLower(language), title, stride, pretty); err != nil {
log.Printf("conversion process failed: %s", err.Error())
return err
}
log.Print("conversion process complete")
return nil
}
func makeTmpDir() (string, error) {
return ioutil.TempDir("", "yomichan_tmp_")
}
func main() {
var (
format = flag.String("format", "", "dictionary format [edict|enamdict|epwing|kanjidic|rikai]")
language = flag.String("language", defaultLanguage, "dictionary language (if supported)")
title = flag.String("title", "", "dictionary title")
stride = flag.Int("stride", defaultStride, "dictionary bank stride")
pretty = flag.Bool("pretty", false, "output prettified dictionary JSON")
)
flag.Usage = usage
flag.Parse()
if flag.NArg() != 2 {
if err := gui(); err == nil {
return
}
usage()
os.Exit(2)
}
var (
inputPath = flag.Arg(0)
outputPath = flag.Arg(1)
)
if _, err := os.Stat(inputPath); err != nil {
log.Fatalf("dictionary path '%s' does not exist", inputPath)
}
if *format == "" {
var err error
if *format, err = detectFormat(&inputPath); err != nil {
log.Fatal(err)
}
}
if err := exportDb(inputPath, outputPath, *format, *language, *title, *stride, *pretty); err != nil {
log.Fatal(err)
}
}

View File

@ -20,7 +20,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"regexp"

View File

@ -20,7 +20,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"database/sql"

View File

@ -20,7 +20,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
package yomichan
import (
"regexp"

View File

@ -29,6 +29,8 @@ import (
"github.com/andlabs/ui"
_ "github.com/andlabs/ui/winmanifest"
yomichan "github.com/FooSoft/yomichan-import"
)
type logger struct {
@ -58,7 +60,11 @@ func gui() error {
pathTargetBox.Append(pathTargetButton, false)
titleEntry := ui.NewEntry()
titleEntry.SetText(yomichan.DefaultTitle)
languageEntry := ui.NewEntry()
languageEntry.SetText(yomichan.DefaultLanguage)
outputEntry := ui.NewEntry()
importButton := ui.NewButton("Import dictionary...")
@ -116,28 +122,26 @@ func gui() error {
return
}
format, err := detectFormat(&inputPath)
if err != nil {
ui.MsgBoxError(window, "Error", "Unable to detect dictionary format")
importButton.Enable()
return
}
title := titleEntry.Text()
language := languageEntry.Text()
go func() {
var success bool
var err error
defer ui.QueueMain(func() {
importButton.Enable()
if success {
if err == nil {
ui.MsgBox(window, "Success", "Conversion process complete")
} else {
ui.MsgBox(window, "Error", "Conversion process failed")
}
})
success = exportDb(inputPath, outputPath, format, language, title, defaultStride, false) == nil
err = yomichan.ExportDb(
inputPath,
outputPath,
yomichan.DefaultFormat,
languageEntry.Text(),
titleEntry.Text(),
yomichan.DefaultStride,
yomichan.DefaultPretty,
)
}()
})
@ -149,3 +153,9 @@ func gui() error {
window.Show()
})
}
func main() {
if err := gui(); err != nil {
log.Fatal(err)
}
}

62
yomichan/main.go Normal file
View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2016 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
yomichan "github.com/FooSoft/yomichan-import"
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] input-path output-path\n", path.Base(os.Args[0]))
fmt.Fprint(os.Stderr, "https://foosoft.net/projects/yomichan-import/\n\n")
fmt.Fprint(os.Stderr, "Parameters:\n")
flag.PrintDefaults()
}
func main() {
var (
format = flag.String("format", yomichan.DefaultFormat, "dictionary format [edict|enamdict|epwing|kanjidic|rikai]")
language = flag.String("language", yomichan.DefaultLanguage, "dictionary language (if supported)")
title = flag.String("title", yomichan.DefaultTitle, "dictionary title")
stride = flag.Int("stride", yomichan.DefaultStride, "dictionary bank stride")
pretty = flag.Bool("pretty", yomichan.DefaultPretty, "output prettified dictionary JSON")
)
flag.Usage = usage
flag.Parse()
if flag.NArg() != 2 {
usage()
os.Exit(2)
}
if err := yomichan.ExportDb(flag.Arg(0), flag.Arg(1), *format, *language, *title, *stride, *pretty); err != nil {
log.Fatal(err)
}
}