如何按单词获取文本之间的差异?

huangapple go评论81阅读模式
英文:

How to get the difference in texts by words?

问题

我想使用库https://github.com/sergi/go-diff

package main

import (
	"fmt"
	"github.com/sergi/go-diff/diffmatchpatch"
)

func main() {
	dmp := diffmatchpatch.New()
	text1 := "some text"
	text2 := "other text"
	diffs := dmp.DiffMain(text1, text2, true)
	fmt.Println(diffs)

}

输出结果:

[{Delete s} {Equal o} {Delete m} {Insert th} {Equal e} {Insert r} {Equal  text}]

我想要的结果是:

[{Delete some}  {Insert other} {Equal  text}]

这个可能吗?

P.S. 对不起,我的英语不好。

英文:

I want to use library https://github.com/sergi/go-diff

package main

import (
	"fmt"
	"github.com/sergi/go-diff/diffmatchpatch"
)

func main() {
	dmp := diffmatchpatch.New()
	text1 := "some text"
	text2 := "other text"
	diffs := dmp.DiffMain(text1, text2, true)
	fmt.Println(diffs)

}

output:

[{Delete s} {Equal o} {Delete m} {Insert th} {Equal e} {Insert r} {Equal  text}]

i wont:

[{Delete some}  {Insert other} {Equal  text}]

Is it possible?

P.S. Sorry for my English.

答案1

得分: 1

请检查以下代码:

package main

import (
	"fmt"

	"github.com/sergi/go-diff/diffmatchpatch"
)

func main() {
	dmp := diffmatchpatch.New()
	text1 := "some text"
	text2 := "other text"

	diffs := dmp.DiffMain(text1, text2, true)
	fmt.Println(dmp.DiffCleanupSemantic(diffs))
}

这段代码使用了 github.com/sergi/go-diff/diffmatchpatch 包来比较两个文本的差异,并进行语义化的差异清理。

英文:

Check this

package main

import (
    "fmt"

    "github.com/sergi/go-diff/diffmatchpatch"
)

func main() {
    dmp := diffmatchpatch.New()
    text1 := "some text"
    text2 := "other text"

    diffs := dmp.DiffMain(text1, text2, true)
    fmt.Println(dmp.DiffCleanupSemantic(diffs))
}

huangapple
  • 本文由 发表于 2021年7月1日 23:45:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/68213242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定