-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenizer.java
More file actions
37 lines (29 loc) · 905 Bytes
/
Tokenizer.java
File metadata and controls
37 lines (29 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
/**
* Tokenizer converts raw text into clean searchable tokens.
*/
public class Tokenizer {
/**
* Converts text into a list of normalized words.
* Example:
* "Java is GREAT!" -> ["java", "is", "great"]
*/
public static List<String> tokenize(String text) {
if (text == null || text.isEmpty()) {
return Collections.emptyList();
}
// Convert to lowercase
text = text.toLowerCase();
// Remove punctuation & special characters
text = text.replaceAll("[^a-z ]", " ");
// Split by whitespace
String[] tokens = text.split("\\s+");
List<String> words = new ArrayList<>();
for (String token : tokens) {
if (!token.isEmpty()) {
words.add(token);
}
}
return words;
}
}