本文整理汇总了Golang中golang.org/x/text/transform.String函数的典型用法代码示例。如果您正苦于以下问题:Golang String函数的具体用法?Golang String怎么用?Golang String使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了String函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: String
// String is a best-effort attempt to get a UTF-8 encoded version of
// Value. Only MicrosoftUnicode (3,1 ,X), MacRomain (1,0,X) and Unicode platform
// strings are supported.
func (nameEntry *NameEntry) String() string {
if nameEntry.PlatformID == PlatformUnicode || (nameEntry.PlatformID == PlatformMicrosoft &&
nameEntry.EncodingID == PlatformEncodingMicrosoftUnicode) {
decoder := unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewDecoder()
outstr, _, err := transform.String(decoder, string(nameEntry.Value))
if err == nil {
return outstr
}
}
if nameEntry.PlatformID == PlatformMac &&
nameEntry.EncodingID == PlatformEncodingMacRoman {
decoder := charmap.Macintosh.NewDecoder()
outstr, _, err := transform.String(decoder, string(nameEntry.Value))
if err == nil {
return outstr
}
}
return string(nameEntry.Value)
}
示例2: Predict
// Predict takes in a document, predicts the
// class of the document based on the training
// data passed so far, and returns the class
// estimated for the document.
func (b *NaiveBayes) Predict(sentence string) uint8 {
sums := make([]float64, len(b.Count))
sentence, _, _ = transform.String(b.sanitize, sentence)
w := strings.Split(strings.ToLower(sentence), " ")
for _, word := range w {
if _, ok := b.Words[word]; !ok {
continue
}
for i := range sums {
sums[i] += math.Log(float64(b.Words[word].Count[i]+1) / float64(b.Words[word].Seen+b.DictCount))
}
}
for i := range sums {
sums[i] += math.Log(b.Probabilities[i])
}
// find best class
var maxI int
for i := range sums {
if sums[i] > sums[maxI] {
maxI = i
}
}
return uint8(maxI)
}
示例3: funcConvert
func (e *Evaluator) funcConvert(f *ast.FuncConvertExpr) bool {
value := f.Expr.GetValue()
// Casting nil to any type returns nil
if value == nil {
f.SetValue(nil)
return true
}
str, ok := value.(string)
if !ok {
return true
}
if strings.ToLower(f.Charset) == "ascii" {
f.SetValue(value)
return true
} else if strings.ToLower(f.Charset) == "utf8mb4" {
f.SetValue(value)
return true
}
encoding, _ := charset.Lookup(f.Charset)
if encoding == nil {
e.err = ErrInvalidOperation.Gen("unknown encoding: %s", f.Charset)
return false
}
target, _, err := transform.String(encoding.NewDecoder(), str)
if err != nil {
log.Errorf("Convert %s to %s with error: %v", str, f.Charset, err)
e.err = errors.Trace(err)
return false
}
f.SetValue(target)
return true
}
示例4: builtinConvert
// See https://dev.mysql.com/doc/refman/5.7/en/cast-functions.html#function_convert
func builtinConvert(args []types.Datum, _ context.Context) (d types.Datum, err error) {
// Casting nil to any type returns nil
if args[0].Kind() != types.KindString {
return d, nil
}
str := args[0].GetString()
Charset := args[1].GetString()
if strings.ToLower(Charset) == "ascii" {
d.SetString(str)
return d, nil
} else if strings.ToLower(Charset) == "utf8mb4" {
d.SetString(str)
return d, nil
}
encoding, _ := charset.Lookup(Charset)
if encoding == nil {
return d, errors.Errorf("unknown encoding: %s", Charset)
}
target, _, err := transform.String(encoding.NewDecoder(), str)
if err != nil {
log.Errorf("Convert %s to %s with error: %v", str, Charset, err)
return d, errors.Trace(err)
}
d.SetString(target)
return d, nil
}
示例5: TestNonRepertoire
func TestNonRepertoire(t *testing.T) {
testCases := []struct {
init func(e encoding.Encoding) (string, transform.Transformer, error)
e encoding.Encoding
src, want string
}{
{dec, EUCKR, "\xfe\xfe", "\ufffd"},
// {dec, EUCKR, "א", "\ufffd"}, // TODO: why is this different?
{enc, EUCKR, "א", ""},
{enc, EUCKR, "aא", "a"},
{enc, EUCKR, "\uac00א", "\xb0\xa1"},
// TODO: should we also handle Jamo?
}
for _, tc := range testCases {
dir, tr, wantErr := tc.init(tc.e)
dst, _, err := transform.String(tr, tc.src)
if err != wantErr {
t.Errorf("%s %v(%q): got %v; want %v", dir, tc.e, tc.src, err, wantErr)
}
if got := string(dst); got != tc.want {
t.Errorf("%s %v(%q):\ngot %q\nwant %q", dir, tc.e, tc.src, got, tc.want)
}
}
}
示例6: TestNonRepertoire
func TestNonRepertoire(t *testing.T) {
testCases := []struct {
init func(e encoding.Encoding) (string, transform.Transformer, error)
e encoding.Encoding
src, want string
}{
{dec, EUCJP, "\xfe\xfc", "\ufffd"},
{dec, ISO2022JP, "\x1b$B\x7e\x7e", "\ufffd"},
{dec, ShiftJIS, "\xef\xfc", "\ufffd"},
{enc, EUCJP, "갂", ""},
{enc, EUCJP, "a갂", "a"},
{enc, EUCJP, "丌갂", "\x8f\xb0\xa4"},
{enc, ISO2022JP, "갂", ""},
{enc, ISO2022JP, "a갂", "a"},
{enc, ISO2022JP, "朗갂", "\x1b$BzF\x1b(B"}, // switch back to ASCII mode at end
{enc, ShiftJIS, "갂", ""},
{enc, ShiftJIS, "a갂", "a"},
{enc, ShiftJIS, "\u2190갂", "\x81\xa9"},
}
for _, tc := range testCases {
dir, tr, wantErr := tc.init(tc.e)
dst, _, err := transform.String(tr, tc.src)
if err != wantErr {
t.Errorf("%s %v(%q): got %v; want %v", dir, tc.e, tc.src, err, wantErr)
}
if got := string(dst); got != tc.want {
t.Errorf("%s %v(%q):\ngot %q\nwant %q", dir, tc.e, tc.src, got, tc.want)
}
}
}
示例7: String
// String converts the given encoded string to UTF-8. It returns the converted
// string or "", err if any error occurred.
func (d *Decoder) String(s string) (string, error) {
s, _, err := transform.String(d, s)
if err != nil {
return "", err
}
return s, nil
}
示例8: Predict
// Predict takes in a document, predicts the
// class of the document based on the training
// data passed so far, and returns the class
// estimated for the document.
func (b *NaiveBayes) Predict(sentence string) uint8 {
sums := make([]float64, len(b.Count))
sentence, _, _ = transform.String(b.sanitize, sentence)
words := b.Tokenizer.Tokenize(sentence)
for _, word := range words {
w, ok := b.Words.Get(word)
if !ok {
continue
}
for i := range sums {
sums[i] += math.Log(float64(w.Count[i]+1) / float64(w.Seen+b.DictCount))
}
}
for i := range sums {
sums[i] += math.Log(b.Probabilities[i])
}
// find best class
var maxI int
for i := range sums {
if sums[i] > sums[maxI] {
maxI = i
}
}
return uint8(maxI)
}
示例9: Probability
// Probability takes in a small document, returns the
// estimated class of the document based on the model
// as well as the probability that the model is part
// of that class
//
// NOTE: you should only use this for small documents
// because, as discussed in the docs for the model, the
// probability will often times underflow because you
// are multiplying together a bunch of probabilities
// which range on [0,1]. As such, the returned float
// could be NaN, and the predicted class could be
// 0 always.
//
// Basically, use Predict to be robust for larger
// documents. Use Probability only on relatively small
// (MAX of maybe a dozen words - basically just
// sentences and words) documents.
func (b *NaiveBayes) Probability(sentence string) (uint8, float64) {
sums := make([]float64, len(b.Count))
for i := range sums {
sums[i] = 1
}
sentence, _, _ = transform.String(b.sanitize, sentence)
words := b.Tokenizer.Tokenize(sentence)
for _, word := range words {
w, ok := b.Words.Get(word)
if !ok {
continue
}
for i := range sums {
sums[i] *= float64(w.Count[i]+1) / float64(w.Seen+b.DictCount)
}
}
for i := range sums {
sums[i] *= b.Probabilities[i]
}
var denom float64
var maxI int
for i := range sums {
if sums[i] > sums[maxI] {
maxI = i
}
denom += sums[i]
}
return uint8(maxI), sums[maxI] / denom
}
示例10: ToUtf8WithErr
func ToUtf8WithErr(content []byte) (error, string) {
charsetLabel, err := DetectEncoding(content)
if err != nil {
return err, ""
}
if charsetLabel == "utf8" {
return nil, string(content)
}
encoding, _ := charset.Lookup(charsetLabel)
if encoding == nil {
return fmt.Errorf("unknow char decoder %s", charsetLabel), string(content)
}
result, n, err := transform.String(encoding.NewDecoder(), string(content))
// If there is an error, we concatenate the nicely decoded part and the
// original left over. This way we won't loose data.
if err != nil {
result = result + string(content[n:])
}
return err, result
}
示例11: String
// String returns a string with the result of converting s using t. It calls
// Reset on t. It returns the empty string if any error was found. This can only
// happen if an error-producing Transformer is passed to If.
func (t Transformer) String(s string) string {
s, _, err := transform.String(t, s)
if err != nil {
return ""
}
return s
}
示例12: TestNormalize
func TestNormalize(t *testing.T) {
testCases := []struct {
in string
want string
}{
{"hello, world\r\n", "hello, world\n"},
{"hello, world\r", "hello, world\n"},
{"hello, world\n", "hello, world\n"},
{"", ""},
{"\r\n", "\n"},
{"hello,\r\nworld", "hello,\nworld"},
{"hello,\rworld", "hello,\nworld"},
{"hello,\nworld", "hello,\nworld"},
{"hello,\n\rworld", "hello,\n\nworld"},
{"hello,\r\n\r\nworld", "hello,\n\nworld"},
}
n := new(Normalize)
for _, c := range testCases {
got, _, err := transform.String(n, c.in)
if err != nil {
t.Errorf("error transforming %q: %v", c.in, err)
continue
}
if got != c.want {
t.Errorf("transforming %q: got %q, want %q", c.in, got, c.want)
}
}
}
示例13: String
// String returns a string with the result of applying the profile to s.
func (p Profile) String(s string) (string, error) {
s, _, err := transform.String(p.NewTransformer(), s)
if err == nil && p.options.disallowEmpty && len(s) == 0 {
return s, errors.New("enforce resulted in empty string")
}
return s, err
}
示例14: Eval
// Eval implements the Expression Eval interface.
func (f *FunctionConvert) Eval(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
value, err := f.Expr.Eval(ctx, args)
if err != nil {
return nil, err
}
// Casting nil to any type returns nil
if value == nil {
return nil, nil
}
str, ok := value.(string)
if !ok {
return nil, nil
}
if strings.ToLower(f.Charset) == "ascii" {
return value, nil
} else if strings.ToLower(f.Charset) == "utf8mb4" {
return value, nil
}
encoding, _ := Lookup(f.Charset)
if encoding == nil {
return nil, fmt.Errorf("unknown encoding: %s", f.Charset)
}
target, _, err := transform.String(encoding.NewDecoder(), str)
if err != nil {
log.Errorf("Convert %s to %s with error: %v", str, f.Charset, err)
return nil, errors.Trace(err)
}
return target, nil
}
示例15: Probability
// Probability takes in a small document, returns the
// estimated class of the document based on the model
// as well as the probability that the model is part
// of that class
//
// NOTE: you should only use this for small documents
// because, as discussed in the docs for the model, the
// probability will often times underflow because you
// are multiplying together a bunch of probabilities
// which range on [0,1]. As such, the returned float
// could be NaN, and the predicted class could be
// 0 always.
//
// Basically, use Predict to be robust for larger
// documents. Use Probability only on relatively small
// (MAX of maybe a dozen words - basically just
// sentences and words) documents.
func (b *NaiveBayes) Probability(sentence string) (uint8, float64) {
sums := make([]float64, len(b.Count))
for i := range sums {
sums[i] = 1
}
sentence, _, _ = transform.String(b.sanitize, sentence)
w := strings.Split(strings.ToLower(sentence), " ")
for _, word := range w {
if _, ok := b.Words[word]; !ok {
continue
}
for i := range sums {
sums[i] *= float64(b.Words[word].Count[i]+1) / float64(b.Words[word].Seen+b.DictCount)
}
}
for i := range sums {
sums[i] *= b.Probabilities[i]
}
var denom float64
var maxI int
for i := range sums {
if sums[i] > sums[maxI] {
maxI = i
}
denom += sums[i]
}
return uint8(maxI), sums[maxI] / denom
}