-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Search for keyword in the given context
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
/** | ||
* Keyword-in-context search. | ||
* | ||
* Computes all of the occurrences of a keyword in a string, with surrounding | ||
* context. | ||
*/ | ||
public class KWIK { | ||
|
||
/** | ||
* Reads the contents of the specified file and returns them as a string. | ||
* | ||
* @param filePath the path to the input file | ||
*/ | ||
private static String readFileAsString(String filePath) { | ||
File f = new File(filePath); | ||
BufferedReader reader = null; | ||
StringBuilder text = new StringBuilder(); | ||
String line = null; | ||
|
||
try { | ||
reader = new BufferedReader(new FileReader(f)); | ||
while ((line = reader.readLine()) != null) { | ||
text.append(line); | ||
} | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
if (reader != null) { | ||
reader.close(); | ||
} | ||
} catch (IOException e) { | ||
} | ||
} | ||
|
||
return text.toString(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
String text = readFileAsString("./data/brave_new_world.txt"); | ||
// int context = Integer.parseInt(args[0]); | ||
// String query = args[1]; | ||
|
||
// number of characters of surrounding context in each side | ||
int context = 20; | ||
|
||
// query string | ||
String query = "world"; | ||
|
||
int length = text.length(); | ||
|
||
// build suffix array | ||
SuffixArray sa = new SuffixArray(text); | ||
|
||
for (int i = sa.rank(query); i < length && sa.selectAsString(i).startsWith(query); i++) { | ||
int from = Math.max(0, sa.indexOf(i) - context); | ||
int to = Math.min(length-1, from + query.length() + 2*context); | ||
System.out.println(text.substring(from, to)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters