Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegEx Homework #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions textminer/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions textminer/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions textminer/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions textminer/.idea/textminer.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions textminer/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

260 changes: 260 additions & 0 deletions textminer/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions textminer/extractor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import re

def phone_numbers(string):
number = re.findall(r'(\(?\d{3}\)?[\-\.]?\s*?\d{3}[\-\.]?\d{4})', string)
return number

def emails(string):
emails = re.findall(r'(\w+?\.?\w+@\w+\.\w{2,3})', string)
return emails
50 changes: 50 additions & 0 deletions textminer/separator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import re

def words(string):
result = re.findall(r'(\b[^\s]+[a-z]+\b)', string)
if result == []:
return None
else:
return result

def phone_number(string):
number = re.match(r'\(?(\d{3})\)?[\-\.]?\s*?(\d{3})[\-\.]?(\d{4})', string)
if number == None:
return None
else:
return {"area_code": number.group(1),
"number": number.group(2)+"-"+number.group(3)}

def money(string):
money = re.match(r'^(\$)(\d+(,?\d{3,})*\.?(\d{2})?)$', string)
if money == None:
return None
else:
money2 = money.group(2).replace(',','')
return {"currency": money.group(1),
"amount": float(money2)}

def zipcode(string):
zip = re.match(r'^(\d{5})-?(\d{4})?$', string)
if zip == None:
return None
else:
return {"zip": zip.group(1),
"plus4": zip.group(2)}

def date(string):
the_date = re.match(r'^(\d{1,4})[-|/](\d{1,2})[-|/](\d{1,4})$', string)
if the_date == None:
return None
else:
if int(the_date.group(1)) > 1000:
year = int(the_date.group(1))
month = int(the_date.group(2))
day = int(the_date.group(3))
elif int(the_date.group(3)) > 1000:
year = int(the_date.group(3))
month = int(the_date.group(1))
day = int(the_date.group(2))
return {"month": month,
"day": day,
"year": year}
4 changes: 2 additions & 2 deletions textminer/tests/test_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import textminer.extractor as x

@xfail
#@xfail
def test_phone_numbers():
text = """Dear Mr. Davis,

Expand All @@ -24,7 +24,7 @@ def test_phone_numbers():

## HARD MODE BEGINS

@xfail
#@xfail
def test_emails():
text = """Veggies es bonus vobis, proinde vos postulo essum magis kohlrabi
welsh onion daikon [email protected] tatsoi tomatillo azuki bean garlic.
Expand Down
Loading