redact: 关键发现, 易触发 and 和边界 are words, not Chinese names

xuanwo on X: with the model on, a report's words were masked as names,
关键 in "### 关键发现", 易触 in "`gh pr view` 易触发" and 边界 in
"判断和边界,". Each starts with a surname (关, 易, 边), and zhName took
a cue around it that was too weak alone:

- 发 and 打 after it counted as the verb a person does. They now count
  only when no Han character follows, or one of 了个给过一 (发了个消息,
  打给他); 发现, 打开 begin a word.
- 和 and 与 before it counted as "a person comes next". They join like
  to like, so they now count only when a pronoun or what may be a name
  (a surname and 1-3 characters, no zhWords word) is before them:
  吴敏和郑浩, 我和张伟, not 判断和边界.
- A sentence started after any space or symbol. A space after a word or
  code is inside a sentence, so the start is now after punctuation (with
  spaces after it or none) or at the text's start; a backtick no longer
  counts.

The fixtures are the reporter's literal lines, beside names around the
same cues that still mask (### 张伟发了个消息, 我和张伟,, 这件事。 李娜说过).

Run (mote, mote-run): go test ./internal/redact (TestChineseNames,
TestMaskEval and the rest) ok; gofmt clean. Broken by hand, each red on
its own case: the 和/与 rule off (边界), wordAfter off (关键发现),
sentenceStart always true (易触发).
Running as this is pushed: vet (darwin, windows, linux) of
internal/redact and go test of redact, masking, pii, otlp, sessions,
settings, gui and gateway.
Not tried: the real model on the reporter's whole session; a name that
starts after a space following a Chinese word (这件事 李娜说过) is now
left, as one inside a sentence.
This commit is contained in:
yetone
2026-10-28 14:27:17 +08:00
parent a3ee00c05c
commit 58fb3a7786
3 changed files with 58 additions and 4 deletions
File diff suppressed because one or more lines are too long
+48 -3
View File
@@ -187,6 +187,13 @@ func zhName(s string, i int) (a, b int, ok bool) {
// (建国路), unless a cue ends there
before := s[:i]
role, verb := endsAny(before, zhRoles, true), endsAny(before, zhBefore, false)
// 和 and 与 join like to like: a person after one where a person is
// before it (吴敏和郑浩, 我和张伟), not 判断和边界
for _, j := range []string{"和", "与"} {
if t, ok := strings.CutSuffix(before, j); ok && verb {
verb = personBefore(t)
}
}
start := sentenceStart(before)
if !role && !verb && !start {
return 0, 0, false
@@ -202,7 +209,7 @@ func zhName(s string, i int) (a, b int, ok bool) {
// the longest that ends where something after it says so
for k := len(ends) - 1; k >= 0; k-- {
e := ends[k]
if after := s[e:]; startsAny(after, zhAfter) {
if after := s[e:]; startsAny(after, zhAfter) && !wordAfter(after) {
return i, e, true
} else if (role || verb) && endsWord(after) {
return i, e, true
@@ -257,13 +264,51 @@ func endsWord(s string) bool {
}
// sentenceStart says a name may start after s: s is empty or ends in
// punctuation or a space.
// punctuation, spaces after it or none. A space after a word or code
// (`gh pr view` 易触发) is inside a sentence.
func sentenceStart(s string) bool {
s = strings.TrimRight(s, " \t\u3000")
if s == "" {
return true
}
r, _ := utf8.DecodeLastRuneInString(s)
return unicode.IsPunct(r) || unicode.IsSpace(r) || unicode.IsSymbol(r)
return unicode.IsPunct(r) || unicode.IsSpace(r)
}
// wordAfter says the 发 or 打 that s starts with begins a word (发现,
// 打开), not the verb a person does (发了个消息, 打给他).
func wordAfter(s string) bool {
for _, v := range []string{"发", "打"} {
if t, ok := strings.CutPrefix(s, v); ok {
r, _ := utf8.DecodeRuneInString(t)
return unicode.Is(unicode.Han, r) && !strings.ContainsRune("了个给过一", r)
}
}
return false
}
// personBefore says s ends with a pronoun or what may be a name: a
// surname and one to three characters, no word of zhWords.
func personBefore(s string) bool {
r, _ := utf8.DecodeLastRuneInString(s)
if strings.ContainsRune("我你您他她们", r) {
return true
}
rs := []rune(s)
for n := 2; n <= 4 && n <= len(rs); n++ {
u := string(rs[len(rs)-n:])
if !mayStart[rs[len(rs)-n]] || !hasHan(u) {
continue
}
word := false
for _, w := range zhWords {
word = word || strings.HasPrefix(u, w)
}
if !word {
return true
}
}
return false
}
// zhTrim is the model's name s[a:b] with what it ran on into cut off: at
+9
View File
@@ -61,6 +61,15 @@ func TestChineseNames(t *testing.T) {
{"和平时一样,周五发版。", nil, []string{"周五", "和平"}},
{"方总结一下:说明文档里要写清楚安装步骤。", nil, []string{"方总结", "说明"}},
{"运行测试的时候,内存一直在涨。签名和工具调用都会保留。", nil, []string{"签名和工具"}},
// xuanwo on X: words of a report, masked as 关键, 易触 and 边界
{"### 关键发现:5 组 bot/人类重复 PR", nil, []string{"关键发现"}},
{"`gh pr view` 易触发 GraphQL rate limit", nil, []string{"易触发"}},
{"\n\n先读判断和边界,仅当来源", nil, []string{"和边界,"}},
{"打开文件之前,边界检查失败。", nil, []string{"边界检查"}},
// and names around the same cues still are
{"### 张伟发了个消息", []string{"张伟"}, []string{"发了个消息"}},
{"我和张伟,还有他。", []string{"张伟"}, []string{"我和"}},
{"这件事。 李娜说过。", []string{"李娜"}, []string{"说过"}},
} {
out, _ := Mask(c.s, o)
for _, n := range c.names {