state jurisdiction v4

This commit is contained in:
Sebastian 2022-05-04 00:44:18 +02:00
parent 721c4ef180
commit c8a9d35c20
5 changed files with 240 additions and 46 deletions

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 26,
"id": "8b07c9a5-e5cf-4cf9-a6d9-e784eb109fef",
"metadata": {},
"outputs": [],
@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 41,
"id": "fce94c21-6792-4938-bf2c-3f46ecf2f954",
"metadata": {},
"outputs": [],
@ -26,19 +26,21 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 42,
"id": "eb1815f2-1876-4437-833a-ff22de81685e",
"metadata": {},
"outputs": [],
"source": [
"def counter(text_in, query):\n",
" for line in text_in:\n",
" line = line.replace('.', ' ').replace(',', ' ').lower()\n",
" pattern = re.compile(query)\n",
" return len(pattern.findall(text_in, re.IGNORECASE))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 43,
"id": "8729062d-87b8-4111-a216-8500334f54b6",
"metadata": {},
"outputs": [],
@ -47,12 +49,12 @@
" state_dict = {}\n",
" for state in states:\n",
" state_dict[state.replace(\" \", \"_\")] = counter(text_in, state) \n",
" return max(state_dict, key=state_dict.get)"
" return state_dict"
]
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 44,
"id": "ea8069f7-de8e-454c-8eac-9fb7cc0df626",
"metadata": {},
"outputs": [],
@ -60,7 +62,6 @@
"def jurisdiction(path_in, path_out): \n",
" with open(path_in, 'r', encoding='utf8') as file:\n",
" lines = file.readlines()\n",
" lines = lines.replace('.', ' ').replace(',', ' ').lower()\n",
" with open(path_out, 'wt')as file_out:\n",
" for i in lines:\n",
" file_out.write(\"jurisdiction=\"+str(state_prediction(i))+'\\n') \n",
@ -69,7 +70,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 45,
"id": "ade45bfb-9eaa-4b2b-bba1-6cfcaf0a9ce6",
"metadata": {},
"outputs": [],
@ -81,7 +82,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 39,
"id": "594a25a9-a0ce-4de9-82c8-df50a4ecac39",
"metadata": {},
"outputs": [
@ -90,7 +91,7 @@
"output_type": "stream",
"text": [
"[NbConvertApp] Converting notebook run.ipynb to script\n",
"[NbConvertApp] Writing 1697 bytes to run.py\n"
"[NbConvertApp] Writing 1675 bytes to run.py\n"
]
}
],

View File

@ -7,7 +7,7 @@
import re
# In[27]:
# In[41]:
states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia',
@ -17,15 +17,17 @@ states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']
# In[28]:
# In[42]:
def counter(text_in, query):
for line in text_in:
line = line.replace('.', ' ').replace(',', ' ').lower()
pattern = re.compile(query)
return len(pattern.findall(text_in, re.IGNORECASE))
# In[29]:
# In[43]:
def state_prediction(text_in):
@ -35,20 +37,19 @@ def state_prediction(text_in):
return state_dict
# In[30]:
# In[44]:
def jurisdiction(path_in, path_out):
with open(path_in, 'r', encoding='utf8') as file:
lines = file.readlines()
lines = lines.replace('.', ' ').replace(',', ' ').lower()
with open(path_out, 'wt')as file_out:
for i in lines:
file_out.write("jurisdiction="+str(state_prediction(i))+'\n')
file_out.close()
# In[31]:
# In[45]:
jurisdiction('dev-0/in.tsv', 'dev-0/out.tsv')
@ -56,7 +57,7 @@ jurisdiction('train/in.tsv', 'train/out.tsv')
jurisdiction('test-A/in.tsv', 'test-A/out.tsv')
# In[24]:
# In[39]:
# get_ipython().system('jupyter nbconvert --to script run.ipynb')

View File

@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 41,
"id": "fce94c21-6792-4938-bf2c-3f46ecf2f954",
"metadata": {},
"outputs": [],
@ -26,19 +26,21 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 42,
"id": "eb1815f2-1876-4437-833a-ff22de81685e",
"metadata": {},
"outputs": [],
"source": [
"def counter(text_in, query):\n",
" for line in text_in:\n",
" line = line.replace('.', ' ').replace(',', ' ').lower()\n",
" pattern = re.compile(query)\n",
" return len(pattern.findall(text_in, re.IGNORECASE))"
]
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 43,
"id": "8729062d-87b8-4111-a216-8500334f54b6",
"metadata": {},
"outputs": [],
@ -52,7 +54,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 44,
"id": "ea8069f7-de8e-454c-8eac-9fb7cc0df626",
"metadata": {},
"outputs": [],
@ -60,7 +62,6 @@
"def jurisdiction(path_in, path_out): \n",
" with open(path_in, 'r', encoding='utf8') as file:\n",
" lines = file.readlines()\n",
" lines = lines.replace('.', ' ').replace(',', ' ').lower()\n",
" with open(path_out, 'wt')as file_out:\n",
" for i in lines:\n",
" file_out.write(\"jurisdiction=\"+str(state_prediction(i))+'\\n') \n",
@ -69,23 +70,10 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 45,
"id": "ade45bfb-9eaa-4b2b-bba1-6cfcaf0a9ce6",
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'list' object has no attribute 'replace'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32mC:\\Users\\SEBAST~1\\AppData\\Local\\Temp/ipykernel_3180/658503462.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mjurisdiction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'dev-0/in.tsv'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'dev-0/out.tsv'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mjurisdiction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'train/in.tsv'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'train/out.tsv'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mjurisdiction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'test-A/in.tsv'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'test-A/out.tsv'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mC:\\Users\\SEBAST~1\\AppData\\Local\\Temp/ipykernel_3180/2629600059.py\u001b[0m in \u001b[0;36mjurisdiction\u001b[1;34m(path_in, path_out)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpath_in\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'r'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencoding\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'utf8'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mfile\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mlines\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfile\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadlines\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mlines\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlines\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreplace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'.'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m' '\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreplace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m','\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m' '\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpath_out\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'wt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;32mas\u001b[0m \u001b[0mfile_out\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mlines\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mAttributeError\u001b[0m: 'list' object has no attribute 'replace'"
]
}
],
"outputs": [],
"source": [
"jurisdiction('dev-0/in.tsv', 'dev-0/out.tsv')\n",
"jurisdiction('train/in.tsv', 'train/out.tsv')\n",
@ -94,7 +82,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 39,
"id": "594a25a9-a0ce-4de9-82c8-df50a4ecac39",
"metadata": {},
"outputs": [
@ -103,7 +91,7 @@
"output_type": "stream",
"text": [
"[NbConvertApp] Converting notebook run.ipynb to script\n",
"[NbConvertApp] Writing 1697 bytes to run.py\n"
"[NbConvertApp] Writing 1675 bytes to run.py\n"
]
}
],

15
run.py
View File

@ -7,7 +7,7 @@
import re
# In[27]:
# In[41]:
states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia',
@ -17,15 +17,17 @@ states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']
# In[28]:
# In[42]:
def counter(text_in, query):
for line in text_in:
line = line.replace('.', ' ').replace(',', ' ').lower()
pattern = re.compile(query)
return len(pattern.findall(text_in, re.IGNORECASE))
# In[29]:
# In[43]:
def state_prediction(text_in):
@ -35,20 +37,19 @@ def state_prediction(text_in):
return state_dict
# In[30]:
# In[44]:
def jurisdiction(path_in, path_out):
with open(path_in, 'r', encoding='utf8') as file:
lines = file.readlines()
lines = lines.replace('.', ' ').replace(',', ' ').lower()
with open(path_out, 'wt')as file_out:
for i in lines:
file_out.write("jurisdiction="+str(state_prediction(i))+'\n')
file_out.close()
# In[31]:
# In[45]:
jurisdiction('dev-0/in.tsv', 'dev-0/out.tsv')
@ -56,7 +57,7 @@ jurisdiction('train/in.tsv', 'train/out.tsv')
jurisdiction('test-A/in.tsv', 'test-A/out.tsv')
# In[24]:
# In[39]:
# get_ipython().system('jupyter nbconvert --to script run.ipynb')

File diff suppressed because one or more lines are too long