Created
August 15, 2017 08:38
-
-
Save bowbowbow/37e0729d92374fadf3408db4002b02f6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
#include <string> | |
#include <map> | |
#include <algorithm> | |
#include <ctime> | |
using namespace std; | |
typedef long long ll; | |
class RatingParser{ | |
private: | |
ifstream fin; | |
ofstream fout; | |
clock_t start; | |
const string mcFileName = "mc.txt"; | |
const string ratingFileName = "rating.txt"; | |
const string outputFileName = "rating_weight.txt"; | |
public: | |
map<ll, ll> contentToUser; | |
map<ll, int> ratingWeight; | |
map<pair<ll, ll>, pair<int, int> > userToUserRating; // key: {useri, userj}, value: {rating sum, rating count} | |
void run () { | |
clockInit("mc parsing"); | |
fin.open(mcFileName); | |
parseMC(); | |
fin.close(); | |
printDurating("mc parse"); | |
clockInit("rating parsing"); | |
fin.open(ratingFileName); | |
parseRating(); | |
fin.close(); | |
printDurating("rating parsing"); | |
clockInit("make output"); | |
fout.open(outputFileName); | |
makeOutput(); | |
fout.close(); | |
printDurating("make output"); | |
} | |
void clockInit(string name) { | |
cout << "[" << name << "] :: Start" << endl; | |
start = clock(); | |
} | |
void printDurating(string name) { | |
cout << "["<<name << "] :: End / duration : " << (clock() - start ) / (double) CLOCKS_PER_SEC << endl; | |
} | |
void parseMC() { | |
string str; | |
while(!getline(fin, str).eof()){ | |
vector<string> row; | |
int pre = 0; | |
for(int i=0;i<str.size();i++){ | |
if(str[i] == '|') { | |
string col = str.substr(pre, i-pre); | |
row.push_back(col); | |
pre = i+1; | |
} | |
} | |
ll contentId = stoll(row[0]); | |
ll userId = stoll(row[1]); | |
contentToUser.insert({contentId, userId}); | |
} | |
} | |
void parseRating() { | |
string str; | |
while(!getline(fin, str).eof()){ | |
vector<string> row; | |
int pre = 0; | |
for(int i=0;i<str.size();i++){ | |
if(str[i] == '\t') { | |
string col = str.substr(pre, i-pre); | |
row.push_back(col); | |
pre = i+1; | |
} | |
} | |
ll contentId = stoll(row[0]); | |
ll userId = stoll(row[1]); | |
int rating = stoi(row[2]); | |
ll authorId = contentToUser[contentId]; | |
auto iter = userToUserRating.find({userId, authorId}); | |
if(iter==userToUserRating.end()) { | |
pair<ll, ll> key = {userId, authorId}; | |
pair<int, int> value = {rating, 1}; | |
userToUserRating.insert({key, value}); | |
} else { | |
auto value = iter->second; | |
value.first += rating; | |
value.second++; | |
iter->second = value; | |
} | |
// rating weight 계산을 위해서 | |
ratingWeight[userId]++; | |
} | |
} | |
void makeOutput() { | |
for(auto iter=userToUserRating.begin(); iter != userToUserRating.end(); iter++) { | |
ll userI = iter->first.first; | |
ll userJ = iter->first.second; | |
int weight = ratingWeight[userI]; | |
fout << userI<< " " << userJ<< " "; | |
// fout << iter->second.first << " " << iter->second.second << endl; | |
fout << weight << endl; | |
} | |
} | |
}; | |
int main() { | |
RatingParser ratingParser; | |
ratingParser.run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment