kleister-nda/run.ipynb

123 lines
3.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 64,
"id": "8b07c9a5-e5cf-4cf9-a6d9-e784eb109fef",
"metadata": {},
"outputs": [],
"source": [
"import re"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "fce94c21-6792-4938-bf2c-3f46ecf2f954",
"metadata": {},
"outputs": [],
"source": [
"states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', \n",
" 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', \n",
" 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', \n",
" 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Pennsylvania', 'Rhode Island', 'South Carolina', \n",
" 'South Dakota', 'Tennessee', 'Texas', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "eb1815f2-1876-4437-833a-ff22de81685e",
"metadata": {},
"outputs": [],
"source": [
"def counter(text_in, query):\n",
" pattern = re.compile(query)\n",
" return len(pattern.findall(text_in, re.IGNORECASE))"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "8729062d-87b8-4111-a216-8500334f54b6",
"metadata": {},
"outputs": [],
"source": [
"def state_prediction(text_in):\n",
" 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)"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "ea8069f7-de8e-454c-8eac-9fb7cc0df626",
"metadata": {},
"outputs": [],
"source": [
"def jurisdiction(path_in, path_out): \n",
" with open(path_in, 'r', encoding='utf8') as file:\n",
" lines = file.readlines()\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",
" file_out.close()"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "ade45bfb-9eaa-4b2b-bba1-6cfcaf0a9ce6",
"metadata": {},
"outputs": [],
"source": [
"jurisdiction('dev-0/in.tsv', 'dev-0/out.tsv')\n",
"jurisdiction('train/in.tsv', 'train/out.tsv')\n",
"jurisdiction('test-A/in.tsv', 'test-A/out.tsv')"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "594a25a9-a0ce-4de9-82c8-df50a4ecac39",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[NbConvertApp] Converting notebook run.ipynb to script\n",
"[NbConvertApp] Writing 1605 bytes to run.py\n"
]
}
],
"source": [
"!jupyter nbconvert --to script run.ipynb"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}