damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

TranslationTests.swift (2960B)


      1 //
      2 //  TranslationTests.swift
      3 //  damusTests
      4 //
      5 //  Created by KernelKind on 2/13/24.
      6 //
      7 
      8 import XCTest
      9 @testable import damus
     10 
     11 final class TranslationTests : XCTestCase {
     12     let translationStringDistanceCases = [
     13         ("test", "test ", false),
     14         ("wat", "what", false),
     15         ("wat's the wether like", "what's the weather like", true),
     16         ("GM GZY⚡️\n\redacted 🍆🦪🤙 https://video.nostr.build/7dadcc39e83cbc37c99fabb883314f29c169c1bd994f1d525bde6e9817facc85.mp4 ", "GM GZY⚡️\n\redacted 🍆🦪🤙 https://video.nostr.build/7dadcc39e83cbc37c99fabb883314f29c169c1bd994f1d525bde6e9817facc85.mp4", false),
     17         ("Fucking nostr forever typo’s lol 😂", "Fucking nostr forever typo's lol 😂", false),
     18         ("where's the library", "donde esta la libreria", true),
     19         ("In America", "En América", true)
     20     ]
     21     
     22     func testStringDistanceRequirements() {
     23         for (original, translated, expectedVal) in translationStringDistanceCases {
     24             XCTAssertEqual(translationMeetsStringDistanceRequirements(original: original, translated: translated), expectedVal)
     25         }
     26     }
     27     
     28     let levenshteinDistanceCases = [
     29         // (original string, mutated string, number of changes from original to mutated)
     30         ("hello", "hello", 0),    // No change
     31         ("123", "1234", 1),       // Addition at the end
     32         ("abcd", "abcde", 1),     // Addition at the end
     33         ("abc", "a", 2),          // Multiple deletions
     34         ("abcdef", "abc", 3),     // Multiple deletions
     35         ("2024", "2025", 1),      // Single substitution
     36         ("openai", "opnai", 1),   // Single deletion
     37         ("swift", "swiift", 1),   // Single addition
     38         ("language", "languag", 1), // Single deletion at the end
     39         ("example", "sxample", 1),  // Single substitution at the beginning
     40         ("distance", "d1stanc3", 2), // Substitutions
     41         ("python", "pyth0n", 1),    // Single substitution
     42         ("algorithm", "algor1thm", 1), // Single substitution in the middle
     43         ("implementation", "implemenation", 1), // Single deletion (typo)
     44         ("correction", "correctionn", 1),       // Single addition at the end
     45         ("levenshtein", "levenshtien", 2),      // Transposition
     46         ("threshold", "threshhold", 1),         // Single addition (double letter)
     47         ("functionality", "fuctionality", 1),   // Single deletion (common typo)
     48         ("assessment", "assesment", 1),         // Single deletion (common typo)
     49         ("performance", "performence", 1),      // Single substitution (common typo)
     50     ]
     51     
     52     func testLevenshteinDistance() {
     53         for (original, mutated, numChanges) in levenshteinDistanceCases {
     54             XCTAssertTrue(levenshteinDistanceIsGreaterThanOrEqualTo(from: original, to: mutated, threshold: numChanges))
     55             XCTAssertFalse(levenshteinDistanceIsGreaterThanOrEqualTo(from: original, to: mutated, threshold: numChanges+1))
     56         }
     57     }
     58 }