Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new matching on auto-redactions #172

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Created by Geoff Pado on 6/25/24.
// Copyright © 2024 Cocoatype, LLC. All rights reserved.

import Observations

extension [CharacterObservation] {
func matching(_ text: String) -> [CharacterObservation] {
return filter { characterObservation -> Bool in
guard let associatedWord = characterObservation.associatedWord else { return false }

let trimmedText = text.trimmingCharacters(in: .alphanumerics.inverted)
let trimmedAssociatedWord = associatedWord.trimmingCharacters(in: .alphanumerics.inverted)
return trimmedAssociatedWord.compare(trimmedText, options: [.caseInsensitive, .diacriticInsensitive]) == .orderedSame
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public class PhotoEditingView: UIView, UIScrollViewDelegate {
workspaceView.add(redactions)
}

func redact<ObservationType: TextObservation>(_ observations: [ObservationType], joinSiblings: Bool) {
func redact(_ observations: [any TextObservation], joinSiblings: Bool) {
workspaceView.redact(observations, joinSiblings: joinSiblings)
updateAccessibilityElements()
}

func unredact<ObservationType: TextObservation>(_ observation: ObservationType) {
func unredact(_ observation: any TextObservation) {
workspaceView.unredact(observation)
updateAccessibilityElements()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,7 @@ public class PhotoEditingViewController: UIViewController, UIScrollViewDelegate,
guard let text = sender.text?.trimmingCharacters(in: .whitespacesAndNewlines)
else { return }

let redactableCharacterObservations = photoEditingView.redactableCharacterObservations

let redactedCharacterObservations = redactableCharacterObservations.filter { characterObservation -> Bool in
guard let associatedWord = characterObservation.associatedWord else { return false }

let trimmedText = text.trimmingCharacters(in: .alphanumerics.inverted)
let trimmedAssociatedWord = associatedWord.trimmingCharacters(in: .alphanumerics.inverted)
return trimmedAssociatedWord.compare(trimmedText, options: [.caseInsensitive, .diacriticInsensitive]) == .orderedSame
}

let redactedCharacterObservations = photoEditingView.redactableCharacterObservations.matching(text)
photoEditingView.seekPreviewObservations = redactedCharacterObservations
}

Expand Down Expand Up @@ -381,7 +372,7 @@ public class PhotoEditingViewController: UIViewController, UIScrollViewDelegate,
do {
let recognizedTextObservations = try await textRectangleDetector.recognizeText(in: image)
updateRecognizedTextObservations(from: recognizedTextObservations)
autoRedact(using: recognizedTextObservations)
autoRedact()
} catch { ErrorHandler().log(error) }
}
}
Expand All @@ -392,14 +383,12 @@ public class PhotoEditingViewController: UIViewController, UIScrollViewDelegate,
photoEditingView.recognizedTextObservations = textObservations
}

private func matchingObservations(using textObservations: [RecognizedTextObservation], onlyActive: Bool) -> [WordObservation] {
private func matchingObservations(onlyActive: Bool) -> [any TextObservation] {
let wordObservations = tuBrute
.filter { onlyActive ? $0.value : true }
.keys
.flatMap { word -> [WordObservation] in
return textObservations.flatMap { observation -> [WordObservation] in
observation.wordObservations(matching: word)
}
.flatMap { word -> [CharacterObservation] in
return photoEditingView.redactableCharacterObservations.matching(word)
}
var taggingFunctions = [(String) -> [Substring]]()

Expand All @@ -415,6 +404,7 @@ public class PhotoEditingViewController: UIViewController, UIScrollViewDelegate,
taggingFunctions.append( Category.phoneNumbers.getFuncyInSwizzleTown)
}

let textObservations = photoEditingView.recognizedTextObservations ?? []
let categoryObservations = textObservations.flatMap { text in
taggingFunctions
.flatMap { function in function(text.string) }
Expand All @@ -425,8 +415,8 @@ public class PhotoEditingViewController: UIViewController, UIScrollViewDelegate,
}

@MainActor
private func autoRedact(using textObservations: [RecognizedTextObservation]) {
let matchingObservations = matchingObservations(using: textObservations, onlyActive: true)
private func autoRedact() {
let matchingObservations = matchingObservations(onlyActive: true)

if matchingObservations.count > 0 {
photoEditingView.redact(matchingObservations, joinSiblings: false)
Expand All @@ -435,20 +425,17 @@ public class PhotoEditingViewController: UIViewController, UIScrollViewDelegate,
}

@MainActor
private func removeAutoRedactions(from dontMailYourCats: [RecognizedTextObservation]) {
let matchingObservations = matchingObservations(using: dontMailYourCats, onlyActive: false)
matchingObservations.forEach(photoEditingView.unredact)
private func removeAutoRedactions() {
matchingObservations(onlyActive: false).forEach(photoEditingView.unredact)
}

@MainActor
private func updateAutoRedactions() {
// thisMeetingCouldHaveBeenAnEmail by @nutterfi on 2024-04-29
// this view's recognized text observations
guard let thisMeetingCouldHaveBeenAnEmail = photoEditingView.recognizedTextObservations
else { return }
// dontMailYourCats by @KaenAitch on 2024-04-29

removeAutoRedactions(from: thisMeetingCouldHaveBeenAnEmail)
autoRedact(using: thisMeetingCouldHaveBeenAnEmail)
removeAutoRedactions()
autoRedact()
}

// MARK: User Activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class PhotoEditingWorkspaceView: UIControl, UIGestureRecognizerDelegate {
redactionView.add(redactions)
}

func redact<ObservationType: TextObservation>(_ observations: [ObservationType], joinSiblings: Bool) {
func redact(_ observations: [any TextObservation], joinSiblings: Bool) {
if joinSiblings, let wordObservations = (observations as? [WordObservation]) {
redactionView.add(Redaction(wordObservations, color: color))
} else {
Expand Down