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() { func areYouOK() {
if rime.AutoConfirm {
fmt.Println("Auto confirm enabled. Skipping prompt.")
return
}
fmt.Println("Are you OK:") fmt.Println("Are you OK:")
var isOK string var isOK string
_, _ = fmt.Scanf("%s", &isOK) _, _ = fmt.Scanf("%s", &isOK)

View File

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

View File

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

View File

@ -7,31 +7,11 @@ import (
"golang.org/x/sys/windows/registry" "golang.org/x/sys/windows/registry"
"log" "log"
"os" "os"
"os/user"
"path/filepath" "path/filepath"
"runtime"
) )
// 获取 macOS/Windows Rime 配置目录 // 获取 Windows Rime 配置目录
func getRimeDir() string { func getRimeDirForPlatform() 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 {
keyPath := `Software\Rime\Weasel` keyPath := `Software\Rime\Weasel`
valueName := "RimeUserDir" valueName := "RimeUserDir"

View File

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

View File

@ -2,6 +2,7 @@ package rime
import ( import (
"bufio" "bufio"
"flag"
"fmt" "fmt"
mapset "github.com/deckarep/golang-set/v2" mapset "github.com/deckarep/golang-set/v2"
"log" "log"
@ -21,26 +22,66 @@ type lemma struct {
} }
var ( var (
mark = "# +_+" // 词库中的标记符号,表示从这行开始进行检查或排序 mark = "# +_+" // 词库中的标记符号,表示从这行开始进行检查或排序
RimeDir = getRimeDir() // Rime 配置目录 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") 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") HanziPath = filepath.Join(RimeDir, "cn_dicts/8105.dict.yaml")
BasePath = filepath.Join(RimeDir, "cn_dicts/base.dict.yaml") BasePath = filepath.Join(RimeDir, "cn_dicts/base.dict.yaml")
ExtPath = filepath.Join(RimeDir, "cn_dicts/ext.dict.yaml") ExtPath = filepath.Join(RimeDir, "cn_dicts/ext.dict.yaml")
TencentPath = filepath.Join(RimeDir, "cn_dicts/tencent.dict.yaml") TencentPath = filepath.Join(RimeDir, "cn_dicts/tencent.dict.yaml")
HanziSet = readToSet(HanziPath) HanziSet = readToSet(HanziPath)
BaseSet = readToSet(BasePath) BaseSet = readToSet(BasePath)
ExtSet = readToSet(ExtPath) ExtSet = readToSet(ExtPath)
TencentSet = readToSet(TencentPath) 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") 汉字拼音映射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供检查或排序使用 // 将所有词库读入 set供检查或排序使用
func readToSet(dictPath string) mapset.Set[string] { func readToSet(dictPath string) mapset.Set[string] {