generated from ElnuDev/rust-project
Compare commits
2 Commits
04fb564b9d
...
ace46c56f4
Author | SHA1 | Date |
---|---|---|
Elnu | ace46c56f4 | 1 year ago |
Elnu | 22bbc83d83 | 1 year ago |
@ -0,0 +1,11 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "jichanorg-dict"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "jichanorg-jmdict"
|
||||
version = "0.1.0"
|
@ -1,8 +1,2 @@
|
||||
[package]
|
||||
name = ""
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
[workspace]
|
||||
members = ["dict", "jmdict"]
|
||||
|
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "jichanorg-dict"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "jichanorg-jmdict"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
@ -0,0 +1 @@
|
||||
use nix
|
@ -0,0 +1,2 @@
|
||||
JMdict*.xml
|
||||
JMdict*.json
|
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# https://www.edrdg.org/wiki/index.php/JMdict-EDICT_Dictionary_Project#CURRENT_VERSION_&_DOWNLOAD
|
||||
wget http://ftp.edrdg.org/pub/Nihongo/JMdict.gz
|
||||
|
||||
# Extract and delete archive
|
||||
gzip -d JMdict.gz
|
||||
|
||||
# Get date
|
||||
date=`grep -m 1 -oE '<!-- JMdict created: [0-9]{4}-[0-9]{2}-[0-9]{2} -->' JMdict | sed -E 's/<!-- JMdict created: (.+) -->/\1/'`
|
||||
|
||||
rm -f JMdict*.xml
|
||||
|
||||
# Add XML extension
|
||||
mv JMdict JMdict-${date}.xml
|
@ -0,0 +1,5 @@
|
||||
module git.elnu.com/ElnuDev/jichanorg-rs/jmdict/loader
|
||||
|
||||
go 1.20
|
||||
|
||||
require foosoft.net/projects/jmdict v0.0.0-20220714211640-cc9bc30b68a3
|
@ -0,0 +1,2 @@
|
||||
foosoft.net/projects/jmdict v0.0.0-20220714211640-cc9bc30b68a3 h1:zjHGpgUR2WP3pf6NVZM38OKYNse0GjovCW2v23V72PQ=
|
||||
foosoft.net/projects/jmdict v0.0.0-20220714211640-cc9bc30b68a3/go.mod h1:ZrjLCcE7ZrND28ZOSGYMd78tL+Dffiv2g+NjOMKgnew=
|
@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"foosoft.net/projects/jmdict"
|
||||
)
|
||||
|
||||
const filetype = ".xml"
|
||||
|
||||
func open() (*os.File, error) {
|
||||
files, err := os.ReadDir(".")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if !file.IsDir() && strings.HasSuffix(file.Name(), filetype) {
|
||||
file, err := os.Open(file.Name())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func loadJmdict() (*jmdict.Jmdict, string, error) {
|
||||
reader, err := open()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
} else if reader == nil {
|
||||
return nil, "", nil
|
||||
}
|
||||
dict, _, err := jmdict.LoadJmdict(reader)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return &dict, reader.Name(), nil
|
||||
}
|
||||
|
||||
func dictToJson(dict *jmdict.Jmdict, pretty bool) ([]byte, error) {
|
||||
if pretty {
|
||||
return json.MarshalIndent(dict, " ", "\t")
|
||||
} else {
|
||||
return json.Marshal(dict)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
dict, filename, err := loadJmdict()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
} else if dict == nil {
|
||||
fmt.Println("Couldn't find any XML files in directory!")
|
||||
return
|
||||
}
|
||||
json, err := dictToJson(dict, true)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = os.WriteFile(filename[:len(filename)-len(filetype)]+".json", json, 0666)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
{ pkgs ? import <nixpkgs> {}}:
|
||||
|
||||
pkgs.mkShell {
|
||||
buildInputs = with pkgs; [
|
||||
go
|
||||
gopls
|
||||
];
|
||||
shellHook = ''
|
||||
export PATH="$HOME/go/bin:$PATH"
|
||||
'';
|
||||
}
|
Loading…
Reference in new issue