Class: Cosine

Cosine()

Class representing cosine similarity calculations.

Constructor

new Cosine()

Source:

Methods

(static) addKeysToDict(map, dict)

Adds all keys from a map to a dictionary, ensuring a consistent set of terms for vector comparison.

Parameters:
Name Type Description
map object

The map containing keys to add to the dictionary.

dict object

The dictionary to add the keys to.

Source:

(static) calculateSimilarity(vecA, vecB) → {number}

Calculates the cosine similarity between two vectors using their dot product and magnitudes.

Parameters:
Name Type Description
vecA array

The first vector for comparison.

vecB array

The second vector for comparison.

Source:
Returns:
  • The cosine similarity between the two vectors, ranging from 0.0 (no similarity) to 1.0 (perfect similarity).
Type
number

(static) similarity(strA, strB) → {number}

Calculates the cosine similarity between two strings based on their term frequency vectors.

Parameters:
Name Type Description
strA string

The first string for comparison.

strB string

The second string for comparison.

Source:
Returns:
  • The cosine similarity between the two strings, ranging from 0.0 (no similarity) to 1.0 (perfect similarity).
Type
number

(static) termFreqMap(str) → {object}

Generates a term frequency map, counting the occurrences of each word in the input string.

Parameters:
Name Type Description
str string

The input string to analyze.

Source:
Returns:
  • A term frequency map with words as keys and their frequencies as values.
Type
object

(static) termFreqMapToVector(map, dict) → {array}

Converts a term frequency map into a term frequency vector, using a common dictionary for consistent order.

Parameters:
Name Type Description
map object

The term frequency map to convert.

dict object

The dictionary of terms to use for vector alignment.

Source:
Returns:
  • A term frequency vector representing the word frequencies of the input string.
Type
array

(static) vecDotProduct(vecA, vecB) → {number}

Calculates the dot product of two vectors, representing the sum of their element-wise multiplications.

Parameters:
Name Type Description
vecA array

The first vector for the dot product.

vecB array

The second vector for the dot product.

Source:
Returns:
  • The dot product of the two vectors.
Type
number

(static) vecMagnitude(vec) → {number}

Calculates the magnitude (Euclidean length) of a vector.

Parameters:
Name Type Description
vec array

The vector for which to calculate the magnitude.

Source:
Returns:
  • The magnitude of the vector.
Type
number