luketheduke
commited on
Commit
•
155a6de
1
Parent(s):
c41679a
Upload create_dataset.ipynb
Browse files- create_dataset.ipynb +123 -0
create_dataset.ipynb
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import pandas as pd\n",
|
10 |
+
"import glob"
|
11 |
+
]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"cell_type": "code",
|
15 |
+
"execution_count": 8,
|
16 |
+
"metadata": {},
|
17 |
+
"outputs": [],
|
18 |
+
"source": [
|
19 |
+
"path = 'custrev.*'\n",
|
20 |
+
"text, label_text, label = [], [], []\n",
|
21 |
+
"for file in glob.glob(path):\n",
|
22 |
+
" with open(file, 'r') as f:\n",
|
23 |
+
" cr, sentiment = file.split('.')\n",
|
24 |
+
" \n",
|
25 |
+
" if sentiment == 'pos':\n",
|
26 |
+
" lab_text = 'positive'\n",
|
27 |
+
" lab = 1\n",
|
28 |
+
"\n",
|
29 |
+
" else:\n",
|
30 |
+
" lab_text = 'negative'\n",
|
31 |
+
" lab = 0\n",
|
32 |
+
"\n",
|
33 |
+
" \n",
|
34 |
+
" for line in f:\n",
|
35 |
+
" if line.strip():\n",
|
36 |
+
" text.append(line.rstrip())\n",
|
37 |
+
" label_text.append(lab_text)\n",
|
38 |
+
" label.append(lab)"
|
39 |
+
]
|
40 |
+
},
|
41 |
+
{
|
42 |
+
"cell_type": "code",
|
43 |
+
"execution_count": 3,
|
44 |
+
"metadata": {},
|
45 |
+
"outputs": [],
|
46 |
+
"source": [
|
47 |
+
"df = pd.DataFrame({'text':text, 'label':label, 'label_text':label_text}).drop_duplicates()"
|
48 |
+
]
|
49 |
+
},
|
50 |
+
{
|
51 |
+
"cell_type": "code",
|
52 |
+
"execution_count": 4,
|
53 |
+
"metadata": {},
|
54 |
+
"outputs": [],
|
55 |
+
"source": [
|
56 |
+
"test = df.sample(frac=0.2, random_state=42)"
|
57 |
+
]
|
58 |
+
},
|
59 |
+
{
|
60 |
+
"cell_type": "code",
|
61 |
+
"execution_count": 5,
|
62 |
+
"metadata": {},
|
63 |
+
"outputs": [],
|
64 |
+
"source": [
|
65 |
+
"train = df.drop(test.index)"
|
66 |
+
]
|
67 |
+
},
|
68 |
+
{
|
69 |
+
"cell_type": "code",
|
70 |
+
"execution_count": 6,
|
71 |
+
"metadata": {},
|
72 |
+
"outputs": [],
|
73 |
+
"source": [
|
74 |
+
"with open('test.jsonl', 'w') as f:\n",
|
75 |
+
" f.write(test.to_json(orient='records', lines=True))\n"
|
76 |
+
]
|
77 |
+
},
|
78 |
+
{
|
79 |
+
"cell_type": "code",
|
80 |
+
"execution_count": 7,
|
81 |
+
"metadata": {},
|
82 |
+
"outputs": [],
|
83 |
+
"source": [
|
84 |
+
"with open('train.jsonl', 'w') as f:\n",
|
85 |
+
" f.write(train.to_json(orient='records', lines=True))"
|
86 |
+
]
|
87 |
+
},
|
88 |
+
{
|
89 |
+
"cell_type": "code",
|
90 |
+
"execution_count": null,
|
91 |
+
"metadata": {},
|
92 |
+
"outputs": [],
|
93 |
+
"source": []
|
94 |
+
}
|
95 |
+
],
|
96 |
+
"metadata": {
|
97 |
+
"kernelspec": {
|
98 |
+
"display_name": "Python 3.9.7 ('base')",
|
99 |
+
"language": "python",
|
100 |
+
"name": "python3"
|
101 |
+
},
|
102 |
+
"language_info": {
|
103 |
+
"codemirror_mode": {
|
104 |
+
"name": "ipython",
|
105 |
+
"version": 3
|
106 |
+
},
|
107 |
+
"file_extension": ".py",
|
108 |
+
"mimetype": "text/x-python",
|
109 |
+
"name": "python",
|
110 |
+
"nbconvert_exporter": "python",
|
111 |
+
"pygments_lexer": "ipython3",
|
112 |
+
"version": "3.9.7"
|
113 |
+
},
|
114 |
+
"orig_nbformat": 4,
|
115 |
+
"vscode": {
|
116 |
+
"interpreter": {
|
117 |
+
"hash": "f4f7bf0ead705f9496960dd44ed8785939deb14cc456821001bcb47e882c9346"
|
118 |
+
}
|
119 |
+
}
|
120 |
+
},
|
121 |
+
"nbformat": 4,
|
122 |
+
"nbformat_minor": 2
|
123 |
+
}
|