redact: an app's one-word name the model took for a person's goes as written
Reported by ylorn. on Discord (11-06): choosing apps in Alma, a question
about one app was answered with another's facts. Privacy Filter took the
apps' names (Cicada, Mimi, Phosphor, Parakeet, Muesli) for people's: in a
bare list of them, alone, and in the Markdown list of apps the assistant
answered with (probed on the real model: ~0.99 each). The follow-up
questions ("How about Mimi?", "Phosphor vs Parakeet") it did not flag, so
they went as written while the history held the names as placeholders;
Phosphor and Parakeet (8 bytes) then became known values and were masked
everywhere. The vendor could not tell which placeholder was which app.
Restore was not the cause: placeholders are a keyed hash of kind and value,
one per value, and restore maps each back to its own value in any case
(TestManyNamesRestoreEachToItself, a guard: it passes on the old code too).
personFits now lets one ASCII word go when what is around it says a thing
and nothing says a person (thingNamed, internal/redact/thingname.go): a
person's word beside it (my colleague, Hi, Mr, said, told) masks; a thing's
word beside it (using, switched to, the ... app, vs, apps like, 这个软件)
lets go; else its sentence decides (he/she/relations/greetings/said mask;
app/macOS/subscription/vs/软件 let go); else a list item among items that
speak of apps, or a text of capitalized names only, lets go.
Trade-off, by design: a single first name alone, or a first name in a
sentence that speaks of an app with nothing of a person ("Mimi uses the
app daily"), goes as written. A full name (John Smith), a Chinese name
(张伟), and a first name beside a relation, a greeting, said or a pronoun
are masked as before.
Run (mote, worktree at 84ad780b6 + this):
- go test ./internal/redact/... ./internal/pii/... ./internal/masking/...
./internal/otlp/... ./internal/sessions/... ./internal/settings/... ok
- go test -run 'Mask|Redact|Privacy|Person|Known' ./internal/gateway
./internal/gui ok
- TestMaskEval re-recorded with both real models (MAGPIE_EVAL_CACHE);
only the 12 new cases' recordings are added, old entries kept as they
were (mote's re-record drifted 4 AstrLink and 2 Privacy Filter entries
by score; not taken in). New cases app-names, app-alone, app-markdown
(real Privacy Filter flags), app-compare/vs/list/using/zh (model finds
none; guard), app-and-person, app-and-full-name, app-and-zh-name,
greeting (person controls).
- gofmt -l clean; GOOS=linux and GOOS=windows go vet ./internal/redact ok.
Broken by hand:
- the personFits hook off: TestAppNamesNotPersons red 9/9,
TestPersonBesideAppStillMasked red on the 3 apps, TestMaskEval red on
app-names (5), app-alone, app-markdown (3) with the real recordings.
- thingNamed always true: TestPersonBesideAppStillMasked red on Mimi (x2),
Laura, Daniel.
Not tried: a real Alma chat through the gateway with the model on; Windows
and Linux runs (pure Go string code, vetted for both); the full suite runs
after the push.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -306,6 +306,66 @@ var evalCases = []evalCase{
|
||||
Text: "- MSRV check. The repo's minimum rust-version is 1.78. Pinning to `nightly-2026-09-01` (about 7 weeks old) is plausible.\n- Risk: the pinned toolchain lacks fixes.",
|
||||
Keep: []evalValue{{V: "nightly-2026-09-01", Kind: "PERSON"}},
|
||||
},
|
||||
// ylorn. on Discord, choosing apps in Alma: the model took the apps'
|
||||
// names for people's, and the vendor answered about the wrong app
|
||||
{
|
||||
ID: "app-names", From: "ylorn.",
|
||||
Text: "Cicada, Mimi, Phosphor, Parakeet, Muesli",
|
||||
Keep: []evalValue{{V: "Cicada", Kind: "PERSON"}, {V: "Mimi", Kind: "PERSON"}, {V: "Phosphor", Kind: "PERSON"}, {V: "Parakeet", Kind: "PERSON"}, {V: "Muesli", Kind: "PERSON"}},
|
||||
},
|
||||
{
|
||||
ID: "app-alone", From: "ylorn.",
|
||||
Text: "Muesli",
|
||||
Keep: []evalValue{{V: "Muesli", Kind: "PERSON"}},
|
||||
},
|
||||
{
|
||||
ID: "app-markdown", From: "ylorn.",
|
||||
Text: "1. **Cicada** – a minimal note-taking app with Markdown.\n2. **Mimi** – a voice memo app with AI summaries.\n3. **Phosphor** – an RSS reader.",
|
||||
Keep: []evalValue{{V: "Cicada", Kind: "PERSON"}, {V: "Mimi", Kind: "PERSON"}, {V: "Phosphor", Kind: "PERSON"}},
|
||||
},
|
||||
{
|
||||
ID: "app-compare", From: "ylorn.",
|
||||
Text: "Compare Cicada and Mimi for note-taking on macOS. Which one syncs better with iCloud?",
|
||||
Keep: []evalValue{{V: "Cicada", Kind: "PERSON"}, {V: "Mimi", Kind: "PERSON"}},
|
||||
},
|
||||
{
|
||||
ID: "app-vs", From: "ylorn.",
|
||||
Text: "Phosphor vs Parakeet: which is the better dictation app?",
|
||||
Keep: []evalValue{{V: "Phosphor", Kind: "PERSON"}, {V: "Parakeet", Kind: "PERSON"}},
|
||||
},
|
||||
{
|
||||
ID: "app-list", From: "ylorn.",
|
||||
Text: "I'm choosing a transcription app. So far: Muesli, Parakeet and Mimi. Muesli is free, Parakeet has a one-time price, Mimi is a subscription.",
|
||||
Keep: []evalValue{{V: "Muesli", Kind: "PERSON"}, {V: "Parakeet", Kind: "PERSON"}, {V: "Mimi", Kind: "PERSON"}},
|
||||
},
|
||||
{
|
||||
ID: "app-using", From: "ylorn.",
|
||||
Text: "I've been using Phosphor for a week, and yesterday I switched to Muesli. Is Cicada worth trying too?",
|
||||
Keep: []evalValue{{V: "Phosphor", Kind: "PERSON"}, {V: "Muesli", Kind: "PERSON"}, {V: "Cicada", Kind: "PERSON"}},
|
||||
},
|
||||
{
|
||||
ID: "app-zh", From: "ylorn.",
|
||||
Text: "帮我对比一下 Cicada 和 Mimi 这两个 app,哪个更适合记笔记?Phosphor 和 Parakeet 呢?",
|
||||
Keep: []evalValue{{V: "Cicada", Kind: "PERSON"}, {V: "Mimi", Kind: "PERSON"}, {V: "Phosphor", Kind: "PERSON"}, {V: "Parakeet", Kind: "PERSON"}},
|
||||
},
|
||||
{
|
||||
ID: "app-and-person", From: "ylorn.",
|
||||
Text: "My colleague Mimi said the Phosphor app is great; she uses it every day.",
|
||||
Mask: []evalValue{{V: "Mimi", Kind: "PERSON"}},
|
||||
Keep: []evalValue{{V: "Phosphor", Kind: "PERSON"}},
|
||||
},
|
||||
{
|
||||
ID: "app-and-full-name", From: "ylorn.",
|
||||
Text: "John Smith asked me to install Cicada on his Mac.",
|
||||
Mask: []evalValue{{V: "John Smith", Kind: "PERSON"}},
|
||||
Keep: []evalValue{{V: "Cicada", Kind: "PERSON"}},
|
||||
},
|
||||
{
|
||||
ID: "app-and-zh-name", From: "ylorn.",
|
||||
Text: "张伟说 Parakeet 这个软件很好用,让我也装一个。",
|
||||
Mask: []evalValue{{V: "张伟", Kind: "PERSON"}},
|
||||
Keep: []evalValue{{V: "Parakeet", Kind: "PERSON"}},
|
||||
},
|
||||
|
||||
// ---- what masking is for --------------------------------------------
|
||||
{
|
||||
@@ -348,6 +408,11 @@ var evalCases = []evalCase{
|
||||
Text: "If the build breaks overnight, page Tom on +1 415-555-0132.",
|
||||
Mask: []evalValue{{V: "415-555-0132", Kind: "PHONE"}, {V: "Tom", Kind: "PERSON"}},
|
||||
},
|
||||
{
|
||||
ID: "greeting", From: "true",
|
||||
Text: "Hi Mimi, thanks for the notes from Tuesday's call. Can you send them to Laura as well?",
|
||||
Mask: []evalValue{{V: "Mimi", Kind: "PERSON"}, {V: "Laura", Kind: "PERSON"}},
|
||||
},
|
||||
{
|
||||
ID: "card", From: "true",
|
||||
Text: "The customer's card number is 4111 1111 1111 1111, expiry 12/29.",
|
||||
|
||||
@@ -761,6 +761,11 @@ func personFits(s string, a, b int, v string) bool {
|
||||
if w := strings.Fields(v); len(w) == 1 && (strings.Trim(v, "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == "" || camelProduct(v) || hostIn(s, v)) {
|
||||
return false
|
||||
}
|
||||
// an app's or a product's name the model took for a first name, where
|
||||
// its sentence speaks of a thing (thingname.go)
|
||||
if w := strings.Fields(v); len(w) == 1 && isASCII(v) && thingNamed(s, a, b) {
|
||||
return false
|
||||
}
|
||||
for i, w := range strings.Fields(v) {
|
||||
if c := w[0]; c >= 'a' && c <= 'z' && (i == 0 || !nameParticles[w]) {
|
||||
return false
|
||||
|
||||
+101
@@ -1,4 +1,76 @@
|
||||
{
|
||||
"app-alone": {
|
||||
"text": "2738400ea67f70e7",
|
||||
"found": []
|
||||
},
|
||||
"app-and-full-name": {
|
||||
"text": "eb8393291d133f45",
|
||||
"found": [
|
||||
{
|
||||
"Start": 0,
|
||||
"End": 10,
|
||||
"Label": "private_person",
|
||||
"score": 0.9936622977256775
|
||||
}
|
||||
]
|
||||
},
|
||||
"app-and-person": {
|
||||
"text": "8cabfaec106728f1",
|
||||
"found": [
|
||||
{
|
||||
"Start": 13,
|
||||
"End": 17,
|
||||
"Label": "private_person",
|
||||
"score": 0.9946044087409973
|
||||
}
|
||||
]
|
||||
},
|
||||
"app-and-zh-name": {
|
||||
"text": "92b091e30f0a0c15",
|
||||
"found": [
|
||||
{
|
||||
"Start": 0,
|
||||
"End": 6,
|
||||
"Label": "private_person",
|
||||
"score": 0.9895697832107544
|
||||
}
|
||||
]
|
||||
},
|
||||
"app-compare": {
|
||||
"text": "47950e5d65bd8e32",
|
||||
"found": []
|
||||
},
|
||||
"app-list": {
|
||||
"text": "3868c5f8307023c8",
|
||||
"found": []
|
||||
},
|
||||
"app-markdown": {
|
||||
"text": "1c4040fd04fdb4c4",
|
||||
"found": []
|
||||
},
|
||||
"app-names": {
|
||||
"text": "24fb4674fdbd13a1",
|
||||
"found": []
|
||||
},
|
||||
"app-using": {
|
||||
"text": "47bf0d9e34732f20",
|
||||
"found": [
|
||||
{
|
||||
"Start": 76,
|
||||
"End": 82,
|
||||
"Label": "private_person",
|
||||
"score": 0.7031570672988892
|
||||
}
|
||||
]
|
||||
},
|
||||
"app-vs": {
|
||||
"text": "27b7c37b61a4eb7f",
|
||||
"found": []
|
||||
},
|
||||
"app-zh": {
|
||||
"text": "a1179923cd48f669",
|
||||
"found": []
|
||||
},
|
||||
"asset-files": {
|
||||
"text": "8b019ca5e2af716b",
|
||||
"found": []
|
||||
@@ -237,6 +309,35 @@
|
||||
"text": "e855cf322da328a9",
|
||||
"found": []
|
||||
},
|
||||
"greeting": {
|
||||
"text": "06eb9e21227a519d",
|
||||
"found": [
|
||||
{
|
||||
"Start": 0,
|
||||
"End": 2,
|
||||
"Label": "private_person",
|
||||
"score": 0.8000637292861938
|
||||
},
|
||||
{
|
||||
"Start": 3,
|
||||
"End": 7,
|
||||
"Label": "private_person",
|
||||
"score": 0.9931860566139221
|
||||
},
|
||||
{
|
||||
"Start": 35,
|
||||
"End": 44,
|
||||
"Label": "private_date",
|
||||
"score": 0.6142452359199524
|
||||
},
|
||||
{
|
||||
"Start": 72,
|
||||
"End": 77,
|
||||
"Label": "private_person",
|
||||
"score": 0.9937676191329956
|
||||
}
|
||||
]
|
||||
},
|
||||
"grep-alts": {
|
||||
"text": "dab9d9ae2f7b6d01",
|
||||
"found": []
|
||||
|
||||
+132
@@ -1,4 +1,119 @@
|
||||
{
|
||||
"app-alone": {
|
||||
"text": "2738400ea67f70e7",
|
||||
"found": [
|
||||
{
|
||||
"Start": 0,
|
||||
"End": 6,
|
||||
"Label": "private_person",
|
||||
"score": 0.9992566704750061
|
||||
}
|
||||
]
|
||||
},
|
||||
"app-and-full-name": {
|
||||
"text": "eb8393291d133f45",
|
||||
"found": [
|
||||
{
|
||||
"Start": 0,
|
||||
"End": 10,
|
||||
"Label": "private_person",
|
||||
"score": 0.999984860420227
|
||||
}
|
||||
]
|
||||
},
|
||||
"app-and-person": {
|
||||
"text": "8cabfaec106728f1",
|
||||
"found": [
|
||||
{
|
||||
"Start": 13,
|
||||
"End": 17,
|
||||
"Label": "private_person",
|
||||
"score": 0.999997615814209
|
||||
}
|
||||
]
|
||||
},
|
||||
"app-and-zh-name": {
|
||||
"text": "92b091e30f0a0c15",
|
||||
"found": []
|
||||
},
|
||||
"app-compare": {
|
||||
"text": "47950e5d65bd8e32",
|
||||
"found": []
|
||||
},
|
||||
"app-list": {
|
||||
"text": "3868c5f8307023c8",
|
||||
"found": []
|
||||
},
|
||||
"app-markdown": {
|
||||
"text": "1c4040fd04fdb4c4",
|
||||
"found": [
|
||||
{
|
||||
"Start": 5,
|
||||
"End": 11,
|
||||
"Label": "private_person",
|
||||
"score": 0.9992601275444031
|
||||
},
|
||||
{
|
||||
"Start": 64,
|
||||
"End": 68,
|
||||
"Label": "private_person",
|
||||
"score": 0.9993537068367004
|
||||
},
|
||||
{
|
||||
"Start": 116,
|
||||
"End": 124,
|
||||
"Label": "private_person",
|
||||
"score": 0.9996298551559448
|
||||
}
|
||||
]
|
||||
},
|
||||
"app-names": {
|
||||
"text": "24fb4674fdbd13a1",
|
||||
"found": [
|
||||
{
|
||||
"Start": 0,
|
||||
"End": 6,
|
||||
"Label": "private_person",
|
||||
"score": 0.9937587976455688
|
||||
},
|
||||
{
|
||||
"Start": 8,
|
||||
"End": 12,
|
||||
"Label": "private_person",
|
||||
"score": 0.9983303546905518
|
||||
},
|
||||
{
|
||||
"Start": 14,
|
||||
"End": 22,
|
||||
"Label": "private_person",
|
||||
"score": 0.9998930096626282
|
||||
},
|
||||
{
|
||||
"Start": 24,
|
||||
"End": 32,
|
||||
"Label": "private_person",
|
||||
"score": 0.9995993375778198
|
||||
},
|
||||
{
|
||||
"Start": 34,
|
||||
"End": 40,
|
||||
"Label": "private_person",
|
||||
"score": 0.9990513324737549
|
||||
}
|
||||
]
|
||||
},
|
||||
"app-using": {
|
||||
"text": "47bf0d9e34732f20",
|
||||
"found": []
|
||||
},
|
||||
"app-vs": {
|
||||
"text": "27b7c37b61a4eb7f",
|
||||
"found": []
|
||||
},
|
||||
"app-zh": {
|
||||
"text": "a1179923cd48f669",
|
||||
"found": []
|
||||
},
|
||||
"asset-files": {
|
||||
"text": "8b019ca5e2af716b",
|
||||
"found": []
|
||||
@@ -312,6 +427,23 @@
|
||||
"text": "e855cf322da328a9",
|
||||
"found": []
|
||||
},
|
||||
"greeting": {
|
||||
"text": "06eb9e21227a519d",
|
||||
"found": [
|
||||
{
|
||||
"Start": 3,
|
||||
"End": 7,
|
||||
"Label": "private_person",
|
||||
"score": 0.9999867081642151
|
||||
},
|
||||
{
|
||||
"Start": 72,
|
||||
"End": 77,
|
||||
"Label": "private_person",
|
||||
"score": 0.9999018907546997
|
||||
}
|
||||
]
|
||||
},
|
||||
"grep-alts": {
|
||||
"text": "dab9d9ae2f7b6d01",
|
||||
"found": []
|
||||
|
||||
@@ -0,0 +1,288 @@
|
||||
package redact
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// An app's or a product's name is often a word, capitalized: Cicada,
|
||||
// Mimi, Phosphor, Parakeet, Muesli. Privacy Filter took each for a
|
||||
// person's name where nothing was around it, in a list of them or alone,
|
||||
// and in a Markdown list of apps with a line on each ("1. **Cicada** – a
|
||||
// minimal note-taking app"), and the vendor answered a chat about
|
||||
// choosing apps with another app's facts: the history had the names as
|
||||
// placeholders, the question that followed had them as written, and
|
||||
// nothing said which placeholder was which app (ylorn. on Discord, 11-06).
|
||||
//
|
||||
// One word the model took for a name goes as written where what is
|
||||
// around it says a thing is meant, and nothing says a person is:
|
||||
// - the word before or after it says so: a person's (my colleague Mimi,
|
||||
// Hi Mimi, Mimi said, 张伟先生) or a thing's (using Phosphor, switched
|
||||
// to Muesli, the Phosphor app, Parakeet 这个软件);
|
||||
// - else its sentence: a pronoun, a relation or a title, a word of
|
||||
// speech or a greeting say a person, and the name is masked; an app's,
|
||||
// a tool's or a system's word, a price or a comparison say a thing;
|
||||
// - else a text of nothing but such words (Cicada, Mimi, Phosphor) is a
|
||||
// list of names with nothing to say whose, and goes as written.
|
||||
//
|
||||
// What this lets go: a first name alone, in a sentence that names an app
|
||||
// and has nothing of a person in it ("Mimi uses the app daily"), and a
|
||||
// text that is only first names. A name of two words or more (John
|
||||
// Smith), one in a sentence with he, she, said, a greeting or a relation
|
||||
// in it, and every Chinese name are masked as before.
|
||||
|
||||
// thingNamed says the one word at s[a:b] that the model took for a
|
||||
// person's name names a thing there.
|
||||
func thingNamed(s string, a, b int) bool {
|
||||
cs, ce := clauseAround(s, a, b)
|
||||
prev, prev2 := wordsBefore(s[cs:a])
|
||||
next := latinAfter(s[b:ce])
|
||||
switch {
|
||||
case (prev == "like" || prev == "called" || prev == "named") && thingKinds[prev2]:
|
||||
return true
|
||||
case nameBefore[prev] || nameAfter[next] || endsAny(strings.TrimRight(s[cs:a], " "), zhRoles, true) || startsAny(strings.TrimLeft(s[b:ce], " "), zhTitles):
|
||||
return false
|
||||
case thingBefore[prev] || (prev == "to" || prev == "from" || prev == "with" || prev == "over") && thingVerbs[prev2] || thingAfter[next] || startsAny(strings.TrimLeft(s[b:ce], " "), zhThingAfter):
|
||||
return true
|
||||
}
|
||||
clause := strings.ToLower(s[cs:a] + " " + s[b:ce])
|
||||
if hasWord(clause, personWords) || containsAny(clause, zhPersonWords) {
|
||||
return false
|
||||
}
|
||||
if hasWord(clause, thingWords) || containsAny(clause, zhThingWords) {
|
||||
return true
|
||||
}
|
||||
return listOfThings(s, a) || namesOnly(s)
|
||||
}
|
||||
|
||||
// listOfThings says the name at s[a:] starts an item of a list whose
|
||||
// other items speak of things: "3. **Phosphor** – an RSS reader" under
|
||||
// "1. **Cicada** – a minimal note-taking app".
|
||||
func listOfThings(s string, a int) bool {
|
||||
ls := strings.LastIndexByte(s[:a], '\n') + 1
|
||||
if !listItem(s[ls:a]) {
|
||||
return false
|
||||
}
|
||||
for start := 0; start < len(s); {
|
||||
end := strings.IndexByte(s[start:], '\n')
|
||||
if end < 0 {
|
||||
end = len(s)
|
||||
} else {
|
||||
end += start
|
||||
}
|
||||
if start != ls {
|
||||
t := strings.TrimLeft(s[start:end], " \t")
|
||||
if i := listMarker(t); i > 0 {
|
||||
l := strings.ToLower(t[i:])
|
||||
if hasWord(l, thingWords) && !hasWord(l, personWords) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
start = end + 1
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// listItem says before, the start of a line up to a name, is a list's
|
||||
// marker and markup only: "1. **", "- ", "* ".
|
||||
func listItem(before string) bool {
|
||||
t := strings.TrimLeft(before, " \t")
|
||||
i := listMarker(t)
|
||||
return i > 0 && strings.Trim(t[i:], " *_`") == ""
|
||||
}
|
||||
|
||||
// listMarker is the length of the list marker t starts with and the
|
||||
// space after it (1. , 2) , - , * , • ), or 0.
|
||||
func listMarker(t string) int {
|
||||
i := 0
|
||||
for i < len(t) && t[i] >= '0' && t[i] <= '9' {
|
||||
i++
|
||||
}
|
||||
switch {
|
||||
case i > 0 && i < len(t) && (t[i] == '.' || t[i] == ')'):
|
||||
i++
|
||||
case i == 0 && (strings.HasPrefix(t, "- ") || strings.HasPrefix(t, "* ")):
|
||||
i = 1
|
||||
case i == 0 && strings.HasPrefix(t, "• "):
|
||||
i = len("•")
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
if i < len(t) && t[i] == ' ' {
|
||||
return i + 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// clauseAround is the sentence s[a:b] is in: from the end of the one
|
||||
// before (a full stop and a space, ?, !, ;, a line's end, 。!?;) to the
|
||||
// end of its own.
|
||||
func clauseAround(s string, a, b int) (int, int) {
|
||||
cs := 0
|
||||
for i := a - 1; i >= 0; i-- {
|
||||
if c := s[i]; c == '\n' || c == '?' || c == '!' || c == ';' || c == '.' && i+1 < len(s) && s[i+1] == ' ' {
|
||||
cs = i + 1
|
||||
break
|
||||
}
|
||||
if r, n := utf8.DecodeLastRuneInString(s[:i+1]); n > 1 && strings.ContainsRune("。!?;", r) {
|
||||
cs = i + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
ce := len(s)
|
||||
for i := b; i < len(s); i++ {
|
||||
if c := s[i]; c == '\n' || c == '?' || c == '!' || c == ';' || c == '.' && (i+1 == len(s) || s[i+1] == ' ') {
|
||||
ce = i
|
||||
break
|
||||
}
|
||||
if r, n := utf8.DecodeRuneInString(s[i:]); n > 1 && strings.ContainsRune("。!?;", r) {
|
||||
ce = i
|
||||
break
|
||||
}
|
||||
}
|
||||
return cs, ce
|
||||
}
|
||||
|
||||
// latinWords are the Latin words of s in lower case, in order.
|
||||
func latinWords(s string) []string {
|
||||
return strings.FieldsFunc(strings.ToLower(s), func(r rune) bool {
|
||||
return r >= utf8.RuneSelf || !(r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '-' || r == '\'')
|
||||
})
|
||||
}
|
||||
|
||||
// wordsBefore are the last word of before and the one before it, leaving
|
||||
// out an article (the Phosphor app, install the Cicada beta). A Chinese
|
||||
// letter or anything but spaces and markup right before the name ends
|
||||
// them: "说 Parakeet" has no Latin word before it.
|
||||
func wordsBefore(before string) (string, string) {
|
||||
t := strings.TrimRight(before, " \t*_`\"'“(")
|
||||
if t == "" {
|
||||
return "", ""
|
||||
}
|
||||
if r, _ := utf8.DecodeLastRuneInString(t); !(r < utf8.RuneSelf && (unicode.IsLetter(r) || unicode.IsDigit(r))) {
|
||||
return "", ""
|
||||
}
|
||||
ws := latinWords(t)
|
||||
for len(ws) > 0 && articles[ws[len(ws)-1]] {
|
||||
ws = ws[:len(ws)-1]
|
||||
}
|
||||
switch len(ws) {
|
||||
case 0:
|
||||
return "", ""
|
||||
case 1:
|
||||
return ws[0], ""
|
||||
}
|
||||
return ws[len(ws)-1], ws[len(ws)-2]
|
||||
}
|
||||
|
||||
// latinAfter is the first word of after, past spaces and markup: app in
|
||||
// "Phosphor app", said in "Mimi said". A comma or a dash between says
|
||||
// nothing of the name ("Cicada, Mimi" are two of a list).
|
||||
func latinAfter(after string) string {
|
||||
t := strings.TrimLeft(after, " \t*_`\"'”)")
|
||||
if strings.HasPrefix(t, "'s ") || strings.HasPrefix(t, "’s ") {
|
||||
return ""
|
||||
}
|
||||
if t == "" {
|
||||
return ""
|
||||
}
|
||||
if r, _ := utf8.DecodeRuneInString(t); !(r < utf8.RuneSelf && unicode.IsLetter(r)) {
|
||||
return ""
|
||||
}
|
||||
if ws := latinWords(t); len(ws) > 0 {
|
||||
return ws[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// hasWord says one of the Latin words of s is in set.
|
||||
func hasWord(s string, set map[string]bool) bool {
|
||||
for _, w := range latinWords(s) {
|
||||
if set[strings.TrimSuffix(w, "'s")] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// namesOnly says s is nothing but capitalized words, the ands and ors
|
||||
// between them and markup: "Cicada, Mimi, Phosphor, Parakeet, Muesli",
|
||||
// "Muesli", "Cicada 和 Mimi".
|
||||
func namesOnly(s string) bool {
|
||||
t := s
|
||||
for _, c := range []string{"和", "或", "、", ",", "与", "跟"} {
|
||||
t = strings.ReplaceAll(t, c, " ")
|
||||
}
|
||||
n := 0
|
||||
for _, w := range strings.FieldsFunc(t, func(r rune) bool {
|
||||
return r < utf8.RuneSelf && !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '\''
|
||||
}) {
|
||||
if listJoins[strings.ToLower(w)] {
|
||||
continue
|
||||
}
|
||||
r, _ := utf8.DecodeRuneInString(w)
|
||||
if !isASCII(w) || !unicode.IsUpper(r) {
|
||||
return false
|
||||
}
|
||||
n++
|
||||
}
|
||||
return n > 0 && n <= 20
|
||||
}
|
||||
|
||||
func wordSet(ws ...string) map[string]bool {
|
||||
m := make(map[string]bool, len(ws))
|
||||
for _, w := range ws {
|
||||
m[w] = true
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
var (
|
||||
articles = wordSet("the", "a", "an")
|
||||
listJoins = wordSet("and", "or", "vs", "versus", "nor")
|
||||
|
||||
// nameBefore and nameAfter say the name next to them is a person's
|
||||
nameBefore = wordSet("colleague", "coworker", "co-worker", "teammate", "friend", "boss", "manager", "wife", "husband",
|
||||
"mom", "mum", "dad", "mother", "father", "son", "daughter", "brother", "sister", "aunt", "uncle", "cousin",
|
||||
"grandma", "grandpa", "boyfriend", "girlfriend", "fiance", "fiancee", "teacher", "student", "client", "customer",
|
||||
"mr", "mrs", "ms", "miss", "dr", "prof", "dear", "hi", "hello", "hey", "thanks", "thank", "cc", "ask", "tell",
|
||||
"told", "asked", "email", "emailed", "call", "called", "text", "texted", "meet", "met", "named", "by")
|
||||
nameAfter = wordSet("said", "says", "told", "tells", "asked", "asks", "wrote", "writes", "replied", "thinks", "thought",
|
||||
"wants", "wanted", "likes", "liked", "loves", "prefers", "recommends", "recommended", "suggested", "mentioned",
|
||||
"emailed", "texted", "called", "messaged", "lives", "works", "worked", "jr", "sr")
|
||||
|
||||
// thingBefore and thingAfter say the name next to them is a thing's.
|
||||
// Words both may do (Mimi has, sync with Mimi, I like Mimi) are left to
|
||||
// the sentence
|
||||
thingBefore = wordSet("use", "uses", "using", "used", "install", "installed", "installing", "uninstall", "download",
|
||||
"downloaded", "downloading", "open", "opened", "launch", "launched", "buy", "bought", "try", "tried", "trying",
|
||||
"app", "apps", "tool", "tools", "vs", "versus")
|
||||
thingVerbs = wordSet("switch", "switched", "switching", "move", "moved", "moving", "migrate", "migrated", "migrating",
|
||||
"upgrade", "upgraded", "compare", "compared", "comparing", "import", "imported", "export", "exported")
|
||||
thingAfter = wordSet("app", "apps", "application", "beta", "pro", "plus", "premium", "vs", "versus", "supports",
|
||||
"syncs", "costs", "lets", "offers", "runs", "crashes", "crashed", "integrates", "exports", "imports")
|
||||
// thingKinds before like, called or named make the name after them a
|
||||
// thing's: apps like Cicada, an app called Mimi
|
||||
thingKinds = wordSet("app", "apps", "application", "applications", "tool", "tools", "software", "editors", "clients",
|
||||
"plugin", "plugins", "extension", "extensions", "alternatives", "options")
|
||||
|
||||
// personWords in a name's sentence say a person is meant
|
||||
personWords = wordSet("he", "she", "him", "her", "his", "hers", "himself", "herself",
|
||||
"colleague", "colleagues", "coworker", "teammate", "friend", "boss", "manager", "wife", "husband", "mom", "mum",
|
||||
"dad", "mother", "father", "son", "daughter", "brother", "sister", "boyfriend", "girlfriend", "teacher",
|
||||
"client", "customer", "mr", "mrs", "ms", "dr", "dear", "hi", "hello", "hey", "thanks", "regards", "cc",
|
||||
"said", "says", "told", "tells", "asked", "emailed", "texted", "birthday", "married")
|
||||
// thingWords in a name's sentence say a thing is meant
|
||||
thingWords = wordSet("app", "apps", "application", "applications", "software", "tool", "tools", "utility", "utilities",
|
||||
"plugin", "plugins", "extension", "extensions", "launcher", "widget", "widgets",
|
||||
"macos", "ios", "ipados", "android", "mac", "iphone", "ipad", "windows", "linux", "download",
|
||||
"install", "installed", "pricing", "subscription", "one-time", "trial", "license",
|
||||
"open-source", "features", "alternative", "alternatives", "vs", "versus", "icloud", "reader", "player",
|
||||
"browser", "note-taking", "markdown", "rss")
|
||||
// and Chinese ones, found inside the text (it has no spaces)
|
||||
zhPersonWords = []string{"他", "她", "同事", "朋友", "老板", "先生", "女士", "老师", "说", "告诉"}
|
||||
zhThingWords = []string{"应用", "软件", "工具", "插件", "客户端", "这款", "一款", "下载", "安装", "订阅", "功能", "版本", "替代", "对比", "比较", "好用"}
|
||||
zhThingAfter = []string{"这个软件", "这个应用", "这个工具", "这款", "应用", "软件", "客户端", "插件"}
|
||||
)
|
||||
@@ -0,0 +1,115 @@
|
||||
package redact
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The apps ylorn. was choosing among (Discord, 11-06) are no one's names:
|
||||
// in a list of them, alone, and in the Markdown list of apps the vendor
|
||||
// answered with, each goes as written though the model took it for a
|
||||
// person's. Privacy Filter flagged exactly these texts (zz probe, 11-06).
|
||||
func TestAppNamesNotPersons(t *testing.T) {
|
||||
apps := []string{"Cicada", "Mimi", "Phosphor", "Parakeet", "Muesli"}
|
||||
f := &fakeModel{values: map[string]string{}}
|
||||
for _, v := range apps {
|
||||
f.values[v] = "private_person"
|
||||
}
|
||||
for _, s := range []string{
|
||||
"Cicada, Mimi, Phosphor, Parakeet, Muesli",
|
||||
"Muesli",
|
||||
"Cicada and Mimi",
|
||||
"1. **Cicada** – a minimal note-taking app with Markdown.\n2. **Mimi** – a voice memo app with AI summaries.\n3. **Phosphor** – an RSS reader.\n4. **Parakeet** – a podcast player for macOS.\n5. **Muesli** – a meal planner.",
|
||||
"Which is better for notes on my Mac, Cicada or Mimi?",
|
||||
"I switched to Muesli last week; Parakeet crashes too often.",
|
||||
"I'm using Phosphor now, and Cicada vs Mimi is still open.",
|
||||
"Apps like Muesli are hard to find.",
|
||||
"Parakeet 这个软件很好用,Cicada 和 Mimi 呢?",
|
||||
} {
|
||||
out, _ := Mask(s, Options{Personal: true, Model: f.find})
|
||||
if out != s {
|
||||
t.Errorf("an app's name masked:\n in: %q\nout: %q", s, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A person's name beside an app's is masked as before: a full name, a
|
||||
// first name with a relation, a greeting or said beside it, a first name
|
||||
// in a sentence of a person's doings, and a Chinese name.
|
||||
func TestPersonBesideAppStillMasked(t *testing.T) {
|
||||
f := &fakeModel{values: map[string]string{
|
||||
"Mimi": "private_person", "Phosphor": "private_person", "John Smith": "private_person",
|
||||
"Cicada": "private_person", "Daniel": "private_person", "Laura": "private_person",
|
||||
"张伟": "private_person", "Parakeet": "private_person",
|
||||
}}
|
||||
for _, c := range []struct {
|
||||
s string
|
||||
mask, keep []string
|
||||
}{
|
||||
{"My colleague Mimi said the Phosphor app is great; she uses it every day.", []string{"Mimi"}, []string{"Phosphor"}},
|
||||
{"John Smith asked me to install Cicada on his Mac.", []string{"John Smith"}, []string{"Cicada"}},
|
||||
{"张伟说 Parakeet 这个软件很好用,让我也装一个。", []string{"张伟"}, []string{"Parakeet"}},
|
||||
{"Hi Mimi, thanks for the notes from Tuesday's call. Can you send them to Laura as well?", []string{"Mimi", "Laura"}, nil},
|
||||
{"Mimi will review the draft tomorrow and send it to Daniel.", []string{"Mimi", "Daniel"}, nil},
|
||||
{"Mimi", nil, nil}, // alone it says nothing whose: let go, by design
|
||||
} {
|
||||
out, _ := Mask(c.s, Options{Personal: true, Model: f.find})
|
||||
for _, v := range c.mask {
|
||||
if strings.Contains(out, v) {
|
||||
t.Errorf("%q went as written:\n in: %q\nout: %q", v, c.s, out)
|
||||
}
|
||||
}
|
||||
for _, v := range c.keep {
|
||||
if !strings.Contains(out, v) {
|
||||
t.Errorf("%q masked:\n in: %q\nout: %q", v, c.s, out)
|
||||
}
|
||||
}
|
||||
if back := Restore(out, false); back != c.s {
|
||||
t.Errorf("round trip: %q", back)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Several one-word names masked across a chat's turns come back each as
|
||||
// itself, however the reply orders and splits their placeholders: restore
|
||||
// never gives one name's placeholder another's value. ylorn.'s mix-up was
|
||||
// not a swap here; it was the names masked in one turn and as written in
|
||||
// the next (thingname.go).
|
||||
func TestManyNamesRestoreEachToItself(t *testing.T) {
|
||||
names := []string{"Rosalind", "Mimi", "Torvald", "Ines", "Bartholomew", "Yuki", "Odile", "Kofi"}
|
||||
f := &fakeModel{values: map[string]string{}}
|
||||
for _, v := range names {
|
||||
f.values[v] = "private_person"
|
||||
}
|
||||
ph := map[string]string{}
|
||||
for i, v := range names {
|
||||
turn := "Hi " + v + ", my colleague " + names[(i+1)%len(names)] + " said she would call."
|
||||
out, _ := Mask(turn, Options{Personal: true, Model: f.find})
|
||||
if strings.Contains(out, v) {
|
||||
t.Fatalf("%q not masked: %q", v, out)
|
||||
}
|
||||
for _, p := range placeholderRe.FindAllString(out, -1) {
|
||||
back := Restore(p, false)
|
||||
if old, ok := ph[back]; ok && !strings.EqualFold(old, p) {
|
||||
t.Fatalf("%q masked as %s and %s", back, old, p)
|
||||
}
|
||||
ph[back] = p
|
||||
}
|
||||
}
|
||||
if len(ph) != len(names) {
|
||||
t.Fatalf("%d placeholders for %d names: %v", len(ph), len(names), ph)
|
||||
}
|
||||
// a reply naming them in reverse and in another case
|
||||
var reply, want strings.Builder
|
||||
for i := len(names) - 1; i >= 0; i-- {
|
||||
p := ph[names[i]]
|
||||
if i%2 == 0 {
|
||||
p = strings.ToLower(p)
|
||||
}
|
||||
reply.WriteString(p + " (" + names[i][:1] + "); ")
|
||||
want.WriteString(names[i] + " (" + names[i][:1] + "); ")
|
||||
}
|
||||
if got := Restore(reply.String(), false); got != want.String() {
|
||||
t.Fatalf("restored\n got %q\nwant %q", got, want.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user