使用Go删除变音符号 [英] Remove diacritics using Go

查看:128
本文介绍了使用Go删除变音符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Go从给定的UTF8编码字符串中删除所有变音符号?例如转换字符串žůžo => zuzo。有没有一种标准的方式?

您可以使用 Go中的文本规范化



以下是这些库的应用程序:

  //示例源自:http://blog.golang.org/normalization 

包主

导入(
fmt
unicode

golang.org/x/text/transform
golang.org/x/text/unicode / norm


func isMn(r rune)bool {
return unicode.Is(unicode.Mn,r)// Mn:nonspacing marks
}

func main(){
t:= transform.Chain(norm.NFD,transform.RemoveFunc(isMn),norm.NFC)
result,_,_:= transform .String(t,žůžo)
fmt.Println(结果)
}


How can I remove all diacritics from the given UTF8 encoded string using Go? e.g. transform the string "žůžo" => "zuzo". Is there a standard way?

解决方案

You can use the libraries described in Text normalization in Go.

Here's an application of those libraries:

// Example derived from: http://blog.golang.org/normalization

package main

import (
    "fmt"
    "unicode"

    "golang.org/x/text/transform"
    "golang.org/x/text/unicode/norm"
)

func isMn(r rune) bool {
    return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}

func main() {
    t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
    result, _, _ := transform.String(t, "žůžo")
    fmt.Println(result)
}

这篇关于使用Go删除变音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆