/* My custom algorithm */ int custom (int user, int movie) { // Declare and initialize variable to hold calculated rating int rating = 0; // Calculate sub-ratings using the other five algorithms int pearsonStandardRating = pearsonStandard (user, movie); int pearsonCaseRating = pearsonCase (user, movie); int pearsonInverseRating = pearsonInverse (user, movie); int vectorRating = vector (user, movie); int cosineSimilarityRating = cosineSimilarity (user, movie); // Average the sub-ratings together because I'm lazy and don't feel like writing more code rating = (pearsonStandardRating + pearsonCaseRating + pearsonInverseRating + vectorRating + cosineSimilarityRating) / 5; // Return final calculated value return rating; }
void PowershellEventSubscriber::addScriptResult(Row& results) { Row r; r["time"] = results["time"]; r["datetime"] = results["datetime"]; r["script_block_id"] = results["ScriptBlockId"]; r["script_block_count"] = INTEGER(results["MessageTotal"]); r["script_name"] = results["Name"]; r["script_path"] = results["Path"]; r["script_text"] = results["ScriptBlockText"]; // Grab the feature vectors from the configuration auto parser = Config::getParser(kFeatureVectorsRootKey); if (parser == nullptr) { VLOG(1) << "Failed to get configured feature vectors"; add(r); return; } // Get the reassembled powershell scripts character frequency vector std::vector<double> freqs(kCharFreqVectorLen, 0.0); for (const auto chr : r["script_text"]) { if (chr < kCharFreqVectorLen) { freqs[chr] += 1.0 / r["script_text"].length(); } } const auto& cf = parser->getData().doc()["feature_vectors"]["character_frequencies"]; if (cf.Empty() || cf.Size() != kCharFreqVectorLen) { VLOG(1) << "Invalid character frequency map found, skipping computation"; add(r); return; } std::vector<double> cfg_freqs(kCharFreqVectorLen, 0.0); for (unsigned int i = 0; i < cf.Size(); i++) { cfg_freqs[i] = cf[i].GetDouble(); } r["cosine_similarity"] = DOUBLE(cosineSimilarity(freqs, cfg_freqs)); add(r); }
/* Serves as a switchboard to the chosen rating prediction algorithm */ void calculate (void) { /* Declare variables for use in functions */ // Counters int i, j; // Variable to hold calculated rating int rating; // Variable to hold which algorithm the user chooses for use in the switch loop int algorithm; /* Ask user which algorithm to use */ printf ("\nWhich algorithm would you like to use to predict ratings?\n"); printf ("1 - Pearson Correlation\n"); printf ("2 - Vector Similarity\n"); printf ("3 - Pearson Correlation - Inverse User Frequency\n"); printf ("4 - Pearson Correlation - Case Modification\n"); printf ("5 - Cosine Similarity\n"); printf ("6 - Custom Algorithm\n"); // Scan user answer scanf ("%d", &algorithm); /* Iterate through array to find ratings that need to be calculated */ // Start user counter at userOffset (starting user ID - 1) for (i=userOffset; i<userOffset+100; i++) { // Start movie counter at 0 (movie ID - 1) for (j=0; j<1000; j++) { // If the value stored for that user and ID is 0 (meaning a non-null 0, "null" being defined as 9) if (ratings[i][j] == 0) { // Extract the user ID and movie ID for use in the selected algorithm int user = i; int movie = j; /* Call the appropriate calculation function */ switch (algorithm) { // Input 1 means Pearson Correlation case 1: rating = pearsonStandard (user, movie); break; // Input 2 means Vector Similarity case 2: rating = vector (user, movie); break; // Input 3 means Inverse User Frequency using Pearson Correlation case 3: rating = pearsonInverse (user, movie); break; // Input 4 means Case Modification using Pearson Correlation case 4: rating = pearsonCase (user, movie); break; // Input 5 means Adjusted Cosine Similarity case 5: rating = cosineSimilarity (user, movie); break; // Input 6 means my custom algorithm case 6: rating = custom (user, movie); break; // If value is not within defined range, default to case 1 default: rating = pearsonStandard (user, movie); break; } // Conver user ID to user index and set that corresponding user/movie value to the calculated rating ratingResults[user-userOffset][movie] = rating; } } } }