Text Feature Extractor
Extract numerical features from text for classification.
Given a text, compute these features:
- word_count — number of words
- char_count — number of characters (excluding spaces)
- avg_word_length — average word length, rounded to 4 decimal places
- vocab_richness — number of unique words / total words, rounded to 4 decimal places
- longest_word — length of the longest word
- digit_ratio — fraction of characters that are digits (of all non-space chars), rounded to 4 decimal places
Input: A single line of text (may contain letters, digits, spaces)
Output: A dictionary with the six features, printed with keys in the order listed above.
Example
hello world hello 123
{'word_count': 4, 'char_count': 17, 'avg_word_length': 4.25, 'vocab_richness': 0.75, 'longest_word': 5, 'digit_ratio': 0.1765}word_count: 4 words: ["hello", "world", "hello", "123"]
char_count: h-e-l-l-o-w-o-r-l-d-h-e-l-l-o-1-2-3 = 17 chars (no spaces)
avg_word_length: (5 + 5 + 5 + 3) / 4 = 18/4 = 4.25
vocab_richness: 3 unique words / 4 total = 0.75
longest_word: max(5, 5, 5, 3) = 5
digit_ratio: 3 digits out of 17 non-space chars = 3/17 = 0.1765
Constraints
- Words are split on whitespace
- Character count excludes spaces
- Word comparisons for vocab_richness are case-sensitive
- Round float values to 4 decimal places