kleister-nda/run.ipynb

4.1 KiB

import re
from datetime import datetime
import csv
states = ["Alaska", "Alabama", "Arkansas", "American Samoa", "Arizona", "California", "Colorado", "Connecticut", "District ", "of Columbia", "Delaware", "Florida", "Georgia", "Guam", "Hawaii", "Iowa", "Idaho", "Illinois", "Indiana", "Kansas", "Kentucky", "Louisiana", "Massachusetts", "Maryland", "Maine", "Michigan", "Minnesota", "Missouri", "Mississippi", "Montana", "North Carolina", "North Dakota", "Nebraska", "New Hampshire", "New Jersey", "New Mexico", "Nevada", "New York", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Puerto Rico", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Virginia", "Virgin Islands", "Vermont", "Washington", "Wisconsin", "West Virginia", "Wyoming"]
rgx = re.compile(r'\b(' + '|'.join(states) + r')\b')
company_rgx = r"(([A-Z][A-za-z]+,?\s)+(Inc\.|LLC|Ltd\.|Company|Corporation|INC\.|LTD\.|COMPANY|CORPORATION))"
def nda(path_in, path_out):
    with open(path_in, 'r', encoding='utf-8') as in_file:
        lines = in_file.readlines()
    with open(path_out, 'w') as out_file:
                for line in lines:
                #line = line.replace('.', ' ').replace(',', ' ').lower()
                #words = line.split()
                    jur = rgx.search(line)
                    #print(jur)
                    if jur:
                        out_file.write('jurisdiction=' + jur.group().replace(' ', '_'))
                        
                        
                    res = re.search(r'\d{4}-\d{2}-\d{2}',line)
                    #print(res)
                    if res:
                        date = datetime.strptime(res.group(), '%Y-%m-%d').date()
                        print(str(date))
                        out_file.write('effective_date=' + date.group().replace(' ', '_'))
                        
                    #party_results = []
                    #party = re.findall(company_rgx, line)
                    #party_score = len([w for w in party if w in party])
                    #if party_score > 2:
                    #    party_results.append(party[0])
                    par = re.search(company_rgx,line)
                    #print(par)
                    if par:
                        out_file.write(' party=' + par.group().replace(','' ', '_'))
                    out_file.write('\n')
                
#pliki
nda('train/in.tsv', 'train/out.tsv')
nda('dev-0/in.tsv', 'dev-0/out.tsv')
nda('test-A/in.tsv', 'test-A/out.tsv')