Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. // Class with the functionality required for Ü9.
  2. class WordCounter {
  3.  
  4. // Read file with given name.
  5. void readFile(String fileName);
  6.  
  7. // Update counts per word (wordCounts below) for given string of words.
  8. void updateWordCounts(Array<String> words);
  9.  
  10. // Sort words by counts, and return the k words with the largest counts.
  11. Array<Pair<String,Integer>> computeFrequentWords(Integer k);
  12.  
  13. // Word counts.
  14. Map<String, Integer> wordCounts;
  15. }
  16.  
  17. void StlDemo::readMovieInfo(std::string fileName) {
  18. std::ifstream file(fileName.c_str());
  19. std::string line;
  20. while (true) {
  21. std::getline(file, line);
  22. if (file.eof()) break;
  23. // Parse line into title, year, popularity.
  24. size_t pos1 = line.find("t");
  25. if (pos1 == std::string::npos) continue;
  26. size_t pos2 = line.find("t", pos1 + 1);
  27. if (pos2 == std::string::npos) continue;
  28. Movie movie;
  29. movie.title = line.substr(0, pos1);
  30. movie.year = atoi(line.substr(pos1 + 1, pos2 - pos1).c_str());
  31. movie.popularity = atoi(line.substr(pos2 + 1).c_str());
  32. // Insert into map.
  33. moviesByYear[movie.year].push_back(movie);
  34. // std::cout << """ << movie.title << "", "
  35. // << movie.year << ", " << movie.popularity << std::endl;
  36. }
  37. }
  38.  
  39. // ___________________________________________________
  40. void StlDemo::sortByPopularity() {
  41. for (auto& pair : moviesByYear) {
  42. auto& movies = pair.second;
  43. std::sort(movies.begin(), movies.end(), [](const Movie& x, const Movie& y)
  44. { return x.popularity > y.popularity; });
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement