-
Notifications
You must be signed in to change notification settings - Fork 0
/
sigle.py
60 lines (48 loc) · 1.71 KB
/
sigle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Récupération de code depuis https://git.stable.innovation.insee.eu/innovation/AIEE/-/blob/master/enrichissement/stephanie/sirus_sigles.ipynb
Génération de sigle à partir de sirus 2017
Author : [email protected]
date : 16/07/2020
"""
import os
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
import psycopg2
import yaml
import time
from tqdm import tqdm
import io
from stop_words import get_stop_words
import string
import re
from bdd import *
my_driver = PostGre_SQL_DB()
def main():
test = my_driver.read_from_sql("""
SELECT sirus_id,nic,enseigne_et1,adr_et_l1,adr_et_l2
FROM sirus_2017
""")
denomination = test.loc[test.adr_et_l1.isna()==False,["sirus_id","nic","adr_et_l1"]]
# Suppression des champs débutant par monsieur madame dans l'adresse (nom de personnes et pas raison sociale d'entreprise)
selection = [w.strip().split(' ')[0] not in ['MONSIEUR','MADAME'] for w in denomination.adr_et_l1]
denomination = denomination.loc[selection,:]
stop_words = get_stop_words('french')
stops = stop_words.append([])
words = list(set(denomination.adr_et_l1))
sortie = []
for s in words:
o = re.sub("-"," ",s)
o = o.split(" ")
out = [re.sub(r"[\w]{1}'",'',w) for w in o]
out = [re.sub(r'[^\w\s]','',w) for w in out]
out = [w[:1] for w in out if (w.lower() not in stop_words) ]
out = ''.join(out)
sortie.append(out)
table = pd.DataFrame({"adr_et_l1":words,"sigle":sortie})
export = pd.merge(denomination,table,on="adr_et_l1",how='outer')
print(export.shape)
print(denomination.shape)
export.to_csv("sigles_adr_et_l1_sirus.csv",sep=",")
if __name__ == "__main__":
main()