From 57eea863e4157e8d9b3307954a854637bd4cb92a Mon Sep 17 00:00:00 2001 From: Bekk Blando Date: Tue, 16 Jun 2015 18:46:18 -0400 Subject: [PATCH 1/7] Stack Overflow = MVP --- .ipynb_checkpoints/Untitled-checkpoint.ipynb | 121 ++++++++++++++ Untitled.ipynb | 158 +++++++++++++++++++ textminer/extractor.py | 2 + textminer/separator.py | 18 +++ textminer/validator.py | 83 ++++++++++ 5 files changed, 382 insertions(+) create mode 100644 .ipynb_checkpoints/Untitled-checkpoint.ipynb create mode 100644 Untitled.ipynb diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb new file mode 100644 index 0000000..16ac99b --- /dev/null +++ b/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -0,0 +1,121 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "[]\n", + "Length 0\n", + "0\n", + "[]\n", + "0\n", + "[]\n", + "0\n", + "[]\n", + "0\n", + "[]\n", + "1\n", + "[]\n", + "Length 0\n", + "Smile False\n", + "2\n", + "[]\n", + "Length 0\n" + ] + }, + { + "ename": "AssertionError", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 29\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"18-wheeler tarbox\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Smile \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"hello\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 31\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"hello world\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 32\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"raggggg hammer dog\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"18 Wheeler\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"18-wheeler tarbox\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mAssertionError\u001b[0m: " + ] + } + ], + "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\"\\w+\\-\", test)))\n", + " if len(re.findall(r\"\\w+\\-\", 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", + "print(\"18 Wheeler\", 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", + "print(\"Hello\", 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": 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/Untitled.ipynb b/Untitled.ipynb new file mode 100644 index 0000000..dc8db97 --- /dev/null +++ b/Untitled.ipynb @@ -0,0 +1,158 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 58, + "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", + "18 Wheeler False\n", + "0\n", + "0\n", + "[]\n", + "2\n", + "['!', '!', '!']\n", + "2\n", + "['*', '!']\n", + "2\n", + "[]\n", + "Length 1\n", + "hello\n", + "Hello False\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" + ] + }, + { + "ename": "AssertionError", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"hello world\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 41\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"raggggg hammer dog\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 42\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"18-wheeler tarbox\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mAssertionError\u001b[0m: " + ] + } + ], + "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\"\\w+\", 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", + "print(\"18 Wheeler\", 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", + "print(\"Hello\", 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": 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..4cb242d 100644 --- a/textminer/extractor.py +++ b/textminer/extractor.py @@ -0,0 +1,2 @@ +def phone_numbers(text): + pass diff --git a/textminer/separator.py b/textminer/separator.py index e69de29..5316da0 100644 --- a/textminer/separator.py +++ b/textminer/separator.py @@ -0,0 +1,18 @@ +def date(test): + pass + + +def zipcode(test): + pass + + +def money(test): + pass + + +def phone_number(test): + pass + + +def words(test): + pass diff --git a/textminer/validator.py b/textminer/validator.py index e69de29..d735173 100644 --- a/textminer/validator.py +++ b/textminer/validator.py @@ -0,0 +1,83 @@ +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_numbers(test): + pass + + +def money(test): + pass + + +def zipcode(test): + pass + + +def date(test): + pass From 1009914a8d058598cc15fe5fe382490ca16b2d69 Mon Sep 17 00:00:00 2001 From: Bekk Blando Date: Tue, 16 Jun 2015 19:40:15 -0400 Subject: [PATCH 2/7] Stack Overflow is Ultimate MVP --- .ipynb_checkpoints/Untitled-checkpoint.ipynb | 129 ++++++++++++++++--- Untitled.ipynb | 113 +++++++++++++--- textminer/validator.py | 14 +- 3 files changed, 215 insertions(+), 41 deletions(-) diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb index 16ac99b..774a9a5 100644 --- a/.ipynb_checkpoints/Untitled-checkpoint.ipynb +++ b/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 29, + "execution_count": 60, "metadata": { "collapsed": false }, @@ -13,7 +13,8 @@ "text": [ "3\n", "[]\n", - "Length 0\n", + "Length 3\n", + "raggggg hammer dog\n", "0\n", "[]\n", "0\n", @@ -24,22 +25,44 @@ "[]\n", "1\n", "[]\n", - "Length 0\n", - "Smile False\n", + "Length 1\n", + "hello\n", + "Smile True\n", "2\n", "[]\n", - "Length 0\n" - ] - }, - { - "ename": "AssertionError", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 29\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"18-wheeler tarbox\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Smile \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"hello\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 31\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"hello world\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 32\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"raggggg hammer dog\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"18 Wheeler\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"18-wheeler tarbox\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mAssertionError\u001b[0m: " + "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" ] } ], @@ -62,8 +85,9 @@ " if count == 0:\n", " return True\n", " else:\n", - " print(\"Length\", len(re.findall(r\"\\w+\\-\", test)))\n", - " if len(re.findall(r\"\\w+\\-\", test)) == count:\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", @@ -76,17 +100,59 @@ "print(\"Smile \", words(\"hello\", count=1))\n", "assert words(\"hello world\", count=2)\n", "assert words(\"raggggg hammer dog\", count=3)\n", - "print(\"18 Wheeler\", words(\"18-wheeler tarbox\", count=2))\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", - "print(\"Hello\", words(\"hello\", 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": 62, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'phone_number' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mphone_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"919-555-1212\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mphone_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"(919) 555-1212\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mphone_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"9195551212\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'phone_number' is not defined" + ] + } + ], + "source": [ + "import re\n", + "def phone_numbers(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": null, @@ -94,7 +160,28 @@ "collapsed": true }, "outputs": [], - "source": [] + "source": [ + "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", + "assert not 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\")" + ] } ], "metadata": { diff --git a/Untitled.ipynb b/Untitled.ipynb index dc8db97..3644d68 100644 --- a/Untitled.ipynb +++ b/Untitled.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 58, + "execution_count": 60, "metadata": { "collapsed": false }, @@ -40,7 +40,6 @@ "[]\n", "Length 2\n", "18-wheeler tarbox\n", - "18 Wheeler False\n", "0\n", "0\n", "[]\n", @@ -52,7 +51,6 @@ "[]\n", "Length 1\n", "hello\n", - "Hello False\n", "3\n", "[]\n", "Length 2\n", @@ -66,17 +64,6 @@ "Length 2\n", "18-wheeler tarbox\n" ] - }, - { - "ename": "AssertionError", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"hello world\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 41\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"raggggg hammer dog\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 42\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mwords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"18-wheeler tarbox\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mAssertionError\u001b[0m: " - ] } ], "source": [ @@ -100,7 +87,7 @@ " else:\n", " print(\"Length\", len(re.findall(r\"\\b[a-z]+\\b\", test)))\n", " print(test)\n", - " if len(re.findall(r\"\\w+\", test)) == count:\n", + " if len(re.findall(r\"\\b[a-z]+\\b\", test)) == count:\n", " return True\n", " else:\n", " return False\n", @@ -113,17 +100,109 @@ "print(\"Smile \", words(\"hello\", count=1))\n", "assert words(\"hello world\", count=2)\n", "assert words(\"raggggg hammer dog\", count=3)\n", - "print(\"18 Wheeler\", words(\"18-wheeler tarbox\", count=2))\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", - "print(\"Hello\", words(\"hello\", 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": 62, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'phone_number' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mphone_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"919-555-1212\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mphone_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"(919) 555-1212\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mphone_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"9195551212\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'phone_number' is not defined" + ] + } + ], + "source": [ + "import re\n", + "def phone_numbers(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": null, diff --git a/textminer/validator.py b/textminer/validator.py index d735173..b4ecc05 100644 --- a/textminer/validator.py +++ b/textminer/validator.py @@ -67,12 +67,20 @@ def words(test, count=0): return False -def phone_numbers(test): - pass +def phone_number(test): + if len(re.findall(r"[\d]", test)) == 10: + return True + else: + return False def money(test): - pass + 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): From bb0be2f946e24345d467141ad33052ec5a2f512d Mon Sep 17 00:00:00 2001 From: Bekk Blando Date: Tue, 16 Jun 2015 19:50:14 -0400 Subject: [PATCH 3/7] Validator Done --- textminer/validator.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/textminer/validator.py b/textminer/validator.py index b4ecc05..8a0bc57 100644 --- a/textminer/validator.py +++ b/textminer/validator.py @@ -84,8 +84,14 @@ def money(test): def zipcode(test): - pass + if len(re.findall(r"[\d]", test)) == 5 or len(re.findall(r"[\d]", test)) == 9: + return True + else: + return False def date(test): - pass + if len(re.findall(r"\d.*/\d.*/\d.*|\d.*-\d.*-\d.*", test)): + return True + else: + return False From 773b5bca6bce3eefcb9b7c753b4075e069690a4d Mon Sep 17 00:00:00 2001 From: Bekk Blando Date: Tue, 16 Jun 2015 20:47:31 -0400 Subject: [PATCH 4/7] iPython may be new MVP --- .ipynb_checkpoints/Untitled-checkpoint.ipynb | 182 +++++++++++++++++-- Untitled.ipynb | 145 +++++++++++++-- textminer/separator.py | 28 ++- 3 files changed, 323 insertions(+), 32 deletions(-) diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb index 774a9a5..632bd68 100644 --- a/.ipynb_checkpoints/Untitled-checkpoint.ipynb +++ b/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 60, + "execution_count": 112, "metadata": { "collapsed": false }, @@ -113,26 +113,39 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 114, "metadata": { "collapsed": false }, "outputs": [ { - "ename": "NameError", - "evalue": "name 'phone_number' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mphone_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"919-555-1212\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mphone_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"(919) 555-1212\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mphone_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"9195551212\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mNameError\u001b[0m: name 'phone_number' is not defined" + "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_numbers(test):\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", @@ -155,12 +168,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 90, "metadata": { - "collapsed": true + "collapsed": false }, - "outputs": [], + "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", @@ -175,13 +208,132 @@ "assert money(\"$456,555,555.55\")\n", "assert money(\"$1234567.89\")\n", "assert not money(\"\")\n", - "assert not money(\"$12,34\")\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": 157, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "456555555.55\n" + ] + }, + { + "data": { + "text/plain": [ + "{'currency': '$', 'value': '456,555,555.55'}" + ] + }, + "execution_count": 157, + "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\": cleantext}\n", + "money(\"456,555,555.55\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/Untitled.ipynb b/Untitled.ipynb index 3644d68..46ead4b 100644 --- a/Untitled.ipynb +++ b/Untitled.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 60, + "execution_count": 112, "metadata": { "collapsed": false }, @@ -113,26 +113,39 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 114, "metadata": { "collapsed": false }, "outputs": [ { - "ename": "NameError", - "evalue": "name 'phone_number' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mphone_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"919-555-1212\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mphone_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"(919) 555-1212\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mphone_number\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"9195551212\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mNameError\u001b[0m: name 'phone_number' is not defined" + "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_numbers(test):\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", @@ -203,6 +216,116 @@ "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": null, diff --git a/textminer/separator.py b/textminer/separator.py index 5316da0..748f964 100644 --- a/textminer/separator.py +++ b/textminer/separator.py @@ -1,3 +1,11 @@ +import re + + +def words(test): + if len(re.findall(r"[a-zA-Z]", test)) > 0: + return test.split() + + def date(test): pass @@ -7,12 +15,20 @@ def zipcode(test): def money(test): - pass + 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): - pass - - -def words(test): - pass + 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} From c1f90ce489f70d9a58e37d8f2cc21355f14921ad Mon Sep 17 00:00:00 2001 From: Bekk Blando Date: Tue, 16 Jun 2015 21:28:10 -0400 Subject: [PATCH 5/7] Only Extractor Left --- .ipynb_checkpoints/Untitled-checkpoint.ipynb | 130 ++++++++++++++++++- Untitled.ipynb | 119 +++++++++++++++++ textminer/separator.py | 37 +++++- 3 files changed, 280 insertions(+), 6 deletions(-) diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb index 632bd68..9f67f91 100644 --- a/.ipynb_checkpoints/Untitled-checkpoint.ipynb +++ b/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -290,7 +290,7 @@ }, { "cell_type": "code", - "execution_count": 157, + "execution_count": 160, "metadata": { "collapsed": false }, @@ -305,10 +305,10 @@ { "data": { "text/plain": [ - "{'currency': '$', 'value': '456,555,555.55'}" + "{'currency': '$', 'value': 456555555.55}" ] }, - "execution_count": 157, + "execution_count": 160, "metadata": {}, "output_type": "execute_result" } @@ -322,10 +322,132 @@ " else:\n", " cleantext = ''.join((re.findall(r\"[\\d\\.]\", test)))\n", " print(cleantext)\n", - " return{\"currency\": \"$\", \"value\": 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": 230, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['9/4/1976']\n", + "9\n", + "['2015-01-01']\n", + "\n" + ] + }, + { + "data": { + "text/plain": [ + "{'day': '01-', 'month': '', 'year': '2015'}" + ] + }, + "execution_count": 230, + "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 = re.findall(r\"\\d*-\\d*-\\d*\", test)\n", + " \n", + " print(date)\n", + " \n", + " month = ''.join(re.findall(r\"-d{1,2}-\", test))\n", + " \n", + " print(month[1:-1])\n", + " \n", + " day = re.findall(r\"-\\d{1,2}.\", test)\n", + " \n", + " day = ''.join(day)[1:]\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", + "date(\"9/4/1976\")\n", + "date(\"2015-01-01\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, diff --git a/Untitled.ipynb b/Untitled.ipynb index 46ead4b..9f537ee 100644 --- a/Untitled.ipynb +++ b/Untitled.ipynb @@ -326,6 +326,125 @@ "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": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, diff --git a/textminer/separator.py b/textminer/separator.py index 748f964..db50003 100644 --- a/textminer/separator.py +++ b/textminer/separator.py @@ -7,11 +7,44 @@ def words(test): def date(test): - pass + 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): - pass + 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): From 37143269d3dcea672db14d01c7d6cb115c4f0a12 Mon Sep 17 00:00:00 2001 From: Bekk Blando Date: Tue, 16 Jun 2015 22:26:15 -0400 Subject: [PATCH 6/7] Normal and Extractor.py Hard Done --- .../Regex Homework-checkpoint.ipynb | 65 ++++++++++++-- ...d-checkpoint.ipynb => Regex Homework.ipynb | 88 +++++++++++++++---- textminer/extractor.py | 9 +- 3 files changed, 138 insertions(+), 24 deletions(-) rename Untitled.ipynb => .ipynb_checkpoints/Regex Homework-checkpoint.ipynb (85%) rename .ipynb_checkpoints/Untitled-checkpoint.ipynb => Regex Homework.ipynb (81%) diff --git a/Untitled.ipynb b/.ipynb_checkpoints/Regex Homework-checkpoint.ipynb similarity index 85% rename from Untitled.ipynb rename to .ipynb_checkpoints/Regex Homework-checkpoint.ipynb index 9f537ee..5752ca6 100644 --- a/Untitled.ipynb +++ b/.ipynb_checkpoints/Regex Homework-checkpoint.ipynb @@ -429,21 +429,76 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 247, "metadata": { - "collapsed": true + "collapsed": false }, "outputs": [], - "source": [] + "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": null, + "execution_count": 243, "metadata": { "collapsed": true }, "outputs": [], - "source": [] + "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", diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/Regex Homework.ipynb similarity index 81% rename from .ipynb_checkpoints/Untitled-checkpoint.ipynb rename to Regex Homework.ipynb index 9f67f91..6ec5148 100644 --- a/.ipynb_checkpoints/Untitled-checkpoint.ipynb +++ b/Regex Homework.ipynb @@ -368,7 +368,7 @@ }, { "cell_type": "code", - "execution_count": 230, + "execution_count": 242, "metadata": { "collapsed": false }, @@ -379,17 +379,17 @@ "text": [ "['9/4/1976']\n", "9\n", - "['2015-01-01']\n", - "\n" + "{'day': '4', 'year': '1976', 'month': '9'}\n", + "2015-01-01\n" ] }, { "data": { "text/plain": [ - "{'day': '01-', 'month': '', 'year': '2015'}" + "{'day': '01', 'month': '01', 'year': '2015'}" ] }, - "execution_count": 230, + "execution_count": 242, "metadata": {}, "output_type": "execute_result" } @@ -409,44 +409,96 @@ " year = year[1:]\n", " return {\"month\": month, \"day\": day, \"year\": year}\n", " if len(re.findall(r\"[-]\", test)) == 2:\n", - " date = re.findall(r\"\\d*-\\d*-\\d*\", test)\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 = ''.join(re.findall(r\"-\\d{1,2}-\", test))\n", + " month = month[1:-1]\n", " \n", - " print(month[1:-1])\n", - " \n", - " day = re.findall(r\"-\\d{1,2}.\", test)\n", - " \n", - " day = ''.join(day)[1:]\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", - "date(\"9/4/1976\")\n", + "print(date(\"9/4/1976\"))\n", "date(\"2015-01-01\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 247, "metadata": { - "collapsed": true + "collapsed": false }, "outputs": [], - "source": [] + "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": null, + "execution_count": 243, "metadata": { "collapsed": true }, "outputs": [], - "source": [] + "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", diff --git a/textminer/extractor.py b/textminer/extractor.py index 4cb242d..be4ce31 100644 --- a/textminer/extractor.py +++ b/textminer/extractor.py @@ -1,2 +1,9 @@ +import re + + def phone_numbers(text): - pass + return re.findall(r"\(\d{3}\).\d{3}.\d{4}", text) + + +def emails(text): + return re.findall(r"[a-z\.]*\@[a-z\.]*", text) From 1e9f71a09f3d168e05aa2ea15d1dfd94d921377c Mon Sep 17 00:00:00 2001 From: Bekk Blando Date: Tue, 18 Aug 2015 10:26:27 -0400 Subject: [PATCH 7/7] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) 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