Create movielens.py
Browse files- movielens.py +90 -0
movielens.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
import os
|
4 |
+
from functools import partial
|
5 |
+
from pathlib import Path
|
6 |
+
from typing import Dict, Iterable
|
7 |
+
|
8 |
+
import datasets
|
9 |
+
from datasets import DatasetDict, DownloadManager, load_dataset
|
10 |
+
import pandas as pd
|
11 |
+
|
12 |
+
|
13 |
+
AVAILABLE_DATASETS = {
|
14 |
+
'small': 'https://files.grouplens.org/datasets/movielens/ml-latest-small.zip',
|
15 |
+
'full': 'https://files.grouplens.org/datasets/movielens/ml-latest.zip',
|
16 |
+
}
|
17 |
+
|
18 |
+
|
19 |
+
VERSION = datasets.Version("0.0.1")
|
20 |
+
|
21 |
+
|
22 |
+
class MovielensDataset(datasets.GeneratorBasedBuilder):
|
23 |
+
"""MovielensDataset dataset."""
|
24 |
+
|
25 |
+
BUILDER_CONFIGS = [
|
26 |
+
datasets.BuilderConfig(
|
27 |
+
name=data_name, version=VERSION, description=f"{data_name} movielens dataset"
|
28 |
+
)
|
29 |
+
for data_name in AVAILABLE_DATASETS
|
30 |
+
]
|
31 |
+
|
32 |
+
def _info(self) -> datasets.DatasetInfo:
|
33 |
+
return datasets.DatasetInfo(
|
34 |
+
description="",
|
35 |
+
features=datasets.Features(
|
36 |
+
{
|
37 |
+
"movieId": datasets.Value("string"),
|
38 |
+
"title": datasets.Value("string"),
|
39 |
+
"genres": datasets.Sequence(datasets.Value("string")),
|
40 |
+
"tag": datasets.Sequence(datasets.Value("string")),
|
41 |
+
}
|
42 |
+
),
|
43 |
+
supervised_keys=None,
|
44 |
+
homepage="https://grouplens.org/datasets/movielens/latest/",
|
45 |
+
citation="",
|
46 |
+
)
|
47 |
+
|
48 |
+
def _split_generators(
|
49 |
+
self, dl_manager: DownloadManager
|
50 |
+
) -> Iterable[datasets.SplitGenerator]:
|
51 |
+
downloader = partial(
|
52 |
+
lambda split: dl_manager.download_and_extract(
|
53 |
+
AVAILABLE_DATASETS[self.config.name]
|
54 |
+
)
|
55 |
+
)
|
56 |
+
|
57 |
+
folder = os.path.splitext(os.path.basename(AVAILABLE_DATASETS[self.config.name]))[
|
58 |
+
0
|
59 |
+
]
|
60 |
+
|
61 |
+
# There is no predefined train/val/test split for this dataset.
|
62 |
+
return [
|
63 |
+
datasets.SplitGenerator(
|
64 |
+
name=datasets.Split.TRAIN,
|
65 |
+
gen_kwargs={
|
66 |
+
"root_path": downloader("train"),
|
67 |
+
"split": "train",
|
68 |
+
"folder": folder,
|
69 |
+
},
|
70 |
+
),
|
71 |
+
]
|
72 |
+
|
73 |
+
def _generate_examples(
|
74 |
+
self, root_path: str, split: str, folder: str
|
75 |
+
) -> Iterable[Dict]:
|
76 |
+
split_path = Path(root_path) / folder
|
77 |
+
movies_file = split_path / "movies.csv"
|
78 |
+
movies = pd.read_csv(movies_file, sep=',', encoding='utf-8')
|
79 |
+
movies['genres'] = movies['genres'].str.split('|')
|
80 |
+
tags_file = split_path / "tags.csv"
|
81 |
+
tags = pd.read_csv(tags_file, sep=',', encoding='utf-8')
|
82 |
+
tags = tags.groupby('movieId').agg({'tag': list}).reset_index()
|
83 |
+
movies = movies.merge(tags, on='movieId')
|
84 |
+
for idx, row in movies.iterrows():
|
85 |
+
yield idx, {
|
86 |
+
'movieId': row['movieId'],
|
87 |
+
'title': row['title'],
|
88 |
+
'genres': row['genres'],
|
89 |
+
'tag': row['tag'],
|
90 |
+
}
|