Skip to content

Commit

Permalink
screenNamesパラメータが設定されている場合は本文中のスクリーンネームも消す
Browse files Browse the repository at this point in the history
  • Loading branch information
lufia committed Dec 26, 2023
1 parent 32f37ce commit f2b750a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sabadisambiguator

import (
"net/url"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -141,7 +142,9 @@ func ExtractFeaturesWithOptions(t *twitter2.Tweet, opts ExtractOptions) FeatureV
text := t.Text

fv = append(fv, "BIAS")
if len(opts.ScreenNames) == 0 {
if len(opts.ScreenNames) > 0 {
text = removeScreenNames(text)
} else {
fv = append(fv, "ScreenName:"+t.User.UserName)
fv = append(fv, "inReplyToScreenName:"+inReplyToScreenName(t))
fv = append(fv, "screenNameInQuotedStatus:"+screenNameInQuotedStatus(t))
Expand All @@ -159,3 +162,10 @@ func ExtractFeaturesWithOptions(t *twitter2.Tweet, opts ExtractOptions) FeatureV
fv = append(fv, wordsInUrlPaths(t)...)
return fv
}

// https://help.twitter.com/managing-your-account/x-username-rules
var screenNamePattern = regexp.MustCompile("@[a-zA-Z0-9_]{4,50}[ \t]*")

func removeScreenNames(s string) string {
return screenNamePattern.ReplaceAllLiteralString(s, "")
}
19 changes: 19 additions & 0 deletions lib/feature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sabadisambiguator

import (
"testing"
)

func TestRemoveScreenNames(t *testing.T) {
tests := map[string]string{
"@screen text": "text",
"@screen aaa@screen2": "aaa",
".@screen": ".",
}
for in, want := range tests {
s := removeScreenNames(in)
if s != want {
t.Errorf("removeScreenNames(%q) = %q; want %q", in, s, want)
}
}
}

0 comments on commit f2b750a

Please sign in to comment.