Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions data
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <iomanip>
class Movie {
public:
std::string title;
std::vector<std::string> genres;
std::vector<int> ratings;

Movie() {} // Adding a default constructor

Movie(std::string t) : title(t) {}

void addRating(int rating) {
ratings.push_back(rating);
}

void addGenre(const std::string& genre) {
genres.push_back(genre);
}

double getAverageRating() const {
if (ratings.empty()) return 0.0;
double sum = 0;
for (int rating : ratings) {
sum += rating;
}
return sum / ratings.size();
}
};


void loadMovies(std::unordered_map<int, Movie>& movies, const std::string& filename) {
std::ifstream file(filename);
std::string line;
while (getline(file, line)) {
std::istringstream iss(line);
std::string segment;
std::vector<std::string> segments;
while (getline(iss, segment, '|')) {
segments.push_back(segment);
}
if (segments.size() >= 24) {
int movieId = std::stoi(segments[0]);
movies[movieId] = Movie(segments[1]); // Movie ID and Title
// Parse genres, which are from segments[5] to segments[23]
static const std::vector<std::string> genreNames = {"Unknown", "Action", "Adventure", "Animation",
"Children's", "Comedy", "Crime", "Documentary", "Drama",
"Fantasy", "Film-Noir", "Horror", "Musical", "Mystery",
"Romance", "Sci-Fi", "Thriller", "War", "Western"};
for (int i = 0; i < 19; ++i) {
if (segments[5 + i] == "1") {
movies[movieId].addGenre(genreNames[i]);
}
}
}
}
}

void loadRatings(std::unordered_map<int, Movie>& movies, const std::string& filename) {
std::ifstream file(filename);
std::string line;
while (getline(file, line)) {
std::istringstream iss(line);
int userId, movieId, rating, timestamp;
iss >> userId >> movieId >> rating >> timestamp;
if (movies.find(movieId) != movies.end()) {
movies[movieId].addRating(rating);
}
}
}

int main() {
std::unordered_map<int, Movie> movies;

loadMovies(movies, "C:/Users/myakh/Downloads/archive/ml-100k/u.data");
loadRatings(movies, "C:/Users/myakh/Downloads/archive/ml-100k/u.data");

int count = 0;
for (const auto& pair : movies) {
if (count >= 5) break;
std::cout << "Movie: " << pair.second.title
<< ", Genres: ";
for (const auto& genre : pair.second.genres) {
std::cout << genre << " ";
}
std::cout << ", Average Rating: " << std::fixed << std::setprecision(2) << pair.second.getAverageRating()
<< std::endl;
++count;
}
return 0;
}
15 changes: 15 additions & 0 deletions link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pandas as pd

if __name__ == '__main__':
#loading in the csv file from the path
df = pd.read_csv(r"C:\Users\myakh\PycharmProjects\DSA Project 3\Fraudulent_E-Commerce_Transaction_Data.csv")

#selecting the columns by index
#they are indexed as one less than the actual column number
selected_columns = df.iloc[:, [2, 4, 13, 14]]

#displaying the isolated/separated columns
print(selected_columns)

#writing the columns to a new txt file
selected_columns.to_csv(r"C:\Users\myakh\PycharmProjects\DSA Project 3\new commerce data.txt", index=False)
44 changes: 44 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

//splitting up the token data created in python
vector<string> split(const string &s, char delimiter) {
vector<string> tokens; //new vector that stores and splits up the string
string token; //a temporary string that holds each split part
istringstream tokenStream(s); //creating a string stream from the input string
while (getline(tokenStream, token, delimiter)) { //extracting strings split by the delimiter
tokens.push_back(token);
}
return tokens;
}

//reading and printing the CSV data
void readAndPrintCSV(const string &filePath) {
ifstream file(filePath); //opens the file stream with given file path
string line;

if (!file.is_open()) {
cerr << "Failed to open file\n";
return;
}

while (getline(file, line)) {
vector<string> tokens = split(line, ','); //splitting each line by the comma
for (const string& token : tokens) {
cout << token << " "; //printing each token followed by a space to ensure its saved correctly
}
cout << endl;
}

file.close();
}

int main() {
string filePath = R"(C:\Users\myakh\PycharmProjects\DSA Project 3\new commerce data.txt)";
readAndPrintCSV(filePath);
return 0;
}
Loading