feat: Support specifying Rime config directory via command args

This commit is contained in:
hegotit 2024-08-13 15:33:52 +08:00 committed by mirtlecn
parent a8f5b6ceee
commit 918e6ed11b
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,7 +118,17 @@ type schema struct {
file *os.File
}
var doublePinyin = schema{
var (
doublePinyin schema
doublePinyinFlypy schema
doublePinyinMSPY schema
doublePinyinSogou schema
doublePinyinZiGuang schema
doublePinyinABC schema
)
func initSchemas() {
doublePinyin = schema{
name: "cn_en_double_pinyin",
desc: "自然码双拼",
combinationType: "unique",
@ -170,9 +180,9 @@ var doublePinyin = schema{
"in": "n",
"ian": "m",
},
}
}
var doublePinyinFlypy = schema{
doublePinyinFlypy = schema{
name: "cn_en_flypy",
desc: "小鹤双拼",
combinationType: "unique",
@ -224,9 +234,9 @@ var doublePinyinFlypy = schema{
"iao": "n",
"ian": "m",
},
}
}
var doublePinyinMSPY = schema{
doublePinyinMSPY = schema{
name: "cn_en_mspy",
desc: "微软双拼",
combinationType: "unique",
@ -279,9 +289,9 @@ var doublePinyinMSPY = schema{
"in": "n",
"ian": "m",
},
}
}
var doublePinyinSogou = schema{
doublePinyinSogou = schema{
name: "cn_en_sogou",
desc: "搜狗双拼",
combinationType: "unique",
@ -334,9 +344,9 @@ var doublePinyinSogou = schema{
"in": "n",
"ian": "m",
},
}
}
var doublePinyinZiGuang = schema{
doublePinyinZiGuang = schema{
name: "cn_en_ziguang",
desc: "紫光双拼",
combinationType: "unique",
@ -388,9 +398,9 @@ var doublePinyinZiGuang = schema{
"ui": "n",
"un": "m",
},
}
}
var doublePinyinABC = schema{
doublePinyinABC = schema{
name: "cn_en_abc",
desc: "智能 ABC 双拼",
combinationType: "unique",
@ -442,6 +452,7 @@ var doublePinyinABC = schema{
"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
// 获取 macOS Rime 配置目录
func getRimeDirForPlatform() string {
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
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"
@ -22,7 +23,30 @@ type lemma struct {
var (
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")
EmojiPath = filepath.Join(RimeDir, "opencc/emoji.txt")
@ -40,7 +64,24 @@ var (
需要注音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] {