TokenizerApp is an iOS application designed to tokenize sentences based on specific keywords. It supports English and Spanish languages, identifying the words "if" and "and" for English, and "si" and "y" for Spanish to tokenize sentences.
- Tokenize sentences by "if" and "and" in English.
- Tokenize sentences by "si" and "y" in Spanish.
- Language configurability.
This application follows the MVVM (Model-View-ViewModel) design pattern to ensure a separation of concerns and improve maintainability.
- Handles the initial language selection and transitions to the main tokenization interface.
- Implements
UIViewControllerProtocol
to standardize UI setup routines.
- Manages user input for sentence tokenization.
- Updates the UI in response to language selection and tokenization output.
The Languages
enumeration defines the supported languages within the TokenizerApp. It includes a failable initializer that can throw an error if an unsupported language code is used.
English
: Represents the English language with the raw value "en".Spanish
: Represents the Spanish language with the raw value "es".
init(rawValue: String) throws
: Attempts to create aLanguages
instance from a given raw value. Throws aTokenizerError.unsupportedLanguage
if the raw value does not correspond to a supported language.
Used throughout the app to handle language-specific functionality, such as tokenization rules and language settings.
SentenceTokenizer
is responsible for the core functionality of tokenizing input sentences based on language-specific keywords.
private let tokens: [Languages: [String]]
: A dictionary mappingLanguages
to their respective tokenization keywords.
init(tokens: [Languages: [String]] = [Languages.English: ["if", "and"], Languages.Spanish: ["si", "y"]])
: Initializes theSentenceTokenizer
with a default set of tokens for English and Spanish. Custom tokens can be provided during initialization.
func tokenize(sentence: String, language: Languages = .English) throws -> [String]
: Tokenizes the provided sentence according to the specified language's keywords. Returns an array of tokenized strings or throws aTokenizerError.unsupportedLanguage
error if the language is not supported.
An instance of SentenceTokenizer
should be created and used by the TokenizerViewModel
to perform sentence tokenization. It requires a sentence and a Languages
value to perform its task.
TokenizerViewModel
is responsible for managing the business logic associated with sentence tokenization. It interacts with the SentenceTokenizer
model to process input sentences and provides a mechanism to update the view with the results.
private let tokenizer: SentenceTokenizer
: An instance of theSentenceTokenizer
class that performs the actual tokenization of sentences.var updateView: (([String]) -> Void)?
: A closure that is called to update the view with the tokenized sentences or error messages.private var currentLanguage: Languages = .English
: Keeps track of the currently selected language for tokenization.
init(tokenizer: SentenceTokenizer = SentenceTokenizer())
: Initializes theTokenizerViewModel
with aSentenceTokenizer
. The tokenizer is default initialized if not provided.
func tokenize(sentence: String)
: Tokenizes the provided sentence. If tokenization is successful, theupdateView
closure is called with the tokenized sentences. If an error occurs, theupdateView
closure is called with an appropriate error message.func updateLanguage(to language: Languages) throws
: Updates thecurrentLanguage
property. Throws anunsupportedLanguage
error if the language is not supported by the app.
Instances of TokenizerViewModel
should be created and used by the corresponding view controllers that require sentence tokenization functionality. The updateView
closure should be set by the view controller to handle the tokenization results.
UIViewControllerProtocol
is a protocol that standardizes the setup of user interface and accessibility identifiers in ViewControllers within the TokenizerApp. It ensures consistency and enforces a structure for initializing UI components and setting up accessibility features.
func setupUI()
: A method that should be implemented to set up the UI components of the ViewController. This includes layout, styling, and any initial state configuration.func setupAccessibilityIdentifier()
: A method that should be implemented to set up accessibility identifiers for UI components. This is crucial for making the app more accessible and for UI testing.
Any ViewController in the TokenizerApp that needs a standardized method of setting up its UI or accessibility features should conform to this protocol. By conforming to UIViewControllerProtocol
, a ViewController is required to implement both setupUI
and setupAccessibilityIdentifier
methods, ensuring that these essential steps are not overlooked.
Example Implementation:
class SomeViewController: UIViewController, UIViewControllerProtocol {
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
setupAccessibilityIdentifier()
}
func setupUI() {
// Implement UI setup here
}
func setupAccessibilityIdentifier() {
// Implement accessibility identifier setup here
}
}
In this section, we document the utility extensions and services provided within the application. These extensions add functionality to existing Swift classes.
Provides utility properties for language codes used within the app.
spanish
: Returns the string"es"
for the Spanish language code.english
: Returns the string"en"
for the English language code.
Adds utility functions to the UIView
class to enhance user interface elements with consistent styling across the app.
addShadow()
: Applies a shadow effect to aUIView
with predefined properties such as color, offset, opacity, and radius. The shadow extends beyond the edges of the view asmasksToBounds
is set tofalse
.
- Tests the functionality of
SplashViewController
, ensuring language selection and UI setup behave as expected.
- UI tests for the app, verifying that the user interface works correctly with user interactions.