Reputation: 145
I have a file with text like this:
4 abiogenezele
5 abiogenezelor
6 acefaliile
7 acefaliilor
8 acetonuriile
....
It have the format (ID_WORD WORD) and have an aproxim. ~33000 words.
I want for input a word to find his ID.
I try this code. It work but is not efficient.
int ID;
String word = "acefaliile";
String pattern = "(?i)([\\d]+) ("+word+")";
Pattern r = Pattern.compile(pattern);
boolean found = false;
// Read the file
try (BufferedReader br = new BufferedReader(new FileReader("./resources/txt/lemma.txt"))) {
String line;
while ((line = br.readLine()) != null) {
Matcher m = r.matcher(line);
if (m.find( )) {
// m.group(1) is ID
// m.group(2) is WORD
ID = Integer.parseInt(m.group(1));
found=true;
break;
}
}
if(!found) {
ID = 0;
}
}
Upvotes: 1
Views: 63
Reputation: 1
You can use Lookeen as file search program! This tool automatically searches in the content of files and you have a lot of filtering options.
Disclaimer: I work for the developer of Lookeen.
Upvotes: 0
Reputation: 80325
Alternative way - put these key/value pairs in array or list, sort it, and use binary search.
Upvotes: 1