识别推文消息中正确的标签索引。

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

Identify the correct hashtag indexes in tweet messages

问题

我需要识别推特消息中的正确索引(包括各种语言、表情符号等)。

我找不到一个能够返回如下示例中所示位置的解决方案。

  1. import (
  2. "regexp"
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestA(t *testing.T) {
  7. text := "🇷🇺 [URGENT] Les forces de dissuasion #nucleaire de la #Russie"
  8. var re = regexp.MustCompile(`#([_A-Za-z0-9]+)`)
  9. pos := re.FindAllStringIndex(text, -1)
  10. // FindAllStringIndex 返回
  11. // [0][43,53]
  12. // [1][60,67]
  13. // 这些是期望的位置。
  14. require.Equal(t, pos[0][0], 37)
  15. require.Equal(t, pos[0][1], 47)
  16. require.Equal(t, pos[1][0], 54)
  17. require.Equal(t, pos[1][1], 61)
  18. }
英文:

I need to identify the correct indexes in twitter messages (various languages, emojis, etc).

I can't find a solution that returns these positions as shown in the example below.

  1. import (
  2. "regexp"
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestA(t *testing.T) {
  7. text := "🇷🇺 [URGENT] Les forces de dissuasion #nucleaire de la #Russie"
  8. var re = regexp.MustCompile(`#([_A-Za-z0-9]+)`)
  9. pos := re.FindAllStringIndex(text, -1)
  10. // FindAllStringIndex returns
  11. // [0][43,53]
  12. // [1][60,67]
  13. // These are the expected positions.
  14. require.Equal(t, pos[0][0], 37)
  15. require.Equal(t, pos[0][1], 47)
  16. require.Equal(t, pos[1][0], 54)
  17. require.Equal(t, pos[1][1], 61)
  18. }

答案1

得分: 2

FindAllStringIndex() 函数返回的是字节的位置,而不是符文的位置。

你需要导入 unicode/utf8 并使用 utf8.RuneCountInString(text[:pos[0][0]]) 等等,而不是使用 pos[0][0],以确保你计算的是 Unicode 代码点而不仅仅是字节:

  1. // 你可以编辑这段代码!
  2. // 点击这里开始输入。
  3. package main
  4. import (
  5. "regexp"
  6. "testing"
  7. "unicode/utf8"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestA(t *testing.T) {
  11. text := "🇷🇺 [URGENT] Les forces de dissuasion #nucleaire de la #Russie"
  12. var re = regexp.MustCompile(`#\w+`)
  13. pos := re.FindAllStringIndex(text, -1)
  14. require.Equal(t, utf8.RuneCountInString(text[:pos[0][0]]), 37)
  15. require.Equal(t, utf8.RuneCountInString(text[:pos[0][1]]), 47)
  16. require.Equal(t, utf8.RuneCountInString(text[:pos[1][0]]), 54)
  17. require.Equal(t, utf8.RuneCountInString(text[:pos[1][1]]), 61)
  18. }

请参考 Go 演示

另外,#\w+ 是一个更短的模式,用于匹配一个 #,然后是一个或多个字母、数字或下划线。

英文:

The FindAllStringIndex() function returns the position of bytes, not runes.

You need to import "unicode/utf8" and use utf8.RuneCountInString(text[:pos[0][0]]) and so on instead of pos[0][0] to make sure you count the Unicode code points and not just bytes:

  1. // You can edit this code!
  2. // Click here and start typing.
  3. package main
  4. import (
  5. "regexp"
  6. "testing"
  7. "unicode/utf8"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestA(t *testing.T) {
  11. text := "🇷🇺 [URGENT] Les forces de dissuasion #nucleaire de la #Russie"
  12. var re = regexp.MustCompile(`#\w+`)
  13. pos := re.FindAllStringIndex(text, -1)
  14. require.Equal(t, utf8.RuneCountInString(text[:pos[0][0]]), 37)
  15. require.Equal(t, utf8.RuneCountInString(text[:pos[0][1]]), 47)
  16. require.Equal(t, utf8.RuneCountInString(text[:pos[1][0]]), 54)
  17. require.Equal(t, utf8.RuneCountInString(text[:pos[1][1]]), 61)
  18. }

See the Go demo.

Also, #\w+ is a a shorter pattern to match a # and then one or more letters, digits or underscores.

huangapple
  • 本文由 发表于 2022年2月28日 22:04:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/71296214.html
匿名

发表评论

匿名网友

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

确定