Skip to content

Instantly share code, notes, and snippets.

@schon
Created January 10, 2016 16:05
Show Gist options
  • Save schon/6f57ab74ec7351a6d0cd to your computer and use it in GitHub Desktop.
Save schon/6f57ab74ec7351a6d0cd to your computer and use it in GitHub Desktop.
import java.util.*;
public class InvertIndexer {
private Map<String, Set<String>> invertedIndex = new HashMap<>();
public void index(String[][] inputSet) {
for (String[] input : inputSet) {
String word = input[0];
Set<String> words;
for (int i = 1; i < input.length; i++) {
String tag = input[i];
if ((words = invertedIndex.get(tag)) == null) {
words = new HashSet<>();
}
words.add(word);
invertedIndex.put(tag, words);
}
}
System.out.println(invertedIndex);
}
public Set<String> searchByTags(String[] input) {
Set<String> allTags = new HashSet<>();
for (String tag : input) {
Set<String> tags = this.invertedIndex.get(tag);
if (tags != null) {
if (allTags.isEmpty()) {
allTags = tags;
} else {
allTags.retainAll(tags);
}
}
}
return allTags;
}
public static void main(String[] args) {
// create invert index
// the first element is the word and the rest of elements are tags
String[][] inputSet = {
new String[]{"구글", "안드로이드", "검색", "모바일"},
new String[]{"애플", "iOS", "모바일", "맥북"}
};
InvertIndexer indexer = new InvertIndexer();
indexer.index(inputSet);
// search by tags
String testInputSet[][] = {
new String[]{"iOS", "모바일"},
new String[]{"모바일"},
new String[]{"Windows"}
};
for (String[] input : testInputSet) {
System.out.println(Arrays.asList(input) + " => " + indexer.searchByTags(input));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment