基本数据

    a := []Text{
        {Text: "白如依山尽", PinYin: "bai ri yi shan jin"},
        {Text: ","},
        {Text: "黄河入海流", PinYin: "huang he ru hai liu"},
        {Text: "."},
    }
  • 第一步先拼接文字
const (
    text1 = "白日依山尽,黄河入海流."
)
  • 第二步拆分成单字结构
    a := []Text{
        {Text: "白", PinYin: "bai"},
        {Text: "日", PinYin: "ri"},
        {Text: "依", PinYin: "yi"},
        {Text: "山", PinYin: "shan"},
        {Text: "尽", PinYin: "jin"},
        {Text: ","},
        {Text: "黄", PinYin: "huang", Fill: true},
        {Text: "河", PinYin: "he", Fill: true},
        {Text: "入", PinYin: "ru"},
        {Text: "海", PinYin: "hai"},
        {Text: "流", PinYin: "liu"},
        {Text: "."},
    }
  • 修改文字
const (
    text1 = "白日依山尽,黄河入海流."   // 隐藏后内容: 白日依山尽,__入海流.
    text2 = "白日依山尽,黄河长江入海流." // 编辑后内容: 白日依山尽,__长江入海流.
)

在原拼音不变的情况下,获取最新的拼音信息.

以下是个粗略的版本,有时间可以继续优化

type Text struct {
    Text   string
    PinYin string
    Fill   bool `json:"fill"`
}

func main() {
    a := []Text{
        {Text: "白", PinYin: "bai"},
        {Text: "日", PinYin: "ri"},
        {Text: "依", PinYin: "yi"},
        {Text: "山", PinYin: "shan"},
        {Text: "尽", PinYin: "jin"},
        {Text: ","},
        {Text: "黄", PinYin: "huang", Fill: true},
        {Text: "河", PinYin: "he", Fill: true},
        {Text: "入", PinYin: "ru"},
        {Text: "海", PinYin: "hai"},
        {Text: "流", PinYin: "liu"},
        {Text: "."},
    }
    var aPoint int
    dmp := diffmatchpatch.New()

    b := make([]Text, 0, 10)
    diffs := dmp.DiffMain(text1, text2, false)
    var buf bytes.Buffer
    var char Text
    for _, diff := range diffs {
        switch diff.Type {
        case diffmatchpatch.DiffInsert:
            textRune := []rune(diff.Text)
            for _, char := range textRune {
                if !unicode.Is(unicode.Scripts["Han"], char) {
                    if buf.Len() > 0 {
                        b = append(b, Text{
                            Text:   buf.String(),
                            PinYin: "##",
                        })
                        buf.Reset()
                    }
                    b = append(b, Text{
                        Text: string(char),
                    })
                    continue
                }
                buf.WriteRune(char)
            }
            if buf.Len() > 0 {
                b = append(b, Text{
                    Text:   buf.String(),
                    PinYin: "##",
                })
                buf.Reset()
            }
        case diffmatchpatch.DiffDelete:
            aPoint += utf8.RuneCountInString(diff.Text)
        case diffmatchpatch.DiffEqual:
            var text string
            var py []string
            var fill bool
            for i := 0; i < utf8.RuneCountInString(diff.Text); i++ {
                char = a[aPoint]
                if char.Fill != fill { // 执行填空操作
                    b = append(b, Text{
                        Text:   text,
                        PinYin: strings.Join(py, " "),
                        Fill:   fill,
                    })
                    text = ""
                    py = make([]string, 0, 10)
                    fill = char.Fill
                }
                text += char.Text
                py = append(py, char.PinYin)
                aPoint++
            }
            b = append(b, Text{
                Text:   text,
                PinYin: strings.Join(py, " "),
                Fill:   fill,
            })
        }
    }
    data, _ := json.MarshalIndent(&b, "", "  ")
}

打印结果

[
  {
    "Text": "白日依山尽,",
    "PinYin": "bai ri yi shan jin ",
    "fill": false
  },
  {
    "Text": "黄河",
    "PinYin": "huang he",
    "fill": true
  },
  {
    "Text": "长江",
    "PinYin": "##",
    "fill": false
  },
  {
    "Text": "入海流.",
    "PinYin": "ru hai liu ",
    "fill": false
  }
]

二十一
1.4k 声望867 粉丝

无论遇到多大的困难,你总是能扛过去,坚持一件事,对自己


引用和评论

0 条评论