Keyword Classifier
Classify a text into one of three categories based on keyword matching:
- sports: contains any of ["goal", "team", "player", "match", "score"]
- tech: contains any of ["code", "software", "computer", "algorithm", "data"]
- food: contains any of ["recipe", "cook", "ingredient", "meal", "dish"]
Check categories in the order: sports → tech → food. Return the first matching category. If no keywords match, return "other".
All matching is case-insensitive and checks if the keyword appears as a whole word in the text.
Input: A single line of text.
Example:
The team scored a goal
sports
Step 1: Lowercase and split "the team scored a goal" → words: ["the", "team", "scored", "a", "goal"]
Step 2: Check sports keywords "team" matches → return "sports"
Constraints:
- Case-insensitive word matching
- Check sports first, then tech, then food
- Return first matching category
- If no match, return "other"
Background Knowledge
The problem of text classification is a fundamental task in Natural Language Processing (NLP), which involves assigning a category or label to a piece of text based on its content. In this case, we are dealing with a simple keyword-based classification system, where the presence of specific words determines the category of the text. This approach relies on the concept of keyword extraction, which is a technique used to identify the most relevant and important words in a text.
To solve this problem, we need to understand the concept of whole word matching, which means that we are looking for exact word matches, rather than substrings or parts of words. For example, the keyword "team" should match the word "team" in the text, but not the word "steam" or "dream". We also need to consider case-insensitivity, which means that the matching should be done regardless of the case of the letters in the text and the keywords.
The problem also involves categorical classification, where we have multiple categories (sports, tech, food, and other) and we need to assign the text to one of these categories based on the presence of specific keywords. The order of checking the categories is important, as we need to return the first matching category. This type of classification is a simple example of a rule-based system, where the classification is based on a set of predefined rules (in this case, the presence of specific keywords).
Algorithm/Approach
The general approach to solve this type of problem involves the following steps:
- Preprocessing the text to normalize it (e.g., converting to lowercase)
- Tokenizing the text into individual words
- Checking each word against the keywords for each category
- Returning the first matching category
This approach can be implemented using a simple linear search algorithm, where we iterate through the text and check each word against the keywords.
Step-by-Step Strategy
To implement the solution, we can follow these steps:
- Convert the input text to lowercase to ensure case-insensitive matching.
- Split the text into individual words (tokenization).
- Define the keywords for each category (sports, tech, food).
- Iterate through the words in the text and check each word against the keywords for each category.
- Return the first matching category (sports → tech → food).
- If no keywords match, return "other".
Common Pitfalls
Some common pitfalls to watch out for when implementing this solution include:
- Not handling case-insensitivity correctly
- Not checking for whole word matches
- Not following the correct order of checking categories
- Not handling punctuation next to keywords (e.g., "team," or "team.")
Time & Space Complexity
The expected time complexity of this solution is O(n), where n is the number of words in the text, since we are iterating through the words in the text once. The space complexity is also O(n), since we need to store the words in the text and the keywords for each category. However, the space complexity can be reduced by using a more efficient data structure, such as a set or dictionary, to store the keywords.