20 lines
831 B
Swift
20 lines
831 B
Swift
import Foundation
|
|
|
|
/// The short word list handed to the speech engine as context.
|
|
public enum VocabularyBuilder {
|
|
/// Terms first (file order), then the unique right-hand sides of correction pairs; at most `maxTerms` words.
|
|
public static func build(from entries: [DictionaryEntry], maxTerms: Int = 24) -> [String] {
|
|
var out: [String] = []
|
|
var seen = Set<String>()
|
|
func add(_ word: String) {
|
|
let key = word.lowercased()
|
|
guard !word.isEmpty, !seen.contains(key) else { return }
|
|
seen.insert(key)
|
|
out.append(word)
|
|
}
|
|
for entry in entries { if case .term(let term) = entry.kind { add(term) } }
|
|
for entry in entries { if case .correction(_, let wrote) = entry.kind { add(wrote) } }
|
|
return Array(out.prefix(maxTerms))
|
|
}
|
|
}
|