From f75fc7eebeef8eb1e0a69d244a7897b9295ad0a5 Mon Sep 17 00:00:00 2001 From: Terry Yiu <963907+tyiu@users.noreply.github.com> Date: Sat, 25 Mar 2023 23:47:23 -0600 Subject: [PATCH] Add optional language filter on Universe feed --- damus/Components/TranslateView.swift | 19 +----------------- damus/Models/UserSettingsStore.swift | 7 +++++++ damus/Nostr/NostrEvent.swift | 20 +++++++++++++++++++ damus/Util/LocalizationUtil.swift | 11 ++++++++++ damus/Views/ConfigView.swift | 2 ++ damus/Views/SearchHomeView.swift | 30 +++++++++++++++++++++++----- 6 files changed, 66 insertions(+), 23 deletions(-) diff --git a/damus/Components/TranslateView.swift b/damus/Components/TranslateView.swift index fc9b8530..89681557 100644 --- a/damus/Components/TranslateView.swift +++ b/damus/Components/TranslateView.swift @@ -80,24 +80,7 @@ struct TranslateView: View { currentLanguage = Locale.current.languageCode ?? "en" } - // Rely on Apple's NLLanguageRecognizer to tell us which language it thinks the note is in - // and filter on only the text portions of the content as URLs and hashtags confuse the language recognizer. - let originalBlocks = event.blocks(damus_state.keypair.privkey) - let originalOnlyText = originalBlocks.compactMap { $0.is_text }.joined(separator: " ") - - // Only accept language recognition hypothesis if there's at least a 50% probability that it's accurate. - let languageRecognizer = NLLanguageRecognizer() - languageRecognizer.processString(originalOnlyText) - noteLanguage = languageRecognizer.languageHypotheses(withMaximum: 1).first(where: { $0.value >= 0.5 })?.key.rawValue ?? currentLanguage - - if let lang = noteLanguage, noteLanguage != currentLanguage { - // If the detected dominant language is a variant, remove the variant component and just take the language part as translation services typically only supports the variant-less language. - if #available(iOS 16, *) { - noteLanguage = Locale.LanguageCode(stringLiteral: lang).identifier(.alpha2) - } else { - noteLanguage = NSLocale(localeIdentifier: lang).languageCode - } - } + noteLanguage = event.note_language(damus_state.keypair.privkey) ?? currentLanguage guard let note_lang = noteLanguage else { noteLanguage = currentLanguage diff --git a/damus/Models/UserSettingsStore.swift b/damus/Models/UserSettingsStore.swift index 618fe04f..d2ca2ece 100644 --- a/damus/Models/UserSettingsStore.swift +++ b/damus/Models/UserSettingsStore.swift @@ -128,6 +128,12 @@ class UserSettingsStore: ObservableObject { } } + @Published var show_only_preferred_languages: Bool { + didSet { + UserDefaults.standard.set(show_only_preferred_languages, forKey: "show_only_preferred_languages") + } + } + @Published var translation_service: TranslationService { didSet { UserDefaults.standard.set(translation_service.rawValue, forKey: "translation_service") @@ -210,6 +216,7 @@ class UserSettingsStore: ObservableObject { left_handed = UserDefaults.standard.object(forKey: "left_handed") as? Bool ?? false zap_vibration = UserDefaults.standard.object(forKey: "zap_vibration") as? Bool ?? false disable_animation = should_disable_image_animation() + show_only_preferred_languages = UserDefaults.standard.object(forKey: "show_only_preferred_languages") as? Bool ?? false // Note from @tyiu: // Default translation service is disabled by default for now until we gain some confidence that it is working well in production. diff --git a/damus/Nostr/NostrEvent.swift b/damus/Nostr/NostrEvent.swift index 2d3f8533..d2efc3a1 100644 --- a/damus/Nostr/NostrEvent.swift +++ b/damus/Nostr/NostrEvent.swift @@ -10,6 +10,7 @@ import CommonCrypto import secp256k1 import secp256k1_implementation import CryptoKit +import NaturalLanguage @@ -259,6 +260,25 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has return event_is_reply(self, privkey: privkey) } + func note_language(_ privkey: String?) -> String? { + // Rely on Apple's NLLanguageRecognizer to tell us which language it thinks the note is in + // and filter on only the text portions of the content as URLs and hashtags confuse the language recognizer. + let originalBlocks = blocks(privkey) + let originalOnlyText = originalBlocks.compactMap { $0.is_text }.joined(separator: " ") + + // Only accept language recognition hypothesis if there's at least a 50% probability that it's accurate. + let languageRecognizer = NLLanguageRecognizer() + languageRecognizer.processString(originalOnlyText) + + guard let locale = languageRecognizer.languageHypotheses(withMaximum: 1).first(where: { $0.value >= 0.5 })?.key.rawValue else { + return nil + } + + // Remove the variant component and just take the language part as translation services typically only supports the variant-less language. + // Moreover, speakers of one variant can generally understand other variants. + return localeToLanguage(locale) + } + public var referenced_ids: [ReferencedId] { return get_referenced_ids(key: "e") } diff --git a/damus/Util/LocalizationUtil.swift b/damus/Util/LocalizationUtil.swift index b0fdad15..74afc98b 100644 --- a/damus/Util/LocalizationUtil.swift +++ b/damus/Util/LocalizationUtil.swift @@ -21,3 +21,14 @@ func localizedStringFormat(key: String, locale: Locale?) -> String { let fallback = bundleForLocale(locale: Locale(identifier: "en-US")).localizedString(forKey: key, value: nil, table: nil) return bundle.localizedString(forKey: key, value: fallback, table: nil) } + +/** + Removes the variant part of a locale code so that it contains only the language code. + */ +func localeToLanguage(_ locale: String) -> String? { + if #available(iOS 16, *) { + return Locale.LanguageCode(stringLiteral: locale).identifier(.alpha2) + } else { + return NSLocale(localeIdentifier: locale).languageCode + } +} diff --git a/damus/Views/ConfigView.swift b/damus/Views/ConfigView.swift index 3be7c049..0eea0c44 100644 --- a/damus/Views/ConfigView.swift +++ b/damus/Views/ConfigView.swift @@ -153,6 +153,8 @@ struct ConfigView: View { } Section(NSLocalizedString("Translations", comment: "Section title for selecting the translation service.")) { + Toggle(NSLocalizedString("Show only preferred languages on Universe feed", comment: "Toggle to show notes that are only in the device's preferred languages on the Universe feed and hide notes that are in other languages."), isOn: $settings.show_only_preferred_languages) + .toggleStyle(.switch) Picker(NSLocalizedString("Service", comment: "Prompt selection of translation service provider."), selection: $settings.translation_service) { ForEach(TranslationService.allCases, id: \.self) { server in Text(server.model.displayName) diff --git a/damus/Views/SearchHomeView.swift b/damus/Views/SearchHomeView.swift index 74c6a2ef..8fc929c7 100644 --- a/damus/Views/SearchHomeView.swift +++ b/damus/Views/SearchHomeView.swift @@ -7,12 +7,15 @@ import SwiftUI import CryptoKit +import NaturalLanguage struct SearchHomeView: View { let damus_state: DamusState @StateObject var model: SearchHomeModel @State var search: String = "" @FocusState private var isFocused: Bool + + let preferredLanguages = Set(Locale.preferredLanguages.map { localeToLanguage($0) }) var SearchInput: some View { HStack { @@ -41,12 +44,29 @@ struct SearchHomeView: View { } var GlobalContent: some View { - return TimelineView(events: model.events, loading: $model.loading, damus: damus_state, show_friend_icon: true, filter: { _ in true }) - .refreshable { - // Fetch new information by unsubscribing and resubscribing to the relay - model.unsubscribe() - model.subscribe() + return TimelineView( + events: model.events, + loading: $model.loading, + damus: damus_state, + show_friend_icon: true, + filter: { + if damus_state.settings.show_only_preferred_languages == false { + return true + } + + // If we can't determine the note's language with 50%+ confidence, lean on the side of caution and show it anyway. + guard let noteLanguage = $0.note_language(damus_state.keypair.privkey) else { + return true + } + + return preferredLanguages.contains(noteLanguage) } + ) + .refreshable { + // Fetch new information by unsubscribing and resubscribing to the relay + model.unsubscribe() + model.subscribe() + } } var SearchContent: some View {