A small package in Swift to assist with minimizing words and phrases using the following methods:
It can dramatically save user's keystrokes by allowing shorthand input for comparison against full-length words and phrases. At the same time, it does not interfere with full-length matching under most conditions.
My jumping into learning Japanese inspired this project. Little did I know how many reviews of kanji and vocabulary it takes to gain an entry point into the language. Thousands later, I am doing everything possible to optimize my study because progress dampens otherwise.
Using shorthand in quizzes where English is needed substantially reduces individual keystrokes and time to enter them. Immediately, dropping all vowels plus 'h,' 'w,' and 'y' is a 30% savings over the alphabet. Further removal of repeating characters and spaces yields additional efficiency.
Instead of typing "carry pizza over," the shorthand form "crpzv" can match a predefined phrase unambiguously. As long as the relative order is maintained, the rearrangement of letters is also allowed. For example, "cpzvr" skips the 'r's in 'carry' and uses the one from 'over.'
Omitting characters in the same coding group is also possible. For "blackjack," the shorthand of "blkj" is acceptable due to the Soundex algorithm where the code is 'B420.'
During input, silent 'gh' digraphs can feel cumbersome. Therefore, Samazama removes all non-initial occurrences. This handling requires preventing removal in such terms as "dining hall."
The following function checks user input against a set of answers.
import Samazama
func answerMatches(input: String) throws -> Bool
{
let smzm = Samazama()
let userVariants = try smzm.repeatCharacterVariants(input: input)
// Match user input against one or more answers:
for answer in answers {
let answerVariants = try smzm.repeatCharacterVariants(input: answer)
// Compare Soundex:
if smzm.soundexEqual(answer, input) { return true }
// Compare permutations:
for userVariant in userVariants {
for answerVariant in answerVariants {
if userVariant == answerVariant { return true }
}
}
}
return false
}
The Soundex algorithm contains the following additions:
There are probably more exceptions to handle.
Given the source phrase "carry pizza over," generate all of the minimal forms as shown in the following set:
{
"crpzv", "cpzvr",
"crpzvr", "cpzzvr",
"crpzzv", "crrpzv",
"crrpzvr", "crpzzvr",
"crrpzzv", "crrpzzvr"
}
A user can enter any of the reduced forms to match the source string successfully. This shorthand allowance significantly reduces input requirements for comparing against fixed sources.
The minimal, or shortest, forms are generated along with all intermediate variants, using only characters in the coding set.
Recursion levels get determined by the number of repeating characters. For example, '00001111' gives eight due to four repeating characters of '0' and four of '1'. Generating all variants for eight levels takes approximately 40 K operations using recursive loops.
link |
Stars: 2 |
Last commit: 3 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics