The goal of this project was to calculate the semantic similarity of terms in documents, specifically with a gene_*_gene pattern.
- Apache Spark
- Scala.
-
Get the totalNumberOfDocuments count
-
Flatmap each document and split the words in the document by whitespace We chose this because we realized that we can get all of the information necessary for the tf-idf calculation in one step, optimizing and preventing reprocessing of each line.
-
Filter the terms to match the gene format and map each term to the key as (term, documentID, totalWordsInDocument) , 1. This is the wordcount problem.
-
MAP : (term, documentID, totalWordsInDocument) => 1
-
We reduce this by key to get the total wordCount for that term per document
-
REDUCE: (term, documentID, totalWordsInDocument) => wordCountForTerm
-
ReMAP this such that : term => (documentID, totalWordsInDocument, wordCountForTerm) which is the rest of the info we need to calculate the tf-idf
-
With the term being the new key, we can group by the key (REDUCE) to get a list of the proper values
-
We map this such that we calculate a list of tf-idfs for each term for the proper documents
-
MAP : Term => Iterable(documentID, tf-idf)
-
We take the Cartesian of the two in order to get the pairs of terms but filter it by lexicographical difference to avoid duplicates
-
We map each pair to their cosineSimilarity, pairNames and filter out those genes with 0 similarity since they don’t give us much useful information and speeds up the program
-
MAP : cosineSimilarity => (term1, term2)
-
Sort by the cosine similarity in descending order. This also would allow us to group pairs with the same cosineSimilarity if we chose to do so.
More details can be seen in the algorithm design file
Program takes in one argument which is the file of documents