feat: Support specifying Rime config directory via command args

This commit is contained in:
hegotit 2024-08-13 15:33:52 +08:00
parent 77a762b7d0
commit 0460c173aa
8 changed files with 417 additions and 374 deletions

View File

@ -68,6 +68,11 @@ SORT:
}
func areYouOK() {
if rime.AutoConfirm {
fmt.Println("Auto confirm enabled. Skipping prompt.")
return
}
fmt.Println("Are you OK:")
var isOK string
_, _ = fmt.Scanf("%s", &isOK)

View File

@ -28,7 +28,7 @@ var (
)
// 初始化特殊词汇列表、需要注音列表、错别字列表、拼音列表
func init() {
func initCheck() {
// 特殊词汇列表,不进行任何检查
specialWords.Add("狄尔斯–阿尔德反应")
specialWords.Add("特里斯坦–达库尼亚")
@ -102,7 +102,7 @@ func init() {
text, code := parts[0], parts[1]
hanPinyin[text] = append(hanPinyin[text], code)
}
// 给 hanPinyin 补充不字表的读音,和过滤列表 hanPinyinFilter
// 给 hanPinyin 补充不字表的读音,和过滤列表 hanPinyinFilter
file4, err := os.Open(汉字拼音映射TXT)
if err != nil {
log.Fatalln(err)

View File

@ -118,330 +118,341 @@ type schema struct {
file *os.File
}
var doublePinyin = schema{
name: "cn_en_double_pinyin",
desc: "自然码双拼",
combinationType: "unique",
path: filepath.Join(RimeDir, "en_dicts/cn_en_double_pinyin.txt"),
mapping: map[string]string{
// 零声母
"-a-": "aa",
"-e-": "ee",
"-o-": "oo",
"-ai-": "ai",
"-ei-": "ei",
"-ou-": "ou",
"-an-": "an",
"-en-": "en",
"-ang-": "ah",
"-eng-": "eg",
"-ao-": "ao",
"-er-": "er",
// zh ch sh
"zh": "v",
"ch": "i",
"sh": "u",
// 韵母
"iu": "q",
"ia": "w",
"ua": "w",
"uan": "r",
"ue": "t",
"ve": "t",
"ing": "y",
"uai": "y",
"uo": "o",
"un": "p",
"iong": "s",
"ong": "s",
"iang": "d",
"uang": "d",
"en": "f",
"eng": "g",
"ang": "h",
"an": "j",
"ao": "k",
"ai": "l",
"ei": "z",
"ie": "x",
"iao": "c",
"ui": "v",
"ou": "b",
"in": "n",
"ian": "m",
},
}
var (
doublePinyin schema
doublePinyinFlypy schema
doublePinyinMSPY schema
doublePinyinSogou schema
doublePinyinZiGuang schema
doublePinyinABC schema
)
var doublePinyinFlypy = schema{
name: "cn_en_flypy",
desc: "小鹤双拼",
combinationType: "unique",
path: filepath.Join(RimeDir, "en_dicts/cn_en_flypy.txt"),
mapping: map[string]string{
// 零声母
"-a-": "aa",
"-e-": "ee",
"-o-": "oo",
"-ai-": "ai",
"-ei-": "ei",
"-ou-": "ou",
"-an-": "an",
"-en-": "en",
"-ang-": "ah",
"-eng-": "eg",
"-ao-": "ao",
"-er-": "er",
// zh ch sh
"zh": "v",
"ch": "i",
"sh": "u",
// 韵母
"iu": "q",
"ei": "w",
"uan": "r",
"ue": "t",
"ve": "t",
"un": "y",
"uo": "o",
"ie": "p",
"iong": "s",
"ong": "s",
"ai": "d",
"en": "f",
"eng": "g",
"ang": "h",
"an": "j",
"ing": "k",
"uai": "k",
"iang": "l",
"uang": "l",
"ou": "z",
"ia": "x",
"ua": "x",
"ao": "c",
"ui": "v",
"in": "b",
"iao": "n",
"ian": "m",
},
}
func initSchemas() {
doublePinyin = schema{
name: "cn_en_double_pinyin",
desc: "自然码双拼",
combinationType: "unique",
path: filepath.Join(RimeDir, "en_dicts/cn_en_double_pinyin.txt"),
mapping: map[string]string{
// 零声母
"-a-": "aa",
"-e-": "ee",
"-o-": "oo",
"-ai-": "ai",
"-ei-": "ei",
"-ou-": "ou",
"-an-": "an",
"-en-": "en",
"-ang-": "ah",
"-eng-": "eg",
"-ao-": "ao",
"-er-": "er",
// zh ch sh
"zh": "v",
"ch": "i",
"sh": "u",
// 韵母
"iu": "q",
"ia": "w",
"ua": "w",
"uan": "r",
"ue": "t",
"ve": "t",
"ing": "y",
"uai": "y",
"uo": "o",
"un": "p",
"iong": "s",
"ong": "s",
"iang": "d",
"uang": "d",
"en": "f",
"eng": "g",
"ang": "h",
"an": "j",
"ao": "k",
"ai": "l",
"ei": "z",
"ie": "x",
"iao": "c",
"ui": "v",
"ou": "b",
"in": "n",
"ian": "m",
},
}
var doublePinyinMSPY = schema{
name: "cn_en_mspy",
desc: "微软双拼",
combinationType: "unique",
path: filepath.Join(RimeDir, "en_dicts/cn_en_mspy.txt"),
mapping: map[string]string{
// 零声母
"-a-": "oa",
"-e-": "oe",
"-o-": "oo",
"-ai-": "ol",
"-ei-": "oz",
"-ou-": "ob",
"-an-": "oj",
"-en-": "of",
"-ang-": "oh",
"-eng-": "og",
"-ao-": "ok",
"-er-": "or",
// zh ch sh
"zh": "v",
"ch": "i",
"sh": "u",
// 韵母
"iu": "q",
"ia": "w",
"ua": "w",
"er": "r",
"uan": "r",
"ue": "t",
"uai": "y",
"uo": "o",
"un": "p",
"iong": "s",
"ong": "s",
"iang": "d",
"uang": "d",
"en": "f",
"eng": "g",
"ang": "h",
"an": "j",
"ao": "k",
"ai": "l",
"ing": ";",
"ei": "z",
"ie": "x",
"iao": "c",
"ui": "v",
"ve": "v",
"ou": "b",
"in": "n",
"ian": "m",
},
}
doublePinyinFlypy = schema{
name: "cn_en_flypy",
desc: "小鹤双拼",
combinationType: "unique",
path: filepath.Join(RimeDir, "en_dicts/cn_en_flypy.txt"),
mapping: map[string]string{
// 零声母
"-a-": "aa",
"-e-": "ee",
"-o-": "oo",
"-ai-": "ai",
"-ei-": "ei",
"-ou-": "ou",
"-an-": "an",
"-en-": "en",
"-ang-": "ah",
"-eng-": "eg",
"-ao-": "ao",
"-er-": "er",
// zh ch sh
"zh": "v",
"ch": "i",
"sh": "u",
// 韵母
"iu": "q",
"ei": "w",
"uan": "r",
"ue": "t",
"ve": "t",
"un": "y",
"uo": "o",
"ie": "p",
"iong": "s",
"ong": "s",
"ai": "d",
"en": "f",
"eng": "g",
"ang": "h",
"an": "j",
"ing": "k",
"uai": "k",
"iang": "l",
"uang": "l",
"ou": "z",
"ia": "x",
"ua": "x",
"ao": "c",
"ui": "v",
"in": "b",
"iao": "n",
"ian": "m",
},
}
var doublePinyinSogou = schema{
name: "cn_en_sogou",
desc: "搜狗双拼",
combinationType: "unique",
path: filepath.Join(RimeDir, "en_dicts/cn_en_sogou.txt"),
mapping: map[string]string{
// 零声母
"-a-": "oa",
"-e-": "oe",
"-o-": "oo",
"-ai-": "ol",
"-ei-": "oz",
"-ou-": "ob",
"-an-": "oj",
"-en-": "of",
"-ang-": "oh",
"-eng-": "og",
"-ao-": "ok",
"-er-": "or",
// zh ch sh
"zh": "v",
"ch": "i",
"sh": "u",
// 韵母
"iu": "q",
"ia": "w",
"ua": "w",
"er": "r",
"uan": "r",
"ue": "t",
"uai": "y",
"uo": "o",
"un": "p",
"iong": "s",
"ong": "s",
"iang": "d",
"uang": "d",
"en": "f",
"eng": "g",
"ang": "h",
"an": "j",
"ao": "k",
"ai": "l",
"ing": ";",
"ei": "z",
"ie": "x",
"iao": "c",
"ui": "v",
"ve": "t",
"ou": "b",
"in": "n",
"ian": "m",
},
}
doublePinyinMSPY = schema{
name: "cn_en_mspy",
desc: "微软双拼",
combinationType: "unique",
path: filepath.Join(RimeDir, "en_dicts/cn_en_mspy.txt"),
mapping: map[string]string{
// 零声母
"-a-": "oa",
"-e-": "oe",
"-o-": "oo",
"-ai-": "ol",
"-ei-": "oz",
"-ou-": "ob",
"-an-": "oj",
"-en-": "of",
"-ang-": "oh",
"-eng-": "og",
"-ao-": "ok",
"-er-": "or",
// zh ch sh
"zh": "v",
"ch": "i",
"sh": "u",
// 韵母
"iu": "q",
"ia": "w",
"ua": "w",
"er": "r",
"uan": "r",
"ue": "t",
"uai": "y",
"uo": "o",
"un": "p",
"iong": "s",
"ong": "s",
"iang": "d",
"uang": "d",
"en": "f",
"eng": "g",
"ang": "h",
"an": "j",
"ao": "k",
"ai": "l",
"ing": ";",
"ei": "z",
"ie": "x",
"iao": "c",
"ui": "v",
"ve": "v",
"ou": "b",
"in": "n",
"ian": "m",
},
}
var doublePinyinZiGuang = schema{
name: "cn_en_ziguang",
desc: "紫光双拼",
combinationType: "unique",
path: filepath.Join(RimeDir, "en_dicts/cn_en_ziguang.txt"),
mapping: map[string]string{
// 零声母
"-a-": "oa",
"-e-": "oe",
"-o-": "oo",
"-ai-": "op",
"-ei-": "ok",
"-ou-": "oz",
"-an-": "or",
"-en-": "ow",
"-ang-": "os",
"-eng-": "ot",
"-ao-": "oq",
"-er-": "oj",
// zh ch sh
"zh": "u",
"ch": "a",
"sh": "i",
// 韵母
"ao": "q",
"en": "w",
"an": "r",
"eng": "t",
"in": "y",
"uai": "y",
"uo": "o",
"ai": "p",
"ang": "s",
"ie": "d",
"ian": "f",
"iang": "g",
"uang": "g",
"iong": "h",
"ong": "h",
"er": "j",
"iu": "j",
"ei": "k",
"uan": "l",
"ing": ";",
"ou": "z",
"ia": "x",
"ua": "x",
"iao": "b",
"ue": "n",
"ui": "n",
"un": "m",
},
}
doublePinyinSogou = schema{
name: "cn_en_sogou",
desc: "搜狗双拼",
combinationType: "unique",
path: filepath.Join(RimeDir, "en_dicts/cn_en_sogou.txt"),
mapping: map[string]string{
// 零声母
"-a-": "oa",
"-e-": "oe",
"-o-": "oo",
"-ai-": "ol",
"-ei-": "oz",
"-ou-": "ob",
"-an-": "oj",
"-en-": "of",
"-ang-": "oh",
"-eng-": "og",
"-ao-": "ok",
"-er-": "or",
// zh ch sh
"zh": "v",
"ch": "i",
"sh": "u",
// 韵母
"iu": "q",
"ia": "w",
"ua": "w",
"er": "r",
"uan": "r",
"ue": "t",
"uai": "y",
"uo": "o",
"un": "p",
"iong": "s",
"ong": "s",
"iang": "d",
"uang": "d",
"en": "f",
"eng": "g",
"ang": "h",
"an": "j",
"ao": "k",
"ai": "l",
"ing": ";",
"ei": "z",
"ie": "x",
"iao": "c",
"ui": "v",
"ve": "t",
"ou": "b",
"in": "n",
"ian": "m",
},
}
var doublePinyinABC = schema{
name: "cn_en_abc",
desc: "智能 ABC 双拼",
combinationType: "unique",
path: filepath.Join(RimeDir, "en_dicts/cn_en_abc.txt"),
mapping: map[string]string{
// 零声母
"-a-": "oa",
"-e-": "oe",
"-o-": "oo",
"-ai-": "ol",
"-ei-": "oq",
"-ou-": "ob",
"-an-": "oj",
"-en-": "of",
"-ang-": "oh",
"-eng-": "og",
"-ao-": "ok",
"-er-": "or",
// zh ch sh
"zh": "a",
"ch": "e",
"sh": "v",
// 韵母
"ao": "k",
"en": "f",
"an": "j",
"eng": "g",
"in": "c",
"uai": "c",
"uo": "o",
"ai": "l",
"ang": "h",
"ie": "x",
"ian": "w",
"iang": "t",
"uang": "t",
"iong": "s",
"ong": "s",
"er": "r",
"iu": "r",
"ei": "q",
"uan": "p",
"ing": "y",
"ou": "b",
"ia": "d",
"ua": "d",
"iao": "z",
"ue": "m",
"ui": "m",
"un": "n",
},
doublePinyinZiGuang = schema{
name: "cn_en_ziguang",
desc: "紫光双拼",
combinationType: "unique",
path: filepath.Join(RimeDir, "en_dicts/cn_en_ziguang.txt"),
mapping: map[string]string{
// 零声母
"-a-": "oa",
"-e-": "oe",
"-o-": "oo",
"-ai-": "op",
"-ei-": "ok",
"-ou-": "oz",
"-an-": "or",
"-en-": "ow",
"-ang-": "os",
"-eng-": "ot",
"-ao-": "oq",
"-er-": "oj",
// zh ch sh
"zh": "u",
"ch": "a",
"sh": "i",
// 韵母
"ao": "q",
"en": "w",
"an": "r",
"eng": "t",
"in": "y",
"uai": "y",
"uo": "o",
"ai": "p",
"ang": "s",
"ie": "d",
"ian": "f",
"iang": "g",
"uang": "g",
"iong": "h",
"ong": "h",
"er": "j",
"iu": "j",
"ei": "k",
"uan": "l",
"ing": ";",
"ou": "z",
"ia": "x",
"ua": "x",
"iao": "b",
"ue": "n",
"ui": "n",
"un": "m",
},
}
doublePinyinABC = schema{
name: "cn_en_abc",
desc: "智能 ABC 双拼",
combinationType: "unique",
path: filepath.Join(RimeDir, "en_dicts/cn_en_abc.txt"),
mapping: map[string]string{
// 零声母
"-a-": "oa",
"-e-": "oe",
"-o-": "oo",
"-ai-": "ol",
"-ei-": "oq",
"-ou-": "ob",
"-an-": "oj",
"-en-": "of",
"-ang-": "oh",
"-eng-": "og",
"-ao-": "ok",
"-er-": "or",
// zh ch sh
"zh": "a",
"ch": "e",
"sh": "v",
// 韵母
"ao": "k",
"en": "f",
"an": "j",
"eng": "g",
"in": "c",
"uai": "c",
"uo": "o",
"ai": "l",
"ang": "h",
"ie": "x",
"ian": "w",
"iang": "t",
"uang": "t",
"iong": "s",
"ong": "s",
"er": "r",
"iu": "r",
"ei": "q",
"uan": "p",
"ing": "y",
"ou": "b",
"ia": "d",
"ua": "d",
"iao": "z",
"ue": "m",
"ui": "m",
"un": "n",
},
}
}
// CnEn 从 others/cn_en.txt 生成全拼和各个双拼的中英混输词库

View File

@ -0,0 +1,18 @@
//go:build linux
// +build linux
package rime
import (
"log"
"os/user"
"path/filepath"
)
func getRimeDirForPlatform() string {
u, err := user.Current()
if err != nil {
log.Fatalln(err)
}
return filepath.Join(u.HomeDir, ".config", "rime")
}

View File

@ -5,26 +5,14 @@ package rime
import (
"log"
"os/user"
"path/filepath"
"runtime"
)
// 获取 macOS/Windows Rime 配置目录
func getRimeDir() string {
var dir string
switch runtime.GOOS {
case "darwin": // macOS
u, err := user.Current()
if err != nil {
log.Fatalln(err)
}
dir = filepath.Join(u.HomeDir, "Library/Rime")
// case "windows": // Windows
// dir = getWeaselDir()
default:
log.Fatalf("Unsupported OS: %s so far", runtime.GOOS)
// 获取 macOS Rime 配置目录
func getRimeDirForPlatform() string {
u, err := user.Current()
if err != nil {
log.Fatalln(err)
}
return dir
return filepath.Join(u.HomeDir, "Library/Rime")
}

View File

@ -7,31 +7,11 @@ import (
"golang.org/x/sys/windows/registry"
"log"
"os"
"os/user"
"path/filepath"
"runtime"
)
// 获取 macOS/Windows Rime 配置目录
func getRimeDir() string {
var dir string
switch runtime.GOOS {
case "darwin": // macOS
u, err := user.Current()
if err != nil {
log.Fatalln(err)
}
dir = filepath.Join(u.HomeDir, "Library/Rime")
case "windows": // Windows
dir = getWeaselDir()
default:
log.Fatalf("Unsupported OS: %s so far", runtime.GOOS)
}
return dir
}
func getWeaselDir() string {
// 获取 Windows Rime 配置目录
func getRimeDirForPlatform() string {
keyPath := `Software\Rime\Weasel`
valueName := "RimeUserDir"

View File

@ -238,7 +238,7 @@ var onlyOne = map[string]string{
"给": "gei",
}
func init() {
func initPinyin() {
// 从 base、ext 准备结巴的词典和词组拼音映射
for _, dictPath := range []string{BasePath, ExtPath} {
file, err := os.Open(dictPath)

View File

@ -2,6 +2,7 @@ package rime
import (
"bufio"
"flag"
"fmt"
mapset "github.com/deckarep/golang-set/v2"
"log"
@ -21,26 +22,66 @@ type lemma struct {
}
var (
mark = "# +_+" // 词库中的标记符号,表示从这行开始进行检查或排序
RimeDir = getRimeDir() // Rime 配置目录
mark = "# +_+" // 词库中的标记符号,表示从这行开始进行检查或排序
RimeDir string
EmojiMapPath string
EmojiPath string
HanziPath string
BasePath string
ExtPath string
TencentPath string
HanziSet mapset.Set[string]
BaseSet mapset.Set[string]
ExtSet mapset.Set[string]
TencentSet mapset.Set[string]
需要注音TXT string
错别字TXT string
汉字拼音映射TXT string
AutoConfirm bool
)
func init() {
// 定义命令行参数
flag.StringVar(&RimeDir, "rime_path", "", "Specify the Rime configuration directory")
flag.BoolVar(&AutoConfirm, "auto_confirm", false, "Automatically confirm the prompt")
flag.Parse()
RimeDir = getRimeDir(RimeDir) // Rime 配置目录
EmojiMapPath = filepath.Join(RimeDir, "others/emoji-map.txt")
EmojiPath = filepath.Join(RimeDir, "opencc/emoji.txt")
EmojiPath = filepath.Join(RimeDir, "opencc/emoji.txt")
HanziPath = filepath.Join(RimeDir, "cn_dicts/8105.dict.yaml")
BasePath = filepath.Join(RimeDir, "cn_dicts/base.dict.yaml")
ExtPath = filepath.Join(RimeDir, "cn_dicts/ext.dict.yaml")
HanziPath = filepath.Join(RimeDir, "cn_dicts/8105.dict.yaml")
BasePath = filepath.Join(RimeDir, "cn_dicts/base.dict.yaml")
ExtPath = filepath.Join(RimeDir, "cn_dicts/ext.dict.yaml")
TencentPath = filepath.Join(RimeDir, "cn_dicts/tencent.dict.yaml")
HanziSet = readToSet(HanziPath)
BaseSet = readToSet(BasePath)
ExtSet = readToSet(ExtPath)
HanziSet = readToSet(HanziPath)
BaseSet = readToSet(BasePath)
ExtSet = readToSet(ExtPath)
TencentSet = readToSet(TencentPath)
需要注音TXT = filepath.Join(RimeDir, "others/script/rime/需要注音.txt")
错别字TXT = filepath.Join(RimeDir, "others/script/rime/错别字.txt")
需要注音TXT = filepath.Join(RimeDir, "others/script/rime/需要注音.txt")
错别字TXT = filepath.Join(RimeDir, "others/script/rime/错别字.txt")
汉字拼音映射TXT = filepath.Join(RimeDir, "others/script/rime/汉字拼音映射.txt")
)
initCheck()
initSchemas()
initPinyin()
}
func getRimeDir(rimePath string) string {
if rimePath != "" {
absPath, err := filepath.Abs(rimePath)
if err != nil {
log.Fatalf("Failed to get absolute path: %v", err)
}
// 使用传入的路径
return absPath
}
return getRimeDirForPlatform()
}
// 将所有词库读入 set供检查或排序使用
func readToSet(dictPath string) mapset.Set[string] {