diff --git a/.ipynb_checkpoints/Regex Homework-checkpoint.ipynb b/.ipynb_checkpoints/Regex Homework-checkpoint.ipynb new file mode 100644 index 0000000..5752ca6 --- /dev/null +++ b/.ipynb_checkpoints/Regex Homework-checkpoint.ipynb @@ -0,0 +1,534 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 112, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "[]\n", + "Length 3\n", + "raggggg hammer dog\n", + "0\n", + "[]\n", + "0\n", + "[]\n", + "0\n", + "[]\n", + "0\n", + "[]\n", + "1\n", + "[]\n", + "Length 1\n", + "hello\n", + "Smile True\n", + "2\n", + "[]\n", + "Length 2\n", + "hello world\n", + "3\n", + "[]\n", + "Length 3\n", + "raggggg hammer dog\n", + "2\n", + "[]\n", + "Length 2\n", + "18-wheeler tarbox\n", + "0\n", + "0\n", + "[]\n", + "2\n", + "['!', '!', '!']\n", + "2\n", + "['*', '!']\n", + "2\n", + "[]\n", + "Length 1\n", + "hello\n", + "3\n", + "[]\n", + "Length 2\n", + "hello world\n", + "1\n", + "[]\n", + "Length 3\n", + "raggggg hammer dog\n", + "3\n", + "[]\n", + "Length 2\n", + "18-wheeler tarbox\n" + ] + } + ], + "source": [ + "import re\n", + "def words(test, count=0):\n", + " print(count)\n", + " if len(test) == 0:\n", + " return False\n", + " checkforletter = re.findall(r\"[a-zA-Z]\", test)\n", + " search = re.findall(r\"[^\\d\\w\\-\\s]\", test)\n", + " if len(search) == 0 and len(checkforletter) > 0:\n", + " pass\n", + " print(search)\n", + " if len(search) > 0:\n", + " return False\n", + " if len(checkforletter) == 0:\n", + " return False\n", + " else:\n", + " if count == 0:\n", + " return True\n", + " else:\n", + " print(\"Length\", len(re.findall(r\"\\b[a-z]+\\b\", test)))\n", + " print(test)\n", + " if len(re.findall(r\"\\b[a-z]+\\b\", test)) == count:\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + "words(\"raggggg hammer dog\", count=3)\n", + "assert words(\"hello\")\n", + "assert words(\"hello world\")\n", + "assert words(\"raggggg hammer dog\")\n", + "assert words(\"18-wheeler tarbox\")\n", + "print(\"Smile \", words(\"hello\", count=1))\n", + "assert words(\"hello world\", count=2)\n", + "assert words(\"raggggg hammer dog\", count=3)\n", + "assert words(\"18-wheeler tarbox\", count=2)\n", + "assert not words(\"\")\n", + "assert not words(\"12\")\n", + "assert not words(\"hey !!!\", count=2)\n", + "assert not words(\"bar*us w!zard\", count=2)\n", + "assert not words(\"hello\", count=2)\n", + "assert not words(\"hello world\", count=3)\n", + "assert not words(\"raggggg hammer dog\", count=1)\n", + "assert not words(\"18-wheeler tarbox\", count=3)" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "919-555-1212\n", + "0\n", + "(919) 555-1212\n", + "0\n", + "9195551212\n", + "0\n", + "919.555.1212\n", + "0\n", + "919 555-1212\n", + "0\n", + "\n", + "0\n", + "555-121\n", + "0\n", + "1212\n", + "0\n", + "mobile\n", + "0\n" + ] + } + ], + "source": [ + "import re\n", + "def phone_number(test):\n", + " print(test)\n", + " print(len(re.findall(\"r[\\d]\", test)))\n", + " if len(re.findall(r\"[\\d]\", test)) == 10:\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + "\n", + "\n", + "assert phone_number(\"919-555-1212\")\n", + "assert phone_number(\"(919) 555-1212\")\n", + "assert phone_number(\"9195551212\")\n", + "assert phone_number(\"919.555.1212\")\n", + "assert phone_number(\"919 555-1212\")\n", + "assert not phone_number(\"\")\n", + "assert not phone_number(\"555-121\")\n", + "assert not phone_number(\"1212\")\n", + "assert not phone_number(\"mobile\")" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "\n", + "def money(test):\n", + " if len(test) < 2:\n", + " return False\n", + " if re.fullmatch(r\"\\$.*\\.\\d{1}|\\$.*\\.\\d{3}|\\${2}\\d.*|\\$\\d*,\\d{1,2}|\\d+\", test):\n", + " return False\n", + " else:\n", + " return True\n", + " \n", + "\n", + "\n", + "\n", + "assert money(\"$4\")\n", + "assert money(\"$19\")\n", + "assert money(\"$19.00\")\n", + "assert money(\"$3.58\")\n", + "assert money(\"$1000\")\n", + "assert money(\"$1000.00\")\n", + "assert money(\"$1,000\")\n", + "assert money(\"$1,000.00\")\n", + "assert money(\"$5,555,555\")\n", + "assert money(\"$5,555,555.55\")\n", + "assert money(\"$45,555,555.55\")\n", + "assert money(\"$456,555,555.55\")\n", + "assert money(\"$1234567.89\")\n", + "assert not money(\"\")\n", + "print(money(\"$12,34\"))\n", + "assert not money(\"$1234.9\")\n", + "assert not money(\"$1234.999\")\n", + "assert not money(\"$\")\n", + "assert not money(\"31\")\n", + "assert not money(\"$$31\")" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def words(test):\n", + " if len(re.findall(r\"[a-zA-Z]\", test)) > 0:\n", + " return test.split()\n", + "words(\"18-wheeler tarbox\")\n", + "words(\"12\")" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "(\"input,expected\", [\n", + " (\"hello\", ['hello']),\n", + " (\"hello world\", ['hello', 'world']),\n", + " (\"raggggg hammer dog\", ['raggggg', 'hammer', 'dog']),\n", + " (\"18-wheeler tarbox\", ['18-wheeler', 'tarbox']),\n", + " (\"12\", None),\n", + "])\n", + "def test_words(input, expected):\n", + " assert words(input) == expected\n" + ] + }, + { + "cell_type": "code", + "execution_count": 154, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8642525185\n" + ] + }, + { + "data": { + "text/plain": [ + "{'area_code': '864', 'number': '252-5185'}" + ] + }, + "execution_count": 154, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import re\n", + "def phone_number(test):\n", + " if len(re.findall(r\"[\\d]\", test)) == 10:\n", + " cleantext = ''.join((re.findall(r\"[\\d]\", test)))\n", + " print(cleantext)\n", + " area = re.findall(r\"^\\d{3}\", cleantext)\n", + " number = cleantext[3:6] + '-' + cleantext[6:]\n", + " return {\"area_code\": ''.join(area), \"number\": number}\n", + " \n", + "phone_number(\"864-252-5185\")" + ] + }, + { + "cell_type": "code", + "execution_count": 160, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "456555555.55\n" + ] + }, + { + "data": { + "text/plain": [ + "{'currency': '$', 'value': 456555555.55}" + ] + }, + "execution_count": 160, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def money(test):\n", + " if len(test) < 2:\n", + " return None\n", + " if re.fullmatch(r\"\\$.*\\.\\d{1}|\\$.*\\.\\d{3}|\\${2}\\d.*|\\$\\d*,\\d{1,2}|\\d+\", test):\n", + " return None\n", + " else:\n", + " cleantext = ''.join((re.findall(r\"[\\d\\.]\", test)))\n", + " print(cleantext)\n", + " return{\"currency\": \"$\", \"value\": float(cleantext)}\n", + "money(\"456,555,555.55\")" + ] + }, + { + "cell_type": "code", + "execution_count": 170, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "641004444\n" + ] + }, + { + "data": { + "text/plain": [ + "{'plus4': '4444', 'zip': '64100'}" + ] + }, + "execution_count": 170, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def zipcode(test):\n", + " if len(re.findall(r\"[\\d]\", test)) == 9 or len(re.findall(r\"[\\d]\", test)) == 5:\n", + " cleantext = ''.join((re.findall(r\"[\\d]\", test)))\n", + " print(cleantext)\n", + " zip = re.findall(r\"^\\d{5}\", cleantext)\n", + " number = cleantext[5:]\n", + " if number == '':\n", + " number = None\n", + " else:\n", + " number = ''.join(number)\n", + " return {\"zip\": ''.join(zip), \"plus4\": number}\n", + "zipcode(\"64100-4444\")" + ] + }, + { + "cell_type": "code", + "execution_count": 242, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['9/4/1976']\n", + "9\n", + "{'day': '4', 'year': '1976', 'month': '9'}\n", + "2015-01-01\n" + ] + }, + { + "data": { + "text/plain": [ + "{'day': '01', 'month': '01', 'year': '2015'}" + ] + }, + "execution_count": 242, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import re\n", + "def date(test):\n", + " if len(re.findall(r\"\\d.*/\\d.*/\\d.*|\\d.*-\\d.*-\\d.*\", test)):\n", + " if len(re.findall(r\"[/]\", test)) == 2:\n", + " date = re.findall(r\"\\d*/\\d*/\\d*\", test)\n", + " print(date)\n", + " month = ''.join(re.findall(r\"^\\d{1,2}\", test))\n", + " print(month)\n", + " day = re.findall(r\"/\\d{1,2}.\", test)\n", + " day = ''.join(day)[1:-1]\n", + " year = ''.join(re.findall(r\"/\\d{4}\", test))\n", + " year = year[1:]\n", + " return {\"month\": month, \"day\": day, \"year\": year}\n", + " if len(re.findall(r\"[-]\", test)) == 2:\n", + " date = ''.join(re.findall(r\"\\d*-\\d*-\\d*\", test))\n", + " \n", + " print(date)\n", + " \n", + " month = ''.join(re.findall(r\"-\\d{1,2}-\", test))\n", + " month = month[1:-1]\n", + " \n", + " day = date[8:]\n", + " \n", + " year = ''.join(re.findall(r\"\\d{4}\", test))\n", + " \n", + " return {\"month\": month, \"day\": day, \"year\": year}\n", + " else:\n", + " return None\n", + "print(date(\"9/4/1976\"))\n", + "date(\"2015-01-01\")" + ] + }, + { + "cell_type": "code", + "execution_count": 247, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def phone_numbers(text):\n", + " return re.findall(r\"\\(\\d{3}\\).\\d{3}.\\d{4}\", text)\n", + "phone_numbers(text)\n", + "assert phone_numbers(text) == [\"(454) 999-1212\", \"(919) 123-4569\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 243, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + " text = \"\"\"Dear Mr. Davis,\n", + "\n", + "I got to know of your company through our mutual friend Fiona Williams and the\n", + "training you offer to graduate students in Advertising.\n", + "\n", + "I am a graduate student of Mass Communications with specialization in\n", + "Advertising. I am currently pursuing the last year of my course.\n", + "I would very much like to see firsthand the work environment in an advertising\n", + "agency.\n", + "\n", + "If you would like a reference, my advisor can be reached at (454) 999-1212.\n", + "\n", + "You can contact me at (919) 123-4569 at your convenience.\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 254, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 254, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def emails(text):\n", + " return re.findall(r\"[a-z\\.]*\\@[a-z\\.]*\", text)\n", + "\n", + "\n", + "text = \"\"\"Veggies es bonus vobis, proinde vos postulo essum magis kohlrabi\n", + "welsh onion daikon amaranth@gmail.com tatsoi tomatillo azuki bean garlic.\n", + "\n", + "Gumbo beet greens corn soko endive gumbo gourd. Parsley shallot courgette\n", + "tatsoi pea@sprouts.org fava bean collard greens dandelion okra wakame\n", + "tomato. Dandelion cucumber.earthnut@pea.net peanut soko zucchini.\"\"\"\n", + "\n", + "# assert emails(text) == [\"amaranth@gmail.com\",\n", + " # \"pea@sprouts.org\",\n", + " #\"cucumber.earthnut@pea.net\"]\n", + "emails(text)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/README.md b/README.md index f208010..b7b584d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +Learned how to create and use Regular Expressions # Text Miner ## Description diff --git a/Regex Homework.ipynb b/Regex Homework.ipynb new file mode 100644 index 0000000..6ec5148 --- /dev/null +++ b/Regex Homework.ipynb @@ -0,0 +1,534 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 112, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "[]\n", + "Length 3\n", + "raggggg hammer dog\n", + "0\n", + "[]\n", + "0\n", + "[]\n", + "0\n", + "[]\n", + "0\n", + "[]\n", + "1\n", + "[]\n", + "Length 1\n", + "hello\n", + "Smile True\n", + "2\n", + "[]\n", + "Length 2\n", + "hello world\n", + "3\n", + "[]\n", + "Length 3\n", + "raggggg hammer dog\n", + "2\n", + "[]\n", + "Length 2\n", + "18-wheeler tarbox\n", + "0\n", + "0\n", + "[]\n", + "2\n", + "['!', '!', '!']\n", + "2\n", + "['*', '!']\n", + "2\n", + "[]\n", + "Length 1\n", + "hello\n", + "3\n", + "[]\n", + "Length 2\n", + "hello world\n", + "1\n", + "[]\n", + "Length 3\n", + "raggggg hammer dog\n", + "3\n", + "[]\n", + "Length 2\n", + "18-wheeler tarbox\n" + ] + } + ], + "source": [ + "import re\n", + "def words(test, count=0):\n", + " print(count)\n", + " if len(test) == 0:\n", + " return False\n", + " checkforletter = re.findall(r\"[a-zA-Z]\", test)\n", + " search = re.findall(r\"[^\\d\\w\\-\\s]\", test)\n", + " if len(search) == 0 and len(checkforletter) > 0:\n", + " pass\n", + " print(search)\n", + " if len(search) > 0:\n", + " return False\n", + " if len(checkforletter) == 0:\n", + " return False\n", + " else:\n", + " if count == 0:\n", + " return True\n", + " else:\n", + " print(\"Length\", len(re.findall(r\"\\b[a-z]+\\b\", test)))\n", + " print(test)\n", + " if len(re.findall(r\"\\b[a-z]+\\b\", test)) == count:\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + "words(\"raggggg hammer dog\", count=3)\n", + "assert words(\"hello\")\n", + "assert words(\"hello world\")\n", + "assert words(\"raggggg hammer dog\")\n", + "assert words(\"18-wheeler tarbox\")\n", + "print(\"Smile \", words(\"hello\", count=1))\n", + "assert words(\"hello world\", count=2)\n", + "assert words(\"raggggg hammer dog\", count=3)\n", + "assert words(\"18-wheeler tarbox\", count=2)\n", + "assert not words(\"\")\n", + "assert not words(\"12\")\n", + "assert not words(\"hey !!!\", count=2)\n", + "assert not words(\"bar*us w!zard\", count=2)\n", + "assert not words(\"hello\", count=2)\n", + "assert not words(\"hello world\", count=3)\n", + "assert not words(\"raggggg hammer dog\", count=1)\n", + "assert not words(\"18-wheeler tarbox\", count=3)" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "919-555-1212\n", + "0\n", + "(919) 555-1212\n", + "0\n", + "9195551212\n", + "0\n", + "919.555.1212\n", + "0\n", + "919 555-1212\n", + "0\n", + "\n", + "0\n", + "555-121\n", + "0\n", + "1212\n", + "0\n", + "mobile\n", + "0\n" + ] + } + ], + "source": [ + "import re\n", + "def phone_number(test):\n", + " print(test)\n", + " print(len(re.findall(\"r[\\d]\", test)))\n", + " if len(re.findall(r\"[\\d]\", test)) == 10:\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + "\n", + "\n", + "assert phone_number(\"919-555-1212\")\n", + "assert phone_number(\"(919) 555-1212\")\n", + "assert phone_number(\"9195551212\")\n", + "assert phone_number(\"919.555.1212\")\n", + "assert phone_number(\"919 555-1212\")\n", + "assert not phone_number(\"\")\n", + "assert not phone_number(\"555-121\")\n", + "assert not phone_number(\"1212\")\n", + "assert not phone_number(\"mobile\")" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "\n", + "def money(test):\n", + " if len(test) < 2:\n", + " return False\n", + " if re.fullmatch(r\"\\$.*\\.\\d{1}|\\$.*\\.\\d{3}|\\${2}\\d.*|\\$\\d*,\\d{1,2}|\\d+\", test):\n", + " return False\n", + " else:\n", + " return True\n", + " \n", + "\n", + "\n", + "\n", + "assert money(\"$4\")\n", + "assert money(\"$19\")\n", + "assert money(\"$19.00\")\n", + "assert money(\"$3.58\")\n", + "assert money(\"$1000\")\n", + "assert money(\"$1000.00\")\n", + "assert money(\"$1,000\")\n", + "assert money(\"$1,000.00\")\n", + "assert money(\"$5,555,555\")\n", + "assert money(\"$5,555,555.55\")\n", + "assert money(\"$45,555,555.55\")\n", + "assert money(\"$456,555,555.55\")\n", + "assert money(\"$1234567.89\")\n", + "assert not money(\"\")\n", + "print(money(\"$12,34\"))\n", + "assert not money(\"$1234.9\")\n", + "assert not money(\"$1234.999\")\n", + "assert not money(\"$\")\n", + "assert not money(\"31\")\n", + "assert not money(\"$$31\")" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def words(test):\n", + " if len(re.findall(r\"[a-zA-Z]\", test)) > 0:\n", + " return test.split()\n", + "words(\"18-wheeler tarbox\")\n", + "words(\"12\")" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "(\"input,expected\", [\n", + " (\"hello\", ['hello']),\n", + " (\"hello world\", ['hello', 'world']),\n", + " (\"raggggg hammer dog\", ['raggggg', 'hammer', 'dog']),\n", + " (\"18-wheeler tarbox\", ['18-wheeler', 'tarbox']),\n", + " (\"12\", None),\n", + "])\n", + "def test_words(input, expected):\n", + " assert words(input) == expected\n" + ] + }, + { + "cell_type": "code", + "execution_count": 154, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8642525185\n" + ] + }, + { + "data": { + "text/plain": [ + "{'area_code': '864', 'number': '252-5185'}" + ] + }, + "execution_count": 154, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import re\n", + "def phone_number(test):\n", + " if len(re.findall(r\"[\\d]\", test)) == 10:\n", + " cleantext = ''.join((re.findall(r\"[\\d]\", test)))\n", + " print(cleantext)\n", + " area = re.findall(r\"^\\d{3}\", cleantext)\n", + " number = cleantext[3:6] + '-' + cleantext[6:]\n", + " return {\"area_code\": ''.join(area), \"number\": number}\n", + " \n", + "phone_number(\"864-252-5185\")" + ] + }, + { + "cell_type": "code", + "execution_count": 160, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "456555555.55\n" + ] + }, + { + "data": { + "text/plain": [ + "{'currency': '$', 'value': 456555555.55}" + ] + }, + "execution_count": 160, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def money(test):\n", + " if len(test) < 2:\n", + " return None\n", + " if re.fullmatch(r\"\\$.*\\.\\d{1}|\\$.*\\.\\d{3}|\\${2}\\d.*|\\$\\d*,\\d{1,2}|\\d+\", test):\n", + " return None\n", + " else:\n", + " cleantext = ''.join((re.findall(r\"[\\d\\.]\", test)))\n", + " print(cleantext)\n", + " return{\"currency\": \"$\", \"value\": float(cleantext)}\n", + "money(\"456,555,555.55\")" + ] + }, + { + "cell_type": "code", + "execution_count": 170, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "641004444\n" + ] + }, + { + "data": { + "text/plain": [ + "{'plus4': '4444', 'zip': '64100'}" + ] + }, + "execution_count": 170, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def zipcode(test):\n", + " if len(re.findall(r\"[\\d]\", test)) == 9 or len(re.findall(r\"[\\d]\", test)) == 5:\n", + " cleantext = ''.join((re.findall(r\"[\\d]\", test)))\n", + " print(cleantext)\n", + " zip = re.findall(r\"^\\d{5}\", cleantext)\n", + " number = cleantext[5:]\n", + " if number == '':\n", + " number = None\n", + " else:\n", + " number = ''.join(number)\n", + " return {\"zip\": ''.join(zip), \"plus4\": number}\n", + "zipcode(\"64100-4444\")" + ] + }, + { + "cell_type": "code", + "execution_count": 242, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['9/4/1976']\n", + "9\n", + "{'day': '4', 'year': '1976', 'month': '9'}\n", + "2015-01-01\n" + ] + }, + { + "data": { + "text/plain": [ + "{'day': '01', 'month': '01', 'year': '2015'}" + ] + }, + "execution_count": 242, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import re\n", + "def date(test):\n", + " if len(re.findall(r\"\\d.*/\\d.*/\\d.*|\\d.*-\\d.*-\\d.*\", test)):\n", + " if len(re.findall(r\"[/]\", test)) == 2:\n", + " date = re.findall(r\"\\d*/\\d*/\\d*\", test)\n", + " print(date)\n", + " month = ''.join(re.findall(r\"^\\d{1,2}\", test))\n", + " print(month)\n", + " day = re.findall(r\"/\\d{1,2}.\", test)\n", + " day = ''.join(day)[1:-1]\n", + " year = ''.join(re.findall(r\"/\\d{4}\", test))\n", + " year = year[1:]\n", + " return {\"month\": month, \"day\": day, \"year\": year}\n", + " if len(re.findall(r\"[-]\", test)) == 2:\n", + " date = ''.join(re.findall(r\"\\d*-\\d*-\\d*\", test))\n", + " \n", + " print(date)\n", + " \n", + " month = ''.join(re.findall(r\"-\\d{1,2}-\", test))\n", + " month = month[1:-1]\n", + " \n", + " day = date[8:]\n", + " \n", + " year = ''.join(re.findall(r\"\\d{4}\", test))\n", + " \n", + " return {\"month\": month, \"day\": day, \"year\": year}\n", + " else:\n", + " return None\n", + "print(date(\"9/4/1976\"))\n", + "date(\"2015-01-01\")" + ] + }, + { + "cell_type": "code", + "execution_count": 247, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def phone_numbers(text):\n", + " return re.findall(r\"\\(\\d{3}\\).\\d{3}.\\d{4}\", text)\n", + "phone_numbers(text)\n", + "assert phone_numbers(text) == [\"(454) 999-1212\", \"(919) 123-4569\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 243, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + " text = \"\"\"Dear Mr. Davis,\n", + "\n", + "I got to know of your company through our mutual friend Fiona Williams and the\n", + "training you offer to graduate students in Advertising.\n", + "\n", + "I am a graduate student of Mass Communications with specialization in\n", + "Advertising. I am currently pursuing the last year of my course.\n", + "I would very much like to see firsthand the work environment in an advertising\n", + "agency.\n", + "\n", + "If you would like a reference, my advisor can be reached at (454) 999-1212.\n", + "\n", + "You can contact me at (919) 123-4569 at your convenience.\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 256, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['amaranth@gmail.com', 'pea@sprouts.org', 'cucumber.earthnut@pea.net']" + ] + }, + "execution_count": 256, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def emails(text):\n", + " return re.findall(r\"[a-z\\.]*\\@[a-z\\.]*\", text)\n", + "\n", + "\n", + "text = \"\"\"Veggies es bonus vobis, proinde vos postulo essum magis kohlrabi\n", + "welsh onion daikon amaranth@gmail.com tatsoi tomatillo azuki bean garlic.\n", + "\n", + "Gumbo beet greens corn soko endive gumbo gourd. Parsley shallot courgette\n", + "tatsoi pea@sprouts.org fava bean collard greens dandelion okra wakame\n", + "tomato. Dandelion cucumber.earthnut@pea.net peanut soko zucchini.\"\"\"\n", + "\n", + "assert emails(text) == [\"amaranth@gmail.com\",\n", + " \"pea@sprouts.org\",\n", + " \"cucumber.earthnut@pea.net\"]\n", + "emails(text)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/textminer/extractor.py b/textminer/extractor.py index e69de29..be4ce31 100644 --- a/textminer/extractor.py +++ b/textminer/extractor.py @@ -0,0 +1,9 @@ +import re + + +def phone_numbers(text): + return re.findall(r"\(\d{3}\).\d{3}.\d{4}", text) + + +def emails(text): + return re.findall(r"[a-z\.]*\@[a-z\.]*", text) diff --git a/textminer/separator.py b/textminer/separator.py index e69de29..db50003 100644 --- a/textminer/separator.py +++ b/textminer/separator.py @@ -0,0 +1,67 @@ +import re + + +def words(test): + if len(re.findall(r"[a-zA-Z]", test)) > 0: + return test.split() + + +def date(test): + if len(re.findall(r"\d.*/\d.*/\d.*|\d.*-\d.*-\d.*", test)): + if len(re.findall(r"[/]", test)) == 2: + date = re.findall(r"\d*/\d*/\d*", test) + print(date) + month = ''.join(re.findall(r"^\d{1,2}", test)) + print(month) + day = re.findall(r"/\d{1,2}.", test) + day = ''.join(day)[1:-1] + year = ''.join(re.findall(r"/\d{4}", test)) + year = year[1:] + return {"month": int(month), "day": int(day), "year": int(year)} + if len(re.findall(r"[-]", test)) == 2: + date = ''.join(re.findall(r"\d*-\d*-\d*", test)) + + print(date) + + month = ''.join(re.findall(r"-\d{1,2}-", test)) + month = month[1:-1] + + day = date[8:] + + year = ''.join(re.findall(r"\d{4}", test)) + + return {"month": int(month), "day": int(day), "year": int(year)} + else: + return None + +def zipcode(test): + if len(re.findall(r"[\d]", test)) == 9 or len(re.findall(r"[\d]", test)) == 5: + cleantext = ''.join((re.findall(r"[\d]", test))) + print(cleantext) + zip = re.findall(r"^\d{5}", cleantext) + number = cleantext[5:] + if number == '': + number = None + else: + number = ''.join(number) + return {"zip": ''.join(zip), "plus4": number} + + +def money(test): + if len(test) < 2: + return None + if re.fullmatch(r"\$.*\.\d{1}|\$.*\.\d{3}|\${2}\d.*|\$\d*,\d{1,2}|\d+", test): + return None + else: + cleantext = ''.join((re.findall(r"[\d\.]", test))) + print(cleantext) + return{"currency": "$", "amount": float(cleantext)} + + +def phone_number(test): + if len(re.findall(r"[\d]", test)) == 10: + cleantext = ''.join((re.findall(r"[\d]", test))) + print(cleantext) + area = re.findall(r"^\d{3}", cleantext) + number = cleantext[3:6] + '-' + cleantext[6:] + return {"area_code": ''.join(area), "number": number} diff --git a/textminer/validator.py b/textminer/validator.py index e69de29..8a0bc57 100644 --- a/textminer/validator.py +++ b/textminer/validator.py @@ -0,0 +1,97 @@ +import re + + +def binary(test): + if len(test) == 0: + return False + search = re.findall(r"[^01]", test) + if len(search) > 0: + return False + else: + return True + + +def binary_even(test): + binar_y = binary(test) + if not binar_y: + return False + elif int(test, 2) % 2 == 0: + return True + else: + return False + + +def hex(test): + if len(test) == 0: + return False + search = re.findall(r"[^\da-fA-F]", test) + if len(search) > 0: + return False + else: + return True + + +def word(test): + if len(test) == 0: + return False + checkforletter = re.findall(r"[a-zA-Z]", test) + search = re.findall(r"[^\d\w\-]", test) + if len(search) == 0 and len(checkforletter) > 0: + return True + elif len(search) > 0: + return False + + +def words(test, count=0): + print(count) + if len(test) == 0: + return False + checkforletter = re.findall(r"[a-zA-Z]", test) + search = re.findall(r"[^\d\w\-\s]", test) + if len(search) == 0 and len(checkforletter) > 0: + pass + print(search) + if len(search) > 0: + return False + if len(checkforletter) == 0: + return False + else: + if count == 0: + return True + else: + print("Length", len(re.findall(r"\b[a-z]+\b", test))) + print(test) + if len(re.findall(r"\b[a-z]+\b", test)) == count: + return True + else: + return False + + +def phone_number(test): + if len(re.findall(r"[\d]", test)) == 10: + return True + else: + return False + + +def money(test): + if len(test) < 2: + return False + if re.fullmatch(r"\$.*\.\d{1}|\$.*\.\d{3}|\${2}\d.*|\$\d*,\d{1,2}|\d+", test): + return False + else: + return True + + +def zipcode(test): + if len(re.findall(r"[\d]", test)) == 5 or len(re.findall(r"[\d]", test)) == 9: + return True + else: + return False + + +def date(test): + if len(re.findall(r"\d.*/\d.*/\d.*|\d.*-\d.*-\d.*", test)): + return True + else: + return False