edu.northwestern.at.wordhoard.swing.calculator.modelutils
Class CountMapUtils

java.lang.Object
  extended by edu.northwestern.at.wordhoard.swing.calculator.modelutils.CountMapUtils

public class CountMapUtils
extends java.lang.Object

Count map utilities.

Count maps have a String or Spelling key representing items to count and Number values as the counts. In many cases, the Number values are integers, but this is not necessarily the case. For example, a count map may have word spellings as keys and scaled word frequencies (taking on values between 0.0 and 1.0) as Number values.


Constructor Summary
protected CountMapUtils()
          Don't allow instantiation but do allow overrides.
 
Method Summary
static void addCountMap(java.util.Map destinationMap, java.util.Map sourceMap)
          Add words/counts from one map to another.
static java.util.Map booleanizeCountMap(java.util.Map map)
          Convert map values to integer 1 or 0.
static java.util.Map convertKeysToStrings(java.util.Map countMap)
          Convert keys in count map to plain strings.
static java.util.Map deepClone(java.util.Map countMap)
          Get deep clone of a count map.
static int getCountOfWordsInCommon(java.util.Map countMap1, java.util.Map countMap2)
          Get count of words which two count maps share.
static java.util.Map getCountsFromFile(java.io.File file)
          Get the word counts from a file.
static java.util.Map getCountsFromFile(java.lang.String fileName)
          Get the word counts from a file.
static java.util.Map getCountsFromReader(java.io.Reader reader)
          Get the word counts from a reader.
static java.util.Map getCountsFromString(java.lang.String countsString)
          Get the word counts from a string.
static double[] getSummaryCountsFromCountMap(java.util.Map map)
          Get summary counts from a count map.
static double getSumOfCrossProducts(java.util.Map countMap1, java.util.Map countMap2)
          Get sum of cross products for counts in two maps.
static int getTotalWordCount(java.util.Map map)
          Get total count of words in map.
static int getWordCount(java.util.Map countMap, java.lang.Object word)
          Get count for a specific word form from a count map.
static java.util.Set getWordsFromMap(java.util.Map map)
          Get words from a map.
static java.util.List getWordsInCommon(java.util.Map countMap1, java.util.Map countMap2)
          Get list of words which two count maps share.
static void incrementCountMap(java.util.Map destinationMap, java.util.Map sourceMap)
          Increment words/counts in one map from another.
static void saveCountsToFile(java.util.Map countsMap, java.io.File file)
          Save the word counts to a file.
static void saveCountsToFile(java.util.Map countsMap, java.lang.String fileName)
          Save the word counts to a file.
static java.lang.String saveCountsToString(java.util.Map countsMap)
          Save the word counts to a string.
static void saveCountsToWriter(java.util.Map countsMap, java.io.PrintWriter printWriter)
          Save the keyed counts to a file.
static java.util.Map scaleCountMap(java.util.Map map, double scaleFactor)
          Scale count entries in count map.
static java.lang.String[] splitKeyedCountString(java.lang.String s)
          Split string at tab character.
static void subtractCountMap(java.util.Map destinationMap, java.util.Map sourceMap)
          Subtract words/counts in one map from another.
static void updateWordCountMap(java.lang.Object word, int count, java.util.Map countMap)
          Updates counts for a word in a map.
static void updateWorkMap(java.lang.Object word, java.lang.Long workId, java.util.Map workMap)
          Updates works for a word in a map.
static void worksToWorkCounts(java.util.Map map)
          Convert work map to work count map.
static java.util.Map worksToWorkCounts(java.util.Map[] maps)
          Convert multiple work maps to a single work count map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CountMapUtils

protected CountMapUtils()
Don't allow instantiation but do allow overrides.

Method Detail

getSummaryCountsFromCountMap

public static double[] getSummaryCountsFromCountMap(java.util.Map map)
Get summary counts from a count map.

Parameters:
map - The map with string keys and Number counts as values.
Returns:
Three entry double array. result[ 0 ] = sum of counts result[ 1 ] = sum of squared counts result[ 2 ] = unique count (size of map)

getTotalWordCount

public static int getTotalWordCount(java.util.Map map)
Get total count of words in map.

Parameters:
map - The map with string keys and Number counts as values.
Returns:
Sum of counts as an integer.

getSumOfCrossProducts

public static double getSumOfCrossProducts(java.util.Map countMap1,
                                           java.util.Map countMap2)
Get sum of cross products for counts in two maps.

Parameters:
countMap1 - First count map.
countMap2 - Second count map.
Returns:
sum of cross products as a double.

booleanizeCountMap

public static java.util.Map booleanizeCountMap(java.util.Map map)
Convert map values to integer 1 or 0.

Parameters:
map - Count map to booleanize.
Returns:
New map containing booleanized count values.

Non-zero counts are converted to integer 1, 0 counts are converted to integer 0.


scaleCountMap

public static java.util.Map scaleCountMap(java.util.Map map,
                                          double scaleFactor)
Scale count entries in count map.

Parameters:
map - The count map.
scaleFactor - The double value by which to multiply each count value in the count map.
Returns:
Map with same keys as input map and counts scaled using scaleFactor.

getWordsFromMap

public static java.util.Set getWordsFromMap(java.util.Map map)
Get words from a map.

Parameters:
map - The map with keys and Number counts as values.
Returns:
Keys as a set.

worksToWorkCounts

public static void worksToWorkCounts(java.util.Map map)
Convert work map to work count map.

Parameters:
map - The map with string keys and HashSet of work IDs as values.

On return, map is modified to have work counts in place of hash set for each key, e.g., the hash set is replaced by its size.


worksToWorkCounts

public static java.util.Map worksToWorkCounts(java.util.Map[] maps)
Convert multiple work maps to a single work count map.

Parameters:
maps - The map withs string keys and HashSet of work IDs as values.
Returns:
Map with words as keys and work counts as values. The counts represent the unique work counts.

splitKeyedCountString

public static java.lang.String[] splitKeyedCountString(java.lang.String s)
Split string at tab character.

Parameters:
s - The string to split into a key and a count.
Returns:
Two element string array with the key and count.

getCountsFromReader

public static java.util.Map getCountsFromReader(java.io.Reader reader)
                                         throws java.io.IOException
Get the word counts from a reader.

Parameters:
reader - The reader.
Throws:
java.io.IOException

getCountsFromFile

public static java.util.Map getCountsFromFile(java.io.File file)
                                       throws java.io.IOException
Get the word counts from a file.

Parameters:
file - The input file.
Throws:
java.io.IOException

getCountsFromString

public static java.util.Map getCountsFromString(java.lang.String countsString)
                                         throws java.io.IOException
Get the word counts from a string.

Parameters:
countsString - The input string.
Throws:
java.io.IOException

getCountsFromFile

public static java.util.Map getCountsFromFile(java.lang.String fileName)
                                       throws java.io.IOException
Get the word counts from a file.

Parameters:
fileName - The input file name.
Throws:
java.io.IOException

saveCountsToWriter

public static void saveCountsToWriter(java.util.Map countsMap,
                                      java.io.PrintWriter printWriter)
                               throws java.io.IOException
Save the keyed counts to a file.

Parameters:
countsMap - The map containing keyed counts.
printWriter - The printWriter specifying the output file.

Each key is output on a separate line followed by a tab and the count.

Example:

aardvark\t25
abacus\t10
...

Throws:
java.io.IOException

saveCountsToFile

public static void saveCountsToFile(java.util.Map countsMap,
                                    java.io.File file)
                             throws java.io.IOException
Save the word counts to a file.

Parameters:
countsMap - The map containing keyed counts.
file - The output file.
Throws:
java.io.IOException

saveCountsToFile

public static void saveCountsToFile(java.util.Map countsMap,
                                    java.lang.String fileName)
                             throws java.io.IOException
Save the word counts to a file.

Parameters:
countsMap - The map containing keyed counts.
fileName - The output file name.
Throws:
java.io.IOException

saveCountsToString

public static java.lang.String saveCountsToString(java.util.Map countsMap)
                                           throws java.io.IOException
Save the word counts to a string.

Parameters:
countsMap - The map containing keyed counts.
Throws:
java.io.IOException

addCountMap

public static void addCountMap(java.util.Map destinationMap,
                               java.util.Map sourceMap)
Add words/counts from one map to another.

Parameters:
destinationMap - Destination map.
sourceMap - Source map.

On output, the destination map is updated with words and counts from the source map.


incrementCountMap

public static void incrementCountMap(java.util.Map destinationMap,
                                     java.util.Map sourceMap)
Increment words/counts in one map from another.

Parameters:
destinationMap - Destination map.
sourceMap - Source map.

On output, the destination map counts are incremented by one for each word appearing in the source map. If a source word does not already appear in the destination, it is added with a count of one.


subtractCountMap

public static void subtractCountMap(java.util.Map destinationMap,
                                    java.util.Map sourceMap)
Subtract words/counts in one map from another.

Parameters:
destinationMap - Destination map.
sourceMap - Source map.

On output, the destination map counts are updated by removing the counts for matching words from the source map. If the count goes to zero for any word in the destination, that word is removed from from the destination map.


getWordsInCommon

public static java.util.List getWordsInCommon(java.util.Map countMap1,
                                              java.util.Map countMap2)
Get list of words which two count maps share.

Parameters:
countMap1 - First count map.
countMap2 - Second count map.
Returns:
List of words appearing in both maps.

getCountOfWordsInCommon

public static int getCountOfWordsInCommon(java.util.Map countMap1,
                                          java.util.Map countMap2)
Get count of words which two count maps share.

Parameters:
countMap1 - First count map.
countMap2 - Second count map.
Returns:
Count of words appearing in both maps.

getWordCount

public static int getWordCount(java.util.Map countMap,
                               java.lang.Object word)
Get count for a specific word form from a count map.

Parameters:
countMap - The word count map.
word - The word text.
Returns:
The count for the specified word. 0 if the word does not occur.

updateWordCountMap

public static void updateWordCountMap(java.lang.Object word,
                                      int count,
                                      java.util.Map countMap)
Updates counts for a word in a map.

Parameters:
word - The word.
count - The word count.
countMap - The word count map.

updateWorkMap

public static void updateWorkMap(java.lang.Object word,
                                 java.lang.Long workId,
                                 java.util.Map workMap)
Updates works for a word in a map.

Parameters:
word - The word.
workId - The work ID.
workMap - The map containing work IDs for the word.

deepClone

public static java.util.Map deepClone(java.util.Map countMap)
Get deep clone of a count map.

Parameters:
countMap - The count map to clone.
Returns:
Deep clone of the count map.

convertKeysToStrings

public static java.util.Map convertKeysToStrings(java.util.Map countMap)
Convert keys in count map to plain strings.

Parameters:
countMap - The count map whose keys should be converted to strings.
Returns:
The count map with keys converted to plain strings.

Each key element's toString() method is called to convert the key object to a plain text string. Key elements without a toString() method will not be added to the result map. The object values (counts) are left untouched. Note that no key should be null. Null keys will be ignored.