Format: application/octet-stream
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "---\n",
8 "\n",
9 "_You are currently looking at **version 1.1** of this notebook. To download notebooks and datafiles, as well as get help on Jupyter notebooks in the Coursera platform, visit the [Jupyter Notebook FAQ](https://www.coursera.org/learn/python-text-mining/resources/d9pwm) course resource._\n",
10 "\n",
11 "---"
12 ]
13 },
14 {
15 "cell_type": "markdown",
16 "metadata": {},
17 "source": [
18 "# Assignment 1\n",
19 "\n",
20 "In this assignment, you'll be working with messy medical data and using regex to extract relevant infromation from the data. \n",
21 "\n",
22 "Each line of the `dates.txt` file corresponds to a medical note. Each note has a date that needs to be extracted, but each date is encoded in one of many formats.\n",
23 "\n",
24 "The goal of this assignment is to correctly identify all of the different date variants encoded in this dataset and to properly normalize and sort the dates. \n",
25 "\n",
26 "Here is a list of some of the variants you might encounter in this dataset:\n",
27 "* 04/20/2009; 04/20/09; 4/20/09; 4/3/09\n",
28 "* Mar-20-2009; Mar 20, 2009; March 20, 2009; Mar. 20, 2009; Mar 20 2009;\n",
29 "* 20 Mar 2009; 20 March 2009; 20 Mar. 2009; 20 March, 2009\n",
30 "* Mar 20th, 2009; Mar 21st, 2009; Mar 22nd, 2009\n",
31 "* Feb 2009; Sep 2009; Oct 2010\n",
32 "* 6/2008; 12/2009\n",
33 "* 2009; 2010\n",
34 "\n",
35 "Once you have extracted these date patterns from the text, the next step is to sort them in ascending chronological order accoring to the following rules:\n",
36 "* Assume all dates in xx/xx/xx format are mm/dd/yy\n",
37 "* Assume all dates where year is encoded in only two digits are years from the 1900's (e.g. 1/5/89 is January 5th, 1989)\n",
38 "* If the day is missing (e.g. 9/2009), assume it is the first day of the month (e.g. September 1, 2009).\n",
39 "* If the month is missing (e.g. 2010), assume it is the first of January of that year (e.g. January 1, 2010).\n",
40 "* Watch out for potential typos as this is a raw, real-life derived dataset.\n",
41 "\n",
42 "With these rules in mind, find the correct date in each note and return a pandas Series in chronological order of the original Series' indices.\n",
43 "\n",
44 "For example if the original series was this:\n",
45 "\n",
46 " 0 1999\n",
47 " 1 2010\n",
48 " 2 1978\n",
49 " 3 2015\n",
50 " 4 1985\n",
51 "\n",
52 "Your function should return this:\n",
53 "\n",
54 " 0 2\n",
55 " 1 4\n",
56 " 2 0\n",
57 " 3 1\n",
58 " 4 3\n",
59 "\n",
60 "Your score will be calculated using [Kendall's tau](https://en.wikipedia.org/wiki/Kendall_rank_correlation_coefficient), a correlation measure for ordinal data.\n",
61 "\n",
62 "*This function should return a Series of length 500 and dtype int.*"
63 ]
64 },
65 {
66 "cell_type": "code",
67 "execution_count": 1,
68 "metadata": {},
69 "outputs": [
70 {
71 "data": {
72 "text/plain": [
73 "0 03/25/93 Total time of visit (in minutes):\\n\n",
74 "1 6/18/85 Primary Care Doctor:\\n\n",
75 "2 sshe plans to move as of 7/8/71 In-Home Servic...\n",
76 "3 7 on 9/27/75 Audit C Score Current:\\n\n",
77 "4 2/6/96 sleep studyPain Treatment Pain Level (N...\n",
78 "5 .Per 7/06/79 Movement D/O note:\\n\n",
79 "6 4, 5/18/78 Patient's thoughts about current su...\n",
80 "7 10/24/89 CPT Code: 90801 - Psychiatric Diagnos...\n",
81 "8 3/7/86 SOS-10 Total Score:\\n\n",
82 "9 (4/10/71)Score-1Audit C Score Current:\\n\n",
83 "dtype: object"
84 ]
85 },
86 "execution_count": 1,
87 "metadata": {},
88 "output_type": "execute_result"
89 }
90 ],
91 "source": [
92 "import pandas as pd\n",
93 "\n",
94 "doc = []\n",
95 "with open('dates.txt') as file:\n",
96 " for line in file:\n",
97 " doc.append(line)\n",
98 "\n",
99 "df = pd.Series(doc)\n",
100 "df.head(10)"
101 ]
102 },
103 {
104 "cell_type": "code",
105 "execution_count": 2,
106 "metadata": {
107 "collapsed": true
108 },
109 "outputs": [],
110 "source": [
111 "def date_sorter():\n",
112 " \n",
113 " regex1 = '(\\d{1,2}[/-]\\d{1,2}[/-]\\d{2,4})'\n",
114 " regex2 = '((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\\S]*[+\\s]\\d{1,2}[,]{0,1}[+\\s]\\d{4})'\n",
115 " regex3 = '(\\d{1,2}[+\\s](?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\\S]*[+\\s]\\d{4})'\n",
116 " regex4 = '((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\\S]*[+\\s]\\d{4})'\n",
117 " regex5 = '(\\d{1,2}[/-][1|2]\\d{3})'\n",
118 " regex6 = '([1|2]\\d{3})'\n",
119 " full_regex = '(%s|%s|%s|%s|%s|%s)' %(regex1, regex2, regex3, regex4, regex5, regex6)\n",
120 " parsed_date = df.str.extract(full_regex)\n",
121 " parsed_date = parsed_date.iloc[:,0].str.replace('Janaury', 'January').str.replace('Decemeber', 'December')\n",
122 " parsed_date = pd.Series(pd.to_datetime(parsed_date))\n",
123 " parsed_date = parsed_date.sort_values(ascending=True).index\n",
124 " return pd.Series(parsed_date.values)"
125 ]
126 },
127 {
128 "cell_type": "code",
129 "execution_count": 3,
130 "metadata": {},
131 "outputs": [
132 {
133 "name": "stderr",
134 "output_type": "stream",
135 "text": [
136 "/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:10: FutureWarning: currently extract(expand=None) means expand=False (return Index/Series/DataFrame) but in a future version of pandas this will be changed to expand=True (return DataFrame)\n",
137 " # Remove the CWD from sys.path while we load stuff.\n"
138 ]
139 },
140 {
141 "data": {
142 "text/plain": [
143 "0 9\n",
144 "1 84\n",
145 "2 2\n",
146 "3 53\n",
147 "4 28\n",
148 "5 474\n",
149 "6 153\n",
150 "7 13\n",
151 "8 129\n",
152 "9 98\n",
153 "10 111\n",
154 "11 225\n",
155 "12 31\n",
156 "13 171\n",
157 "14 191\n",
158 "15 486\n",
159 "16 335\n",
160 "17 415\n",
161 "18 36\n",
162 "19 405\n",
163 "20 323\n",
164 "21 422\n",
165 "22 375\n",
166 "23 380\n",
167 "24 345\n",
168 "25 57\n",
169 "26 481\n",
170 "27 436\n",
171 "28 104\n",
172 "29 299\n",
173 " ... \n",
174 "470 220\n",
175 "471 208\n",
176 "472 243\n",
177 "473 139\n",
178 "474 320\n",
179 "475 383\n",
180 "476 244\n",
181 "477 286\n",
182 "478 480\n",
183 "479 431\n",
184 "480 279\n",
185 "481 198\n",
186 "482 381\n",
187 "483 463\n",
188 "484 366\n",
189 "485 439\n",
190 "486 255\n",
191 "487 401\n",
192 "488 475\n",
193 "489 257\n",
194 "490 152\n",
195 "491 235\n",
196 "492 464\n",
197 "493 253\n",
198 "494 427\n",
199 "495 231\n",
200 "496 141\n",
201 "497 186\n",
202 "498 161\n",
203 "499 413\n",
204 "Length: 500, dtype: int64"
205 ]
206 },
207 "execution_count": 3,
208 "metadata": {},
209 "output_type": "execute_result"
210 }
211 ],
212 "source": [
213 "date_sorter()"
214 ]
215 }
216 ],
217 "metadata": {
218 "coursera": {
219 "course_slug": "python-text-mining",
220 "graded_item_id": "LvcWI",
221 "launcher_item_id": "krne9",
222 "part_id": "Mkp1I"
223 },
224 "kernelspec": {
225 "display_name": "Python 3",
226 "language": "python",
227 "name": "python3"
228 },
229 "language_info": {
230 "codemirror_mode": {
231 "name": "ipython",
232 "version": 3
233 },
234 "file_extension": ".py",
235 "mimetype": "text/x-python",
236 "name": "python",
237 "nbconvert_exporter": "python",
238 "pygments_lexer": "ipython3",
239 "version": "3.6.2"
240 }
241 },
242 "nbformat": 4,
243 "nbformat_minor": 2
244 }
245