kleister-nda/run.ipynb
2022-05-04 00:23:23 +02:00

137 lines
6.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 26,
"id": "8b07c9a5-e5cf-4cf9-a6d9-e784eb109fef",
"metadata": {},
"outputs": [],
"source": [
"import re"
]
},
{
"cell_type": "code",
"execution_count": 27,
"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', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', \n",
" 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', \n",
" 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', \n",
" 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']"
]
},
{
"cell_type": "code",
"execution_count": 28,
"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": 29,
"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 state_dict"
]
},
{
"cell_type": "code",
"execution_count": 30,
"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",
" 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",
" file_out.close()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"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'"
]
}
],
"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": 24,
"id": "594a25a9-a0ce-4de9-82c8-df50a4ecac39",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[NbConvertApp] Converting notebook run.ipynb to script\n",
"[NbConvertApp] Writing 1697 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
}