Add workaround to fix note language recognition and reduce wasteful translation requests

This commit is contained in:
tyiu committed 2024-01-08 20:28:45 -05:00
1 parent 84cfeb1604
commit 1110ffa8af
8 files changed
+161 -25

No files matched your search

+10 -2
View File
@@ -25,6 +25,8 @@
3A8CC6CC2A2CFEF900940F5F /* StringUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A8CC6CB2A2CFEF900940F5F /* StringUtil.swift */; }; 3A8CC6CC2A2CFEF900940F5F /* StringUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A8CC6CB2A2CFEF900940F5F /* StringUtil.swift */; };
3A90B1812A4EA3AF00000D94 /* UserSearchCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A90B1802A4EA3AF00000D94 /* UserSearchCache.swift */; }; 3A90B1812A4EA3AF00000D94 /* UserSearchCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A90B1802A4EA3AF00000D94 /* UserSearchCache.swift */; };
3A90B1832A4EA3C600000D94 /* UserSearchCacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A90B1822A4EA3C600000D94 /* UserSearchCacheTests.swift */; }; 3A90B1832A4EA3C600000D94 /* UserSearchCacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A90B1822A4EA3C600000D94 /* UserSearchCacheTests.swift */; };
3A9ADA302B4CB5F400756AA0 /* TranslatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A9ADA2F2B4CB5F400756AA0 /* TranslatorTests.swift */; };
3A9ADA322B4CBFD000756AA0 /* BlocksExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A9ADA312B4CBFD000756AA0 /* BlocksExtensionTests.swift */; };
3AA247FD297E3CFF0090C62D /* RepostsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA247FC297E3CFF0090C62D /* RepostsModel.swift */; }; 3AA247FD297E3CFF0090C62D /* RepostsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA247FC297E3CFF0090C62D /* RepostsModel.swift */; };
3AA247FF297E3D900090C62D /* RepostsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA247FE297E3D900090C62D /* RepostsView.swift */; }; 3AA247FF297E3D900090C62D /* RepostsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA247FE297E3D900090C62D /* RepostsView.swift */; };
3AA24802297E3DC20090C62D /* RepostView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA24801297E3DC20090C62D /* RepostView.swift */; }; 3AA24802297E3DC20090C62D /* RepostView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA24801297E3DC20090C62D /* RepostView.swift */; };
@@ -260,8 +262,8 @@
4C9B0DF32A65C46800CBDA21 /* ProfileEditButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9B0DF22A65C46800CBDA21 /* ProfileEditButton.swift */; }; 4C9B0DF32A65C46800CBDA21 /* ProfileEditButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9B0DF22A65C46800CBDA21 /* ProfileEditButton.swift */; };
4C9BB83129C0ED4F00FC4E37 /* DisplayName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9BB83029C0ED4F00FC4E37 /* DisplayName.swift */; }; 4C9BB83129C0ED4F00FC4E37 /* DisplayName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9BB83029C0ED4F00FC4E37 /* DisplayName.swift */; };
4C9BB83429C12D9900FC4E37 /* EventProfileName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9BB83329C12D9900FC4E37 /* EventProfileName.swift */; }; 4C9BB83429C12D9900FC4E37 /* EventProfileName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9BB83329C12D9900FC4E37 /* EventProfileName.swift */; };
4C9D6D1B2B1D35D7004E5CD9 /* PullDownSearch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9D6D1A2B1D35D7004E5CD9 /* PullDownSearch.swift */; };
4C9D6D162B1AA9C6004E5CD9 /* DisplayTabBarNotify.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9D6D152B1AA9C6004E5CD9 /* DisplayTabBarNotify.swift */; }; 4C9D6D162B1AA9C6004E5CD9 /* DisplayTabBarNotify.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9D6D152B1AA9C6004E5CD9 /* DisplayTabBarNotify.swift */; };
4C9D6D1B2B1D35D7004E5CD9 /* PullDownSearch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9D6D1A2B1D35D7004E5CD9 /* PullDownSearch.swift */; };
4C9F18E229AA9B6C008C55EC /* CustomizeZapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9F18E129AA9B6C008C55EC /* CustomizeZapView.swift */; }; 4C9F18E229AA9B6C008C55EC /* CustomizeZapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9F18E129AA9B6C008C55EC /* CustomizeZapView.swift */; };
4C9F18E429ABDE6D008C55EC /* MaybeAnonPfpView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9F18E329ABDE6D008C55EC /* MaybeAnonPfpView.swift */; }; 4C9F18E429ABDE6D008C55EC /* MaybeAnonPfpView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9F18E329ABDE6D008C55EC /* MaybeAnonPfpView.swift */; };
4CA2EFA0280E37AC0044ACD8 /* TimelineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA2EF9F280E37AC0044ACD8 /* TimelineView.swift */; }; 4CA2EFA0280E37AC0044ACD8 /* TimelineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA2EF9F280E37AC0044ACD8 /* TimelineView.swift */; };
@@ -669,6 +671,8 @@
3A96D41A298DA94500388A2A /* nl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nl; path = nl.lproj/InfoPlist.strings; sourceTree = "<group>"; }; 3A96D41A298DA94500388A2A /* nl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nl; path = nl.lproj/InfoPlist.strings; sourceTree = "<group>"; };
3A96D41B298DA94500388A2A /* nl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nl; path = nl.lproj/Localizable.strings; sourceTree = "<group>"; }; 3A96D41B298DA94500388A2A /* nl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nl; path = nl.lproj/Localizable.strings; sourceTree = "<group>"; };
3A96D41C298DA94500388A2A /* nl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = nl; path = nl.lproj/Localizable.stringsdict; sourceTree = "<group>"; }; 3A96D41C298DA94500388A2A /* nl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = nl; path = nl.lproj/Localizable.stringsdict; sourceTree = "<group>"; };
3A9ADA2F2B4CB5F400756AA0 /* TranslatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TranslatorTests.swift; sourceTree = "<group>"; };
3A9ADA312B4CBFD000756AA0 /* BlocksExtensionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlocksExtensionTests.swift; sourceTree = "<group>"; };
3AA247FC297E3CFF0090C62D /* RepostsModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RepostsModel.swift; sourceTree = "<group>"; }; 3AA247FC297E3CFF0090C62D /* RepostsModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RepostsModel.swift; sourceTree = "<group>"; };
3AA247FE297E3D900090C62D /* RepostsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepostsView.swift; sourceTree = "<group>"; }; 3AA247FE297E3D900090C62D /* RepostsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepostsView.swift; sourceTree = "<group>"; };
3AA24801297E3DC20090C62D /* RepostView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepostView.swift; sourceTree = "<group>"; }; 3AA24801297E3DC20090C62D /* RepostView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepostView.swift; sourceTree = "<group>"; };
@@ -1070,8 +1074,8 @@
4C9B0DF22A65C46800CBDA21 /* ProfileEditButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileEditButton.swift; sourceTree = "<group>"; }; 4C9B0DF22A65C46800CBDA21 /* ProfileEditButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileEditButton.swift; sourceTree = "<group>"; };
4C9BB83029C0ED4F00FC4E37 /* DisplayName.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayName.swift; sourceTree = "<group>"; }; 4C9BB83029C0ED4F00FC4E37 /* DisplayName.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayName.swift; sourceTree = "<group>"; };
4C9BB83329C12D9900FC4E37 /* EventProfileName.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventProfileName.swift; sourceTree = "<group>"; }; 4C9BB83329C12D9900FC4E37 /* EventProfileName.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventProfileName.swift; sourceTree = "<group>"; };
4C9D6D1A2B1D35D7004E5CD9 /* PullDownSearch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PullDownSearch.swift; sourceTree = "<group>"; };
4C9D6D152B1AA9C6004E5CD9 /* DisplayTabBarNotify.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayTabBarNotify.swift; sourceTree = "<group>"; }; 4C9D6D152B1AA9C6004E5CD9 /* DisplayTabBarNotify.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayTabBarNotify.swift; sourceTree = "<group>"; };
4C9D6D1A2B1D35D7004E5CD9 /* PullDownSearch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PullDownSearch.swift; sourceTree = "<group>"; };
4C9F18E129AA9B6C008C55EC /* CustomizeZapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomizeZapView.swift; sourceTree = "<group>"; }; 4C9F18E129AA9B6C008C55EC /* CustomizeZapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomizeZapView.swift; sourceTree = "<group>"; };
4C9F18E329ABDE6D008C55EC /* MaybeAnonPfpView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaybeAnonPfpView.swift; sourceTree = "<group>"; }; 4C9F18E329ABDE6D008C55EC /* MaybeAnonPfpView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaybeAnonPfpView.swift; sourceTree = "<group>"; };
4CA2EF9F280E37AC0044ACD8 /* TimelineView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineView.swift; sourceTree = "<group>"; }; 4CA2EF9F280E37AC0044ACD8 /* TimelineView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineView.swift; sourceTree = "<group>"; };
@@ -2052,6 +2056,7 @@
children = ( children = (
4C9B0DED2A65A75F00CBDA21 /* AttrStringTestExtensions.swift */, 4C9B0DED2A65A75F00CBDA21 /* AttrStringTestExtensions.swift */,
B5B4D1422B37D47600844320 /* NdbExtensions.swift */, B5B4D1422B37D47600844320 /* NdbExtensions.swift */,
3A9ADA2F2B4CB5F400756AA0 /* TranslatorTests.swift */,
); );
path = Util; path = Util;
sourceTree = "<group>"; sourceTree = "<group>";
@@ -2367,6 +2372,7 @@
D72A2CFF2AD9B66B002AFF62 /* EventViewTests.swift */, D72A2CFF2AD9B66B002AFF62 /* EventViewTests.swift */,
D7315A2B2ACDF4DA0036E30A /* DamusCacheManagerTests.swift */, D7315A2B2ACDF4DA0036E30A /* DamusCacheManagerTests.swift */,
B501062C2B363036003874F5 /* AuthIntegrationTests.swift */, B501062C2B363036003874F5 /* AuthIntegrationTests.swift */,
3A9ADA312B4CBFD000756AA0 /* BlocksExtensionTests.swift */,
); );
path = damusTests; path = damusTests;
sourceTree = "<group>"; sourceTree = "<group>";
@@ -3245,6 +3251,7 @@
D78525252A7B2EA4002FA637 /* NoteContentViewTests.swift in Sources */, D78525252A7B2EA4002FA637 /* NoteContentViewTests.swift in Sources */,
4C3EA67B28FF7B3900C48A62 /* InvoiceTests.swift in Sources */, 4C3EA67B28FF7B3900C48A62 /* InvoiceTests.swift in Sources */,
4C363A9E2828A822006E126D /* ReplyTests.swift in Sources */, 4C363A9E2828A822006E126D /* ReplyTests.swift in Sources */,
3A9ADA322B4CBFD000756AA0 /* BlocksExtensionTests.swift in Sources */,
4C7D097E2A0C58B900943473 /* WalletConnectTests.swift in Sources */, 4C7D097E2A0C58B900943473 /* WalletConnectTests.swift in Sources */,
4CB883AA297612FF00DC99E7 /* ZapTests.swift in Sources */, 4CB883AA297612FF00DC99E7 /* ZapTests.swift in Sources */,
D72A2D022AD9C136002AFF62 /* EventViewTests.swift in Sources */, D72A2D022AD9C136002AFF62 /* EventViewTests.swift in Sources */,
@@ -3264,6 +3271,7 @@
3A3040F329A91366008A0F29 /* ProfileViewTests.swift in Sources */, 3A3040F329A91366008A0F29 /* ProfileViewTests.swift in Sources */,
4CF0ABDC2981A19E00D66079 /* ListTests.swift in Sources */, 4CF0ABDC2981A19E00D66079 /* ListTests.swift in Sources */,
4C684A552A7E91FE005E6031 /* LongPostTests.swift in Sources */, 4C684A552A7E91FE005E6031 /* LongPostTests.swift in Sources */,
3A9ADA302B4CB5F400756AA0 /* TranslatorTests.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
+8
View File
@@ -421,6 +421,14 @@ let test_reply_json = """
{"kind":1,"id":"9c845174ff41dfafe5a1a435ce0da8e00d253951956d760b2d1fa3f45295f146","content":"nostr:npub1wxl6njlcgygduct7jkgzrvyvd9fylj4pqvll6p32h59wyetm5fxqjchcan and nostr:npub1vp8fdcyejd4pqjyrjk9sgz68vuhq7pyvnzk8j0ehlljvwgp8n6eqsrnpsw have come up with 2 different solutions that are a few extra steps but are fantastic.","created_at":1690211612,"tags":[["e","7e31960ca1c9e03664dcbde1b208f2c340af3dc5acfe0479c0bad761f561b6c2"],["p","3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681"],["p","32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"],["p","4d8e327543efbe13ef4f49e43922a40258ac60ededcee062a568f18845a09a04"],["p","71bfa9cbf84110de617e959021b08c69524fcaa1033ffd062abd0ae2657ba24c"],["p","604e96e099936a104883958b040b47672e0f048c98ac793f37ffe4c720279eb2"]],"pubkey":"8c71091d5b7dea833eae73bb07e28f12cc456dc791aa7f213ad175fb5a1d6fe6","sig":"b8a81536c10e2633340206cd9a822f2725827d158f6af9e5888af361d927c83dc6c8ea7598fbe7d6fdbe3086a5f595a92b97a690a6e5e2bfd5c9d216c3a97271"} {"kind":1,"id":"9c845174ff41dfafe5a1a435ce0da8e00d253951956d760b2d1fa3f45295f146","content":"nostr:npub1wxl6njlcgygduct7jkgzrvyvd9fylj4pqvll6p32h59wyetm5fxqjchcan and nostr:npub1vp8fdcyejd4pqjyrjk9sgz68vuhq7pyvnzk8j0ehlljvwgp8n6eqsrnpsw have come up with 2 different solutions that are a few extra steps but are fantastic.","created_at":1690211612,"tags":[["e","7e31960ca1c9e03664dcbde1b208f2c340af3dc5acfe0479c0bad761f561b6c2"],["p","3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681"],["p","32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"],["p","4d8e327543efbe13ef4f49e43922a40258ac60ededcee062a568f18845a09a04"],["p","71bfa9cbf84110de617e959021b08c69524fcaa1033ffd062abd0ae2657ba24c"],["p","604e96e099936a104883958b040b47672e0f048c98ac793f37ffe4c720279eb2"]],"pubkey":"8c71091d5b7dea833eae73bb07e28f12cc456dc791aa7f213ad175fb5a1d6fe6","sig":"b8a81536c10e2633340206cd9a822f2725827d158f6af9e5888af361d927c83dc6c8ea7598fbe7d6fdbe3086a5f595a92b97a690a6e5e2bfd5c9d216c3a97271"}
""" """
let test_english_text_note_with_right_single_quotation_mark = """
{"id": "f742d3cafb2e3f46b44b861e0b5b5faaf7762581dfb13ccf88d5a2217fc89b40","pubkey":"ee6ea13ab9fe5c4a68eaf9b1a34fe014a66b40117c50ee2a614f4cda959b6e74","created_at": 1704641279,"kind": 1,"tags": [["e","29e7bd67d6940de64ed48d21890d25147e588c13c564a4fb3f9c852d9fe02920"],["e","2ea305b10759dfdc85669472c54c8ea4b89931b0ab0a393f262499e510871722"],["p","9770fb48aa3861dd393eb857e740f2df6f18e0ead43bad1d30c65e5c198200a6"]],"content": "It’s a meme.","sig": "87e447cbe3d1983ca64164ce27885d874906975ede15d52415890795d52e3cf720e92eeb9ade79c10cb6a55103dd1df32775152371d736dd61c271f3f2f756b6"}
"""
let test_japanese_text_note = """
{"id": "0d68169af76092b29e043aaa29f000d45cc55923f5b63086cbd332ca16f47221","pubkey":"cd408a69cc6c737ca1a76efc3fa247c6ca53ec807f6e7c9574164164797e8162","created_at": 1704755218,"kind": 1,"tags": [],"content": "もうちょいかかるかとおもた…","sig": "49cb17b25388425aca9b8518919f956c038e6ce8f13be10977927dc4f3c1d6f62aa9cdea0ecabb03f97368e295b352af219b24e8d87304da15e8e01f3b2000f7"}
"""
let test_contact_list_json = """ let test_contact_list_json = """
{"content":"{\\\"wss://nos.lol\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"wss://relay.damus.io\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"ws://monad.jb55.com:8080\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"wss://nostr.wine\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"wss://welcome.nostr.wine\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"wss://eden.nostr.land\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"wss://relay.mostr.pub\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"wss://nostr-pub.wellorder.net\\\":{\\\"write\\\":true,\\\"read\\\":true}}","created_at":1689904312,"id":"20d0ff27d6fcb13de8366328c5b1a7af26bcac07f2e558fbebd5e9242e608c09","kind":3,"pubkey":"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245","sig":"f388e9b45d2872e9320e2d092f52d3d9ccee7e83ef3376b48a705c22099d27736fcc750800e0b6cd4a2a6b7a44c4b0b435c7242392c7ceeb809da40c1fa1ee6c","tags":[["p","6cad545430904b84a8101c5783b65043f19ae29d2da1076b8fc3e64892736f03","wss://nostr-pub.wellorder.net"],["p","2ef93f01cd2493e04235a6b87b10d3c4a74e2a7eb7c3caf168268f6af73314b5","wss://nostr-pub.wellorder.net"],["p","9ec7a778167afb1d30c4833de9322da0c08ba71a69e1911d5578d3144bb56437","wss://nostr.bitcoiner.social"],["p","b2d670de53b27691c0c3400225b65c35a26d06093bcc41f48ffc71e0907f9d4a","wss://nostr-relay.untethr.me"],["p","3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d","wss://nostr-relay.freeberty.net"],["p","8c0da4862130283ff9e67d889df264177a508974e2feb96de139804ea66d6168","wss://nostr-pub.wellorder.net"],["p","21763c71793764a2661eb10ede32a8f2312c9f8db08bc539c888bafa38dcf368"],["p","4570d7a0b49b5524797120810116a2a5c18281423b173a557056f08f15c5382d","wss://nostr-pub.wellorder.net"],["p","04c915daefee38317fa734444acee390a8269fe5810b2241e5e6dd343dfbecc9","wss://nostr-relay.freeberty.net"],["p","d3646691ba5b1d796c1e1b3430df00fe1189ec9c232877adde18c8f656af18f0"],["p","34af16f1787b261331a67fa519e9edb11f0a851c0b10a15901720b0fe0a06d1d"],["p","b7c66ce6f7bbe034e96be54c2ffc0adf631a889abc0834ba1431171b67c489aa"],["p","8355095016fddbe31fcf1453b26f613553e9758cf2263e190eac8fd96a3d3de9"],["p","3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681"],["p","35d26e4690cbe1a898af61cc3515661eb5fa763b57bd0b42e45099c8b32fd50f"],["p","0810b5bc4cddc3e7624a1f6acbdccdc95c6e9409c144ce83365ee04a3a63314e"],["p","51fc7209201b1414f721c3d2d2b3430699b1e6317716c5182cc1d7945072e358"],["p","e6a92d8b6c20426f78bba8510ccdc73df5122814a3bac1d553adebac67a92b27"],["p","619d0c0483a311a16767b0a7999ce9f28e58e79eff66f0edafae9ca2d9054f08"],["p","d7b76d02c758a62a505e03bd5f5049aaee4e7e36283d273c7f6798912692df2b"],["p","d4d4fdde8ab4924b1e452e896709a3bd236da4c0576274b52af5992d4d34762c"],["p","3878d95db7b854c3a0d3b2d6b7bf9bf28b36162be64326f5521ba71cf3b45a69"],["p","ac9ec020170155f0feb347f0d777ee5fc38dd1f36353093046323646cff5169f"],["p","d91191e30e00444b942c0e82cad470b32af171764c2275bee0bd99377efd4075"],["p","146bda4ec6932830503ee4f8e8b626bd7b3a5784232b8240ba15c8cbff9a07cd"],["p","ea75802dd1c86933c1e20c582541bb283d44c88e3445ed90d4375fc3d973f3a0"],["p","41108126409bf99cb77ff16fd53f4da2e53010b0dca04b0a53ebdf46eade37aa"],["p","9682c33f9024dadb1bffdf762c3156e26b4aa340de8d06c91ca537fcc0fdb3a9"],["p","a8f14f05c64f9e62bdada89c21a52f09aa5d7948b47ccf52da1be16b0de9efac"],["p","80482e60178c2ce996da6d67577f56a2b2c47ccb1c84c81f2b7960637cb71b78"],["p","b10c0000079a83cf26815dc7538818d8d56a2983e374e30a4143e50060978457"],["p","547fcc5c7e655fe7c83da5a812e6332f0a4779c87bf540d8e75a4edbbf36fe4a"],["p","dd81a8bacbab0b5c3007d1672fb8301383b4e9583d431835985057223eb298a5"],["p","bb1cf5250435ff475cd8b32acb23e3ee7bbe8fc38f6951704b4798513947672c"],["p","1fa91680ebfc68069ec13423fc8b9b0a746e9265584e16cf7d80be7ad721de6e"],["p","52b4a076bcbbbdc3a1aefa3735816cf74993b1b8db202b01c883c58be7fad8bd"],["p","d8139f130cc1dec5d92e3a4dc49fb11f064bf5b32c65d96da107ba2389547dc7"],["p","9ac12013d20fae4f8829ba4e5ba6343e410288d3a0752d6143386d2c1af1f57e"],["p","46fcbe3065eaf1ae7811465924e48923363ff3f526bd6f73d7c184b16bd8ce4d"],["p","4b0572ab1f9a575415326b599f1179c20134bc23040d207156e71800b4ed33fe"],["p","2fa4b9ba71b6dab17c4723745bb7850dfdafcb6ae1a8642f76f9c64fa5f43436"],["p","3235036bd0957dfb27ccda02d452d7c763be40c91a1ac082ba6983b25238388c"],["p","954aaf69c2e7c9fb3f9998f61944ab8ab08ce3c8679ecd985e4486a6eb696217"],["p","d7f0e3917c466f1e2233e9624fbd6d4bd1392dbcfcaf3574f457569d496cb731"],["p","08b80da85ba68ac031885ea555ab42bb42231fde9b690bbd0f48c128dfbf8009"],["p","104749bc9151a0e54b9845ee50fc4b559439dd1ada006e36a6c49ad3ea16a55c"],["p","c47daa0cd21a70797fe9404f8fe0c3f679c2b46148788d1295e6424232064f1d"],["p","d987084c48390a290f5d2a34603ae64f55137d9b4affced8c0eae030eb222a25"],["p","e1ff3bfdd4e40315959b08b4fcc8245eaa514637e1d4ec2ae166b743341be1af"],["p","cf9413eb6bbe55c8a3c10119ec0635e134fa266f2c50f825d7225da9b92ecc4e"],["p","bae77874946ec111f94be59aef282de092dc4baf213f8ecb8c9e15cb7ed7304e"],["p","c7eda660a6bc8270530e82b4a7712acdea2e31dc0a56f8dc955ac009efd97c86"],["p","2590201e2919a8aa6568c88900192aa54ef00e6c0974a5Line truncated {"content":"{\\\"wss://nos.lol\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"wss://relay.damus.io\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"ws://monad.jb55.com:8080\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"wss://nostr.wine\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"wss://welcome.nostr.wine\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"wss://eden.nostr.land\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"wss://relay.mostr.pub\\\":{\\\"write\\\":true,\\\"read\\\":true},\\\"wss://nostr-pub.wellorder.net\\\":{\\\"write\\\":true,\\\"read\\\":true}}","created_at":1689904312,"id":"20d0ff27d6fcb13de8366328c5b1a7af26bcac07f2e558fbebd5e9242e608c09","kind":3,"pubkey":"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245","sig":"f388e9b45d2872e9320e2d092f52d3d9ccee7e83ef3376b48a705c22099d27736fcc750800e0b6cd4a2a6b7a44c4b0b435c7242392c7ceeb809da40c1fa1ee6c","tags":[["p","6cad545430904b84a8101c5783b65043f19ae29d2da1076b8fc3e64892736f03","wss://nostr-pub.wellorder.net"],["p","2ef93f01cd2493e04235a6b87b10d3c4a74e2a7eb7c3caf168268f6af73314b5","wss://nostr-pub.wellorder.net"],["p","9ec7a778167afb1d30c4833de9322da0c08ba71a69e1911d5578d3144bb56437","wss://nostr.bitcoiner.social"],["p","b2d670de53b27691c0c3400225b65c35a26d06093bcc41f48ffc71e0907f9d4a","wss://nostr-relay.untethr.me"],["p","3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d","wss://nostr-relay.freeberty.net"],["p","8c0da4862130283ff9e67d889df264177a508974e2feb96de139804ea66d6168","wss://nostr-pub.wellorder.net"],["p","21763c71793764a2661eb10ede32a8f2312c9f8db08bc539c888bafa38dcf368"],["p","4570d7a0b49b5524797120810116a2a5c18281423b173a557056f08f15c5382d","wss://nostr-pub.wellorder.net"],["p","04c915daefee38317fa734444acee390a8269fe5810b2241e5e6dd343dfbecc9","wss://nostr-relay.freeberty.net"],["p","d3646691ba5b1d796c1e1b3430df00fe1189ec9c232877adde18c8f656af18f0"],["p","34af16f1787b261331a67fa519e9edb11f0a851c0b10a15901720b0fe0a06d1d"],["p","b7c66ce6f7bbe034e96be54c2ffc0adf631a889abc0834ba1431171b67c489aa"],["p","8355095016fddbe31fcf1453b26f613553e9758cf2263e190eac8fd96a3d3de9"],["p","3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681"],["p","35d26e4690cbe1a898af61cc3515661eb5fa763b57bd0b42e45099c8b32fd50f"],["p","0810b5bc4cddc3e7624a1f6acbdccdc95c6e9409c144ce83365ee04a3a63314e"],["p","51fc7209201b1414f721c3d2d2b3430699b1e6317716c5182cc1d7945072e358"],["p","e6a92d8b6c20426f78bba8510ccdc73df5122814a3bac1d553adebac67a92b27"],["p","619d0c0483a311a16767b0a7999ce9f28e58e79eff66f0edafae9ca2d9054f08"],["p","d7b76d02c758a62a505e03bd5f5049aaee4e7e36283d273c7f6798912692df2b"],["p","d4d4fdde8ab4924b1e452e896709a3bd236da4c0576274b52af5992d4d34762c"],["p","3878d95db7b854c3a0d3b2d6b7bf9bf28b36162be64326f5521ba71cf3b45a69"],["p","ac9ec020170155f0feb347f0d777ee5fc38dd1f36353093046323646cff5169f"],["p","d91191e30e00444b942c0e82cad470b32af171764c2275bee0bd99377efd4075"],["p","146bda4ec6932830503ee4f8e8b626bd7b3a5784232b8240ba15c8cbff9a07cd"],["p","ea75802dd1c86933c1e20c582541bb283d44c88e3445ed90d4375fc3d973f3a0"],["p","41108126409bf99cb77ff16fd53f4da2e53010b0dca04b0a53ebdf46eade37aa"],["p","9682c33f9024dadb1bffdf762c3156e26b4aa340de8d06c91ca537fcc0fdb3a9"],["p","a8f14f05c64f9e62bdada89c21a52f09aa5d7948b47ccf52da1be16b0de9efac"],["p","80482e60178c2ce996da6d67577f56a2b2c47ccb1c84c81f2b7960637cb71b78"],["p","b10c0000079a83cf26815dc7538818d8d56a2983e374e30a4143e50060978457"],["p","547fcc5c7e655fe7c83da5a812e6332f0a4779c87bf540d8e75a4edbbf36fe4a"],["p","dd81a8bacbab0b5c3007d1672fb8301383b4e9583d431835985057223eb298a5"],["p","bb1cf5250435ff475cd8b32acb23e3ee7bbe8fc38f6951704b4798513947672c"],["p","1fa91680ebfc68069ec13423fc8b9b0a746e9265584e16cf7d80be7ad721de6e"],["p","52b4a076bcbbbdc3a1aefa3735816cf74993b1b8db202b01c883c58be7fad8bd"],["p","d8139f130cc1dec5d92e3a4dc49fb11f064bf5b32c65d96da107ba2389547dc7"],["p","9ac12013d20fae4f8829ba4e5ba6343e410288d3a0752d6143386d2c1af1f57e"],["p","46fcbe3065eaf1ae7811465924e48923363ff3f526bd6f73d7c184b16bd8ce4d"],["p","4b0572ab1f9a575415326b599f1179c20134bc23040d207156e71800b4ed33fe"],["p","2fa4b9ba71b6dab17c4723745bb7850dfdafcb6ae1a8642f76f9c64fa5f43436"],["p","3235036bd0957dfb27ccda02d452d7c763be40c91a1ac082ba6983b25238388c"],["p","954aaf69c2e7c9fb3f9998f61944ab8ab08ce3c8679ecd985e4486a6eb696217"],["p","d7f0e3917c466f1e2233e9624fbd6d4bd1392dbcfcaf3574f457569d496cb731"],["p","08b80da85ba68ac031885ea555ab42bb42231fde9b690bbd0f48c128dfbf8009"],["p","104749bc9151a0e54b9845ee50fc4b559439dd1ada006e36a6c49ad3ea16a55c"],["p","c47daa0cd21a70797fe9404f8fe0c3f679c2b46148788d1295e6424232064f1d"],["p","d987084c48390a290f5d2a34603ae64f55137d9b4affced8c0eae030eb222a25"],["p","e1ff3bfdd4e40315959b08b4fcc8245eaa514637e1d4ec2ae166b743341be1af"],["p","cf9413eb6bbe55c8a3c10119ec0635e134fa266f2c50f825d7225da9b92ecc4e"],["p","bae77874946ec111f94be59aef282de092dc4baf213f8ecb8c9e15cb7ed7304e"],["p","c7eda660a6bc8270530e82b4a7712acdea2e31dc0a56f8dc955ac009efd97c86"],["p","2590201e2919a8aa6568c88900192aa54ef00e6c0974a5Line truncated
""" """
+49 -1
View File
@@ -6,7 +6,7 @@
// //
import Foundation import Foundation
import NaturalLanguage
fileprivate extension String { fileprivate extension String {
/// Failable initializer to build a Swift.String from a C-backed `str_block_t`. /// Failable initializer to build a Swift.String from a C-backed `str_block_t`.
@@ -218,3 +218,51 @@ extension Block {
} }
} }
} }
extension Blocks {
/// Returns a language hypothesis represented as an ``NLLanguage`` (determined by ``NLLanguageRecognizer``),
/// which is the most likely language detected using the combination of blocks. If it cannot determine one, `nil` is returned.
var languageHypothesis: NLLanguage? {
// Rely on Apple's NLLanguageRecognizer to tell us which language it thinks the blocks are in
// and filter on only the text portions of the content as URLs, hashtags, and anything else confuse the language recognizer.
let originalOnlyText = blocks.compactMap {
if case .text(let txt) = $0 {
// Replacing right single quotation marks (’) with "typewriter or ASCII apostrophes" (')
// as a workaround to get Apple's language recognizer to predict language the correctly.
// It is important to add this workaround to get the language right because it wastes users' money to send translation requests.
// Until Apple fixes their language model, this workaround will be kept in place.
// See https://en.wikipedia.org/wiki/Apostrophe#Unicode for an explanation of the differences between the two characters.
//
// For example,
// "nevent1qqs0wsknetaju06xk39cv8sttd064amkykqalvfue7ydtg3p0lyfksqzyrhxagf6h8l9cjngatumrg60uq22v66qz979pm32v985ek54ndh8gj42wtp"
// has the note content "It’s a meme".
// Without the character replacement, it is 61% confident that the text is in Turkish (tr) and 8% confident that the text is in English (en),
// which is a wildly incorrect hypothesis.
// With the character replacement, it is 65% confident that the text is in English (en) and 24% confident that the text is in Turkish (tr), which is more accurate.
//
// Similarly,
// "nevent1qqspjqlln6wvxrqg6kzl2p7gk0rgr5stc7zz5sstl34cxlw55gvtylgpp4mhxue69uhkummn9ekx7mqpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5qy28wumn8ghj7un9d3shjtnwdaehgu3wvfnsygpx6655ve67vqlcme9ld7ww73pqx7msclhwzu8lqmkhvuluxnyc7yhf3xut"
// has the note content "You’re funner".
// Without the character replacement, it is 52% confident that the text is in Norwegian Bokmål (nb) and 41% confident that the text is in English (en).
// With the character replacement, it is 93% confident that the text is in English (en) and 4% confident that the text is in Norwegian Bokmål (nb).
return txt.replacingOccurrences(of: "’", with: "'")
}
else {
return nil
}
}
.joined(separator: " ")
// If there is no text, there's nothing to use to detect language.
guard !originalOnlyText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
return nil
}
let languageRecognizer = NLLanguageRecognizer()
languageRecognizer.processString(originalOnlyText)
// Only accept language recognition hypothesis if there's at least a 50% probability that it's accurate.
return languageRecognizer.languageHypotheses(withMaximum: 1).first(where: { $0.value >= 0.5 })?.key
}
}
+11 -1
View File
@@ -22,7 +22,17 @@ public struct Translator {
self.purple = purple self.purple = purple
} }
/// Returns true if the translator should attempt to translate from the source language to the target language and valid translation service settings are configured.
func shouldTranslate(from sourceLanguage: String, to targetLanguage: String) -> Bool {
return sourceLanguage != targetLanguage && userSettingsStore.can_translate
}
public func translate(_ text: String, from sourceLanguage: String, to targetLanguage: String) async throws -> String? { public func translate(_ text: String, from sourceLanguage: String, to targetLanguage: String) async throws -> String? {
// Do not attempt to translate if the source and target languages are the same.
guard shouldTranslate(from: sourceLanguage, to: targetLanguage) else {
return nil
}
switch userSettingsStore.translation_service { switch userSettingsStore.translation_service {
case .purple: case .purple:
return try await translateWithPurple(text, from: sourceLanguage, to: targetLanguage) return try await translateWithPurple(text, from: sourceLanguage, to: targetLanguage)
@@ -35,7 +45,7 @@ public struct Translator {
case .deepl: case .deepl:
return try await translateWithDeepL(text, from: sourceLanguage, to: targetLanguage) return try await translateWithDeepL(text, from: sourceLanguage, to: targetLanguage)
case .none: case .none:
return text return nil
} }
} }
+26
View File
@@ -0,0 +1,26 @@
//
// BlocksExtensionTests.swift
// damusTests
//
// Created by Terry Yiu on 1/8/24.
//
import XCTest
import NaturalLanguage
@testable import damus
final class BlocksExtensionTests: XCTestCase {
func testLanguageHypothesisIsCorrectWithRightSingleQuotationMark() throws {
let note = try XCTUnwrap(NdbNote.owned_from_json(json: test_english_text_note_with_right_single_quotation_mark))
let blocks = note.blocks(test_keypair)
XCTAssertEqual(blocks.languageHypothesis, NLLanguage.english)
}
func testLanguageHypothesisIsCorrectWithNonEnglishLocale() throws {
let note = try XCTUnwrap(NdbNote.owned_from_json(json: test_japanese_text_note))
let blocks = note.blocks(test_keypair)
XCTAssertEqual(blocks.languageHypothesis, NLLanguage.japanese)
}
}
+40
View File
@@ -0,0 +1,40 @@
//
// TranslatorTests.swift
// damusTests
//
// Created by Terry Yiu on 1/8/24.
//
import XCTest
@testable import damus
final class TranslatorTests: XCTestCase {
func testShouldTranslateWhenLanguagesAreDifferent() throws {
let userSettingsStore = UserSettingsStore()
userSettingsStore.translation_service = .purple
let translator = Translator(userSettingsStore, purple: DamusPurple(environment: .local_test, keypair: test_keypair))
XCTAssertTrue(translator.shouldTranslate(from: "en", to: "es"))
XCTAssertTrue(translator.shouldTranslate(from: "es", to: "fr"))
}
func testShouldNotTranslateWhenLanguagesAreTheSame() throws {
let userSettingsStore = UserSettingsStore()
userSettingsStore.translation_service = .purple
let translator = Translator(userSettingsStore, purple: DamusPurple(environment: .local_test, keypair: test_keypair))
XCTAssertFalse(translator.shouldTranslate(from: "en", to: "en"))
XCTAssertFalse(translator.shouldTranslate(from: "es", to: "es"))
}
func testShouldNotTranslateWhenNoTranslationServiceSelected() throws {
let userSettingsStore = UserSettingsStore()
userSettingsStore.translation_service = .none
let translator = Translator(userSettingsStore, purple: DamusPurple(environment: .local_test, keypair: test_keypair))
XCTAssertFalse(translator.shouldTranslate(from: "en", to: "es"))
XCTAssertFalse(translator.shouldTranslate(from: "es", to: "fr"))
}
}
+3 -21
View File
@@ -406,31 +406,13 @@ extension NdbNote {
func note_language(_ keypair: Keypair) -> String? { func note_language(_ keypair: Keypair) -> String? {
assert(!Thread.isMainThread, "This function must not be run on the main thread.") assert(!Thread.isMainThread, "This function must not be run on the main thread.")
// Rely on Apple's NLLanguageRecognizer to tell us which language it thinks the note is in guard let languageHypothesis = self.blocks(keypair).languageHypothesis else {
// and filter on only the text portions of the content as URLs and hashtags confuse the language recognizer. return nil
let originalBlocks = self.blocks(keypair).blocks
let originalOnlyText = originalBlocks.compactMap {
if case .text(let txt) = $0 {
return txt
}
else {
return nil
}
}
.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 {
let nstr: String? = nil
return nstr
} }
// Remove the variant component and just take the language part as translation services typically only supports the variant-less language. // 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. // Moreover, speakers of one variant can generally understand other variants.
return localeToLanguage(locale) return localeToLanguage(languageHypothesis.rawValue)
} }
var age: TimeInterval { var age: TimeInterval {
+14
View File
@@ -281,5 +281,19 @@ final class NdbTests: XCTestCase {
} }
func test_note_language_english_with_right_single_quotation_mark() throws {
let note = try XCTUnwrap(NdbNote.owned_from_json(json: test_english_text_note_with_right_single_quotation_mark))
Task {
XCTAssertEqual(note.note_language(test_keypair), "en")
}
}
func test_note_language_non_english() throws {
let note = try XCTUnwrap(NdbNote.owned_from_json(json: test_japanese_text_note))
Task {
XCTAssertEqual(note.note_language(test_keypair), "ja")
}
}
} }