rime-ice/others/script/rime/sort.go

302 lines
7.1 KiB
Go
Raw Permalink Normal View History

2022-10-30 16:47:40 +01:00
package rime
import (
"bufio"
2023-04-13 11:04:44 +02:00
"crypto/sha1"
"encoding/hex"
2022-10-30 16:47:40 +01:00
"fmt"
2023-04-13 11:04:44 +02:00
mapset "github.com/deckarep/golang-set/v2"
"io"
2022-10-30 16:47:40 +01:00
"log"
"os"
"path"
"sort"
"strconv"
"strings"
"time"
)
2023-04-13 11:04:44 +02:00
func Sort(dictPath string, _type int) {
2022-10-30 16:47:40 +01:00
// 控制台输出
defer updateVersion(dictPath, getSha1(dictPath))
defer printfTimeCost("排序 "+path.Base(dictPath), time.Now())
// 打开文件
file, err := os.OpenFile(dictPath, os.O_RDWR, 0644)
if err != nil {
2023-04-13 11:04:44 +02:00
log.Fatalln(err)
2022-10-30 16:47:40 +01:00
}
defer file.Close()
2023-04-13 11:04:44 +02:00
// 前缀内容切片、词库切片,前者原封不动写入,后者排序后写入
prefixContents := make([]string, 0) // 前置内容
contents := make([]lemma, 0) // 词库
aSet := mapset.NewSet[string]() // 去重用的的 set
2022-10-30 16:47:40 +01:00
isMark := false
sc := bufio.NewScanner(file)
for sc.Scan() {
line := sc.Text()
2023-04-13 11:04:44 +02:00
2022-10-30 16:47:40 +01:00
if !isMark {
2023-04-13 11:04:44 +02:00
// mark 之前的写入 prefixContents
2022-10-30 16:47:40 +01:00
prefixContents = append(prefixContents, line)
2023-04-13 11:04:44 +02:00
if strings.HasPrefix(line, mark) {
2022-10-30 16:47:40 +01:00
isMark = true
}
continue
}
2023-04-13 11:04:44 +02:00
// 分割为 text code weight
2022-10-30 16:47:40 +01:00
parts := strings.Split(line, "\t")
text, code, weight := parts[0], "", ""
// 检查分割长度
2023-04-13 11:04:44 +02:00
if (_type == 1 || _type == 2 || _type == 3) && len(parts) != _type {
log.Println("分割错误123")
2022-10-30 16:47:40 +01:00
}
2023-04-13 11:04:44 +02:00
if _type == 4 && len(parts) != 2 {
fmt.Println("分割错误4")
2022-10-30 16:47:40 +01:00
}
2023-04-13 11:04:44 +02:00
// 将 base 中注释了但没删除的词汇的权重调为 0
if dictPath == BasePath && strings.HasPrefix(line, "# ") {
2022-10-30 16:47:40 +01:00
parts[2] = "0"
}
// mark 之后的,写入到 contents
2023-04-13 11:04:44 +02:00
// 词库自身有重复内容则直接排除,不重复的写入到 contents
switch _type {
2022-10-30 16:47:40 +01:00
case 1: // 一列 【汉字】
if aSet.Contains(text) {
fmt.Println("重复:", line)
continue
}
aSet.Add(text)
contents = append(contents, lemma{text: text})
case 2: // 两列 【汉字+注音】
text, code = parts[0], parts[1]
if aSet.Contains(text + code) {
fmt.Println("重复:", line)
continue
}
aSet.Add(text + code)
contents = append(contents, lemma{text: text, code: code})
case 3: // 三列 【汉字+注音+权重】
text, code, weight = parts[0], parts[1], parts[2]
if aSet.Contains(text + code) {
fmt.Println("重复:", line)
continue
}
aSet.Add(text + code)
weight, _ := strconv.Atoi(weight)
contents = append(contents, lemma{text: text, code: code, weight: weight})
case 4: // 两列 【汉字+权重】
text, weight = parts[0], parts[1]
if aSet.Contains(text) {
fmt.Println("重复:", line)
continue
}
aSet.Add(text)
weight, _ := strconv.Atoi(weight)
contents = append(contents, lemma{text: text, weight: weight})
}
}
2023-04-13 11:04:44 +02:00
if err := sc.Err(); err != nil {
log.Fatalln(err)
}
2022-10-30 16:47:40 +01:00
2023-04-13 11:04:44 +02:00
// 排序:拼音升序,权重降序,最后直接按 Unicode 编码排序
2024-02-25 12:00:19 +01:00
// 英文排序不区分大小写
if strings.Contains(dictPath, "en.dict.yaml") {
sort.SliceStable(contents, func(i, j int) bool {
2024-02-25 12:00:19 +01:00
textI, textJ := strings.ToLower(contents[i].text), strings.ToLower(contents[j].text)
if strings.HasPrefix(textI, "# ") {
textI = textI[2:]
}
if strings.HasPrefix(textJ, "# ") {
textJ = textJ[2:]
}
if textI != textJ {
return textI < textJ
}
return false
})
} else {
sort.SliceStable(contents, func(i, j int) bool {
2024-02-25 12:00:19 +01:00
if contents[i].code != contents[j].code {
return contents[i].code < contents[j].code
}
if contents[i].weight != contents[j].weight {
return contents[i].weight > contents[j].weight
}
if contents[i].text != contents[j].text {
return contents[i].text < contents[j].text
}
return false
})
}
2022-10-30 16:47:40 +01:00
2023-04-13 11:04:44 +02:00
// 准备写入,清空文件,移动指针到开头
2022-10-30 16:47:40 +01:00
err = file.Truncate(0)
if err != nil {
log.Fatalln(err)
}
_, err = file.Seek(0, 0)
if err != nil {
log.Fatalln(err)
}
// 写入前缀
for _, line := range prefixContents {
_, err := file.WriteString(line + "\n")
if err != nil {
2023-04-13 11:04:44 +02:00
log.Fatalln(err)
2022-10-30 16:47:40 +01:00
}
}
// 字表、base 直接写入,不需要从其他词库去重
if dictPath == HanziPath || dictPath == BasePath {
2022-10-30 16:47:40 +01:00
for _, line := range contents {
_, err := file.WriteString(line.text + "\t" + line.code + "\t" + strconv.Itoa(line.weight) + "\n")
if err != nil {
2023-04-13 11:04:44 +02:00
log.Fatalln(err)
2022-10-30 16:47:40 +01:00
}
}
}
2023-04-13 11:04:44 +02:00
// ext tencent 词库需要从一个或多个词库中去重后再写入
if dictPath == ExtPath || dictPath == TencentPath {
var intersect mapset.Set[string] // 交集,有交集的就是重复的
2022-10-30 16:47:40 +01:00
switch dictPath {
2023-03-04 06:25:37 +01:00
case ExtPath:
2023-04-13 11:04:44 +02:00
intersect = ExtSet.Intersect(BaseSet)
2023-03-04 06:25:37 +01:00
case TencentPath:
2023-04-13 11:04:44 +02:00
intersect = TencentSet.Intersect(BaseSet.Union(ExtSet))
2022-10-30 16:47:40 +01:00
}
for _, line := range contents {
if intersect.Contains(line.text) {
fmt.Printf("%s 重复于其他词库:%s\n", strings.Split(path.Base(dictPath), ".")[0], line.text)
continue
}
2023-06-26 09:39:10 +02:00
var s string
if line.code != "" {
s = line.text + "\t" + line.code + "\t" + strconv.Itoa(line.weight) + "\n"
} else {
s = line.text + "\t" + strconv.Itoa(line.weight) + "\n"
}
2023-04-13 11:04:44 +02:00
_, err := file.WriteString(s)
2022-10-30 16:47:40 +01:00
if err != nil {
2023-04-13 11:04:44 +02:00
log.Fatalln(err)
2022-10-30 16:47:40 +01:00
}
}
}
// 外部词库或临时文件,只排序,不去重
2023-04-13 11:04:44 +02:00
if !contains([]string{HanziPath, BasePath, ExtPath, TencentPath}, dictPath) {
switch _type {
2022-10-30 16:47:40 +01:00
case 1:
for _, line := range contents {
_, err := file.WriteString(line.text + "\n")
if err != nil {
log.Fatalln(err)
}
}
case 2:
for _, line := range contents {
_, err := file.WriteString(line.text + "\t" + line.code + "\n")
if err != nil {
log.Fatalln(err)
}
}
case 3:
for _, line := range contents {
_, err := file.WriteString(line.text + "\t" + line.code + "\t" + strconv.Itoa(line.weight) + "\n")
if err != nil {
log.Fatalln(err)
}
}
case 4:
for _, line := range contents {
_, err := file.WriteString(line.text + "\t" + strconv.Itoa(line.weight) + "\n")
if err != nil {
log.Fatalln(err)
}
}
}
}
2023-04-13 11:04:44 +02:00
if err := file.Sync(); err != nil {
log.Fatalln(err)
}
}
// 获取文件 sha1
func getSha1(dictPath string) string {
f, err := os.Open(dictPath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
sha1Handle := sha1.New()
if _, err := io.Copy(sha1Handle, f); err != nil {
log.Fatal(err)
}
return hex.EncodeToString(sha1Handle.Sum(nil))
}
// 排序后,如果文件有改动,则修改 version 日期,并输出 "...sorted"
func updateVersion(dictPath string, oldSha1 string) {
// 判断文件是否有改变
newSha1 := getSha1(dictPath)
if newSha1 == oldSha1 {
fmt.Println()
return
}
fmt.Println(" ...sorted")
// 打开文件
file, err := os.OpenFile(dictPath, os.O_RDWR, 0644)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// 修改那一行
arr := make([]string, 0)
sc := bufio.NewScanner(file)
for sc.Scan() {
line := sc.Text()
if strings.HasPrefix(line, "version:") {
s := fmt.Sprintf("version: \"%s\"", time.Now().Format("2006-01-02"))
arr = append(arr, s)
} else {
arr = append(arr, line)
}
}
// 重新写入
err = file.Truncate(0)
if err != nil {
log.Fatal(err)
}
_, err = file.Seek(0, 0)
if err != nil {
log.Fatal(err)
}
for _, line := range arr {
_, err := file.WriteString(line + "\n")
if err != nil {
log.Fatal(err)
}
}
2022-10-30 16:47:40 +01:00
err = file.Sync()
if err != nil {
log.Fatal(err)
}
}