File size: 14,335 Bytes
0928f44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install sentence-transformers==2.0.0"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://www.kaggle.com/datasets/dataranch/upwork-1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Load dataset with pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('freelancers.csv')\n",
    "df = df[['shortName', 'title', 'description', 'location', 'hourlyRate', 'avgFeedbackScore', 'skills']]\n",
    "df = df.dropna(subset='skills')\n",
    "df = df.dropna(subset='title')\n",
    "df['location'] = df['location'].apply(lambda x : eval(x)['state'])\n",
    "df['skills'] = df['skills'].apply(lambda x : [x['skill']['name'] for x in eval(x)])\n",
    "df['hourlyRate'] = df['hourlyRate'].apply(lambda x : eval(x)['amount'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def keyword_extractor(total_keywords, str1):\n",
    "    word_list = list()\n",
    "    for keyword in total_keywords:\n",
    "        for word in str1.split(' '):\n",
    "            if word == keyword:\n",
    "                word_list.append(word)\n",
    "    return word_list\n",
    "total_keywords = df.explode('skills')['skills'].unique().tolist()\n",
    "\n",
    "skill_keywords = keyword_extractor(total_keywords, 'I want to hire a wordpress')\n",
    "df[df['skills'].apply(lambda x: all(val in x for val in skill_keywords))]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Encode 100 samples into vectors (1 column with product text, 1 column with vectors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 5815/5815 [12:10<00:00,  7.96it/s]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>shortName</th>\n",
       "      <th>title</th>\n",
       "      <th>description</th>\n",
       "      <th>location</th>\n",
       "      <th>hourlyRate</th>\n",
       "      <th>avgFeedbackScore</th>\n",
       "      <th>skills</th>\n",
       "      <th>text_vector_</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Jason V.</td>\n",
       "      <td>Expert WordPress Developer</td>\n",
       "      <td>Hello! Welcome to my profile!\\n\\nMy name is Ja...</td>\n",
       "      <td>IL</td>\n",
       "      <td>60.00</td>\n",
       "      <td>4.925208</td>\n",
       "      <td>[wordpress, seo, wp-ecommerce, woocommerce, bo...</td>\n",
       "      <td>[0.078628771007061, 0.024731114506721497, -0.0...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Miranda S.</td>\n",
       "      <td>Social Media Manager</td>\n",
       "      <td>I am a Social Media Manager who specializes in...</td>\n",
       "      <td>NY</td>\n",
       "      <td>20.00</td>\n",
       "      <td>4.675676</td>\n",
       "      <td>[social-media-content-creation, video-editing,...</td>\n",
       "      <td>[0.07423530519008636, -0.022386642172932625, -...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Gagan S J.</td>\n",
       "      <td>Solution Architect</td>\n",
       "      <td>More than 25 years in IT with 20 years in US h...</td>\n",
       "      <td>NJ</td>\n",
       "      <td>65.00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>[oracle-java-ee, spring-framework, hibernate, ...</td>\n",
       "      <td>[0.04637446999549866, 0.03554175794124603, -0....</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Roxana L.</td>\n",
       "      <td>Procurement, Logistics and Supply Chain profes...</td>\n",
       "      <td>I work FAST - I am COST EFFICIENT - I deliver ...</td>\n",
       "      <td>NC</td>\n",
       "      <td>70.00</td>\n",
       "      <td>4.916684</td>\n",
       "      <td>[procurement-function, pharmaceutical-industry...</td>\n",
       "      <td>[0.026502298191189766, -0.02052873745560646, -...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>John M.</td>\n",
       "      <td>3d generalist, post production artist and Vide...</td>\n",
       "      <td>I am a 3D artist, animator, and designer with ...</td>\n",
       "      <td>OK</td>\n",
       "      <td>50.00</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>[animation, motion-graphics, video-editing, vi...</td>\n",
       "      <td>[0.05356863886117935, 0.032190944999456406, -0...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5818</th>\n",
       "      <td>Megan D.</td>\n",
       "      <td>Web Research/Content Curation/Data Entry/Socia...</td>\n",
       "      <td>Extremely driven, yet easy-going gal who refus...</td>\n",
       "      <td>CA</td>\n",
       "      <td>22.37</td>\n",
       "      <td>4.574029</td>\n",
       "      <td>[data-entry, internet-research, virtual-assist...</td>\n",
       "      <td>[0.07812528312206268, -0.018792806193232536, -...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5819</th>\n",
       "      <td>Austin V.</td>\n",
       "      <td>Product Manager</td>\n",
       "      <td>Experienced in building and growing digital pl...</td>\n",
       "      <td>AZ</td>\n",
       "      <td>100.00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>[atlassian-jira, atlassian-confluence, project...</td>\n",
       "      <td>[0.056266412138938904, -0.007661229465156794, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5820</th>\n",
       "      <td>Eric M.</td>\n",
       "      <td>Web Developer/Designer And Graphic Designer</td>\n",
       "      <td>4 YEARS experience in  Wordpress / Shopify / D...</td>\n",
       "      <td>OH</td>\n",
       "      <td>25.00</td>\n",
       "      <td>4.451507</td>\n",
       "      <td>[html, css, wordpress, shopify, joomla, drupal...</td>\n",
       "      <td>[0.048749279230833054, -0.013894445262849331, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5821</th>\n",
       "      <td>Kristina A.</td>\n",
       "      <td>Voice Over Talent, video editing, video produc...</td>\n",
       "      <td>A musician from birth. I studied music educati...</td>\n",
       "      <td>VA</td>\n",
       "      <td>60.00</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>[articulate]</td>\n",
       "      <td>[0.03207482025027275, -0.027680398896336555, -...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5822</th>\n",
       "      <td>Ashley C.</td>\n",
       "      <td>Print and Digital Layout Designer</td>\n",
       "      <td>Hello, I'm Ashley, a professional designer and...</td>\n",
       "      <td>OR</td>\n",
       "      <td>25.00</td>\n",
       "      <td>4.951186</td>\n",
       "      <td>[print-layout-design, brochure-design, flyer-d...</td>\n",
       "      <td>[0.04141489043831825, -0.04500063508749008, -0...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5815 rows Γ— 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        shortName                                              title  \\\n",
       "0        Jason V.                         Expert WordPress Developer   \n",
       "1      Miranda S.                               Social Media Manager   \n",
       "2      Gagan S J.                                 Solution Architect   \n",
       "3       Roxana L.  Procurement, Logistics and Supply Chain profes...   \n",
       "4         John M.  3d generalist, post production artist and Vide...   \n",
       "...           ...                                                ...   \n",
       "5818     Megan D.  Web Research/Content Curation/Data Entry/Socia...   \n",
       "5819    Austin V.                                    Product Manager   \n",
       "5820      Eric M.        Web Developer/Designer And Graphic Designer   \n",
       "5821  Kristina A.  Voice Over Talent, video editing, video produc...   \n",
       "5822    Ashley C.                  Print and Digital Layout Designer   \n",
       "\n",
       "                                            description location  hourlyRate  \\\n",
       "0     Hello! Welcome to my profile!\\n\\nMy name is Ja...       IL       60.00   \n",
       "1     I am a Social Media Manager who specializes in...       NY       20.00   \n",
       "2     More than 25 years in IT with 20 years in US h...       NJ       65.00   \n",
       "3     I work FAST - I am COST EFFICIENT - I deliver ...       NC       70.00   \n",
       "4     I am a 3D artist, animator, and designer with ...       OK       50.00   \n",
       "...                                                 ...      ...         ...   \n",
       "5818  Extremely driven, yet easy-going gal who refus...       CA       22.37   \n",
       "5819  Experienced in building and growing digital pl...       AZ      100.00   \n",
       "5820  4 YEARS experience in  Wordpress / Shopify / D...       OH       25.00   \n",
       "5821  A musician from birth. I studied music educati...       VA       60.00   \n",
       "5822  Hello, I'm Ashley, a professional designer and...       OR       25.00   \n",
       "\n",
       "      avgFeedbackScore                                             skills  \\\n",
       "0             4.925208  [wordpress, seo, wp-ecommerce, woocommerce, bo...   \n",
       "1             4.675676  [social-media-content-creation, video-editing,...   \n",
       "2             0.000000  [oracle-java-ee, spring-framework, hibernate, ...   \n",
       "3             4.916684  [procurement-function, pharmaceutical-industry...   \n",
       "4             5.000000  [animation, motion-graphics, video-editing, vi...   \n",
       "...                ...                                                ...   \n",
       "5818          4.574029  [data-entry, internet-research, virtual-assist...   \n",
       "5819          0.000000  [atlassian-jira, atlassian-confluence, project...   \n",
       "5820          4.451507  [html, css, wordpress, shopify, joomla, drupal...   \n",
       "5821          5.000000                                       [articulate]   \n",
       "5822          4.951186  [print-layout-design, brochure-design, flyer-d...   \n",
       "\n",
       "                                           text_vector_  \n",
       "0     [0.078628771007061, 0.024731114506721497, -0.0...  \n",
       "1     [0.07423530519008636, -0.022386642172932625, -...  \n",
       "2     [0.04637446999549866, 0.03554175794124603, -0....  \n",
       "3     [0.026502298191189766, -0.02052873745560646, -...  \n",
       "4     [0.05356863886117935, 0.032190944999456406, -0...  \n",
       "...                                                 ...  \n",
       "5818  [0.07812528312206268, -0.018792806193232536, -...  \n",
       "5819  [0.056266412138938904, -0.007661229465156794, ...  \n",
       "5820  [0.048749279230833054, -0.013894445262849331, ...  \n",
       "5821  [0.03207482025027275, -0.027680398896336555, -...  \n",
       "5822  [0.04141489043831825, -0.04500063508749008, -0...  \n",
       "\n",
       "[5815 rows x 8 columns]"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from tqdm import tqdm\n",
    "from sentence_transformers import SentenceTransformer\n",
    "tqdm.pandas()\n",
    "\n",
    "model = SentenceTransformer('all-mpnet-base-v2') #all-MiniLM-L6-v2 #all-mpnet-base-v2\n",
    "\n",
    "#encode df version: for small dataset only\n",
    "df['text_vector_'] = df['title'].progress_apply(lambda x : model.encode(x).tolist())\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "df = df.drop_duplicates(subset=['shortName', 'location', 'title', 'hourlyRate', 'avgFeedbackScore', 'description'])\n",
    "df = df.reset_index(drop=True)\n",
    "df['location'] = df['location'].apply(lambda x : random.choice(['New York', 'Chicago', 'Washington']))\n",
    "df.to_parquet('df_encoded.parquet', index=None)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.0 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "fdf377d643bc1cb065454f0ad2ceac75d834452ecf289e7ba92c6b3f59a7cee1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}