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

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

Identify the correct hashtag indexes in tweet messages

问题

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

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

import (
	"regexp"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestA(t *testing.T) {
	text := "🇷🇺 [URGENT] Les forces de dissuasion #nucleaire de la #Russie"

	var re = regexp.MustCompile(`#([_A-Za-z0-9]+)`)

	pos := re.FindAllStringIndex(text, -1)

	// FindAllStringIndex 返回
	// [0][43,53]
	// [1][60,67]

    // 这些是期望的位置。

	require.Equal(t, pos[0][0], 37) 
	require.Equal(t, pos[0][1], 47)

	require.Equal(t, pos[1][0], 54)
	require.Equal(t, pos[1][1], 61)
}
英文:

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.

import (
	"regexp"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestA(t *testing.T) {
	text := "🇷🇺 [URGENT] Les forces de dissuasion #nucleaire de la #Russie"

	var re = regexp.MustCompile(`#([_A-Za-z0-9]+)`)

	pos := re.FindAllStringIndex(text, -1)

	// FindAllStringIndex returns
	// [0][43,53]
	// [1][60,67]

    // These are the expected positions.

	require.Equal(t, pos[0][0], 37) 
	require.Equal(t, pos[0][1], 47)

	require.Equal(t, pos[1][0], 54)
	require.Equal(t, pos[1][1], 61)
}

答案1

得分: 2

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

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

// 你可以编辑这段代码!
// 点击这里开始输入。
package main

import (
	"regexp"
	"testing"
	"unicode/utf8"

	"github.com/stretchr/testify/require"
)

func TestA(t *testing.T) {
	text := "🇷🇺 [URGENT] Les forces de dissuasion #nucleaire de la #Russie"

	var re = regexp.MustCompile(`#\w+`)

	pos := re.FindAllStringIndex(text, -1)

	require.Equal(t, utf8.RuneCountInString(text[:pos[0][0]]), 37)
	require.Equal(t, utf8.RuneCountInString(text[:pos[0][1]]), 47)
	require.Equal(t, utf8.RuneCountInString(text[:pos[1][0]]), 54)
	require.Equal(t, utf8.RuneCountInString(text[:pos[1][1]]), 61)

}

请参考 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:

// You can edit this code!
// Click here and start typing.
package main

import (
	"regexp"
	"testing"
	"unicode/utf8"

	"github.com/stretchr/testify/require"
)

func TestA(t *testing.T) {
	text := "🇷🇺 [URGENT] Les forces de dissuasion #nucleaire de la #Russie"

	var re = regexp.MustCompile(`#\w+`)

	pos := re.FindAllStringIndex(text, -1)

	require.Equal(t, utf8.RuneCountInString(text[:pos[0][0]]), 37)
	require.Equal(t, utf8.RuneCountInString(text[:pos[0][1]]), 47)
	require.Equal(t, utf8.RuneCountInString(text[:pos[1][0]]), 54)
	require.Equal(t, utf8.RuneCountInString(text[:pos[1][1]]), 61)

}

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:

确定