Created
June 12, 2018 07:00
-
-
Save takageymt/5f9e256a0f22d91589ce0943554a8188 to your computer and use it in GitHub Desktop.
integrated exercise for software i: ex08
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::vector<double> get_associations(const std::vector<Page>& pages, | |
const std::vector<Word>& words, | |
const std::vector<std::string>& keywords, | |
const bool normalizable) { | |
std::map<int, int> pid_conv; | |
for(int i = 0; i < static_cast<int>(pages.size()); ++i) { | |
pid_conv[pages[i].id()] = i; | |
} | |
std::map<std::string, int> word_conv; | |
for(int i = 0; i < static_cast<int>(words.size()); ++i) { | |
word_conv[words[i].str()] = i; | |
} | |
std::vector<double> associations(pages.size(), 0.0); | |
for(const std::string& keyword : keywords) { | |
if(!word_conv.count(keyword)) continue; | |
int widx = word_conv[keyword]; | |
std::set<int> pid_set; | |
for(const auto& location : words[widx].locations()) { | |
pid_set.insert(location.first); | |
} | |
for(int pid : pid_set) { | |
if(pid_conv.count(pid)) associations[pid_conv[pid]] += 1.0; | |
} | |
} | |
if(normalizable) { | |
double max_association = *std::max_element(associations.begin(), associations.end()); | |
if(max_association != 0.0) { | |
for(int i = 0; i < static_cast<int>(pages.size()); ++i) { | |
associations[i] /= max_association; | |
} | |
} | |
} | |
return associations; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment