Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Google's <a href="http://code.google.com/p/guava-libraries/" rel="nofollow">Guava Libraries</a> have some very useful utilities and data structures for this sort of problem.</p> <p>To split up the file into words you can use Splitter:</p> <pre><code>Iterable&lt;String&gt; wordsInFile = Splitter.on(' ').trimResuls().omitEmptyStrings().split(fileAsString);</code></pre> <p>To count up the occurrences of a given word, you can use Multiset:</p> <pre><code>Multiset&lt;String&gt; countOfEachWord = HashMultiset.create(); countOfEachWord.addAll(wordsInFile);</code></pre> <p>You could build on these two pieces to create some kind of object like a WordLookupTable. ie:</p> <pre><code>public class WordLookupTable { private static final Splitter SPLITTER = Splitter.on(' ').trimResults().omitEmptyStrings(); private final Map&lt;String, Multiset&lt;String&gt;&gt; filenameToWordCountSet = Maps.newHashMap(); public void addFile(String filename, String fileText) { Multiset&lt;String&gt; wordsInFile = getWordSetForFile(filename); for (String word : SPLITTER.split(fileText)) { wordsInFile.add(word); } } // Gets the count of all words for the file public long getCountOfWordsForFile(String filename) { return getWordSetForFile(filename).size(); } public long getCountOfWordInFile(String filename, String word) { return getWordSetForFile(filename).count(word); } public long getCountOfWordOverAllFiles(String word) { long count = 0; for (Multiset&lt;String&gt; wordSet : filenameToWordCountSet.values()) { count += wordSet.count(word); } return count; } private Multiset&lt;String&gt; getWordSetForFile(String filename) { Multiset&lt;String&gt; wordsInFile = filenameToWordCountSet.get(filename); if(wordsInFile == null) { wordsInFile = HashMultiset.create(); filenameToWordCountSet.put(filename, wordsInFile); } return wordsInFile; } }</code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload