Created
January 10, 2016 16:05
-
-
Save schon/6f57ab74ec7351a6d0cd to your computer and use it in GitHub Desktop.
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
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