英文:
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))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论