Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Class with the functionality required for Ü9.
- class WordCounter {
- // Read file with given name.
- void readFile(String fileName);
- // Update counts per word (wordCounts below) for given string of words.
- void updateWordCounts(Array<String> words);
- // Sort words by counts, and return the k words with the largest counts.
- Array<Pair<String,Integer>> computeFrequentWords(Integer k);
- // Word counts.
- Map<String, Integer> wordCounts;
- }
- void StlDemo::readMovieInfo(std::string fileName) {
- std::ifstream file(fileName.c_str());
- std::string line;
- while (true) {
- std::getline(file, line);
- if (file.eof()) break;
- // Parse line into title, year, popularity.
- size_t pos1 = line.find("t");
- if (pos1 == std::string::npos) continue;
- size_t pos2 = line.find("t", pos1 + 1);
- if (pos2 == std::string::npos) continue;
- Movie movie;
- movie.title = line.substr(0, pos1);
- movie.year = atoi(line.substr(pos1 + 1, pos2 - pos1).c_str());
- movie.popularity = atoi(line.substr(pos2 + 1).c_str());
- // Insert into map.
- moviesByYear[movie.year].push_back(movie);
- // std::cout << """ << movie.title << "", "
- // << movie.year << ", " << movie.popularity << std::endl;
- }
- }
- // ___________________________________________________
- void StlDemo::sortByPopularity() {
- for (auto& pair : moviesByYear) {
- auto& movies = pair.second;
- std::sort(movies.begin(), movies.end(), [](const Movie& x, const Movie& y)
- { return x.popularity > y.popularity; });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement