Adrian Simionescu
Adrian Simionescu

Reputation: 145

Best method for search into file

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

Answers (2)

Axonic
Axonic

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

MBo
MBo

Reputation: 80325

  • Load the file in memory.
  • Divide lines by the space (the first space, if 'words' can contain spaces)
  • Load data into map (dictionary) data structure (HashMap in Java?) with word as key and id as value.
  • Do searches in this map.

Alternative way - put these key/value pairs in array or list, sort it, and use binary search.

Upvotes: 1

Related Questions