Newer
Older
web-services / hospital-affiliations / aff_hosp.py
from numpy import column_stack
import pandas as pd
from nltk.tokenize import word_tokenize
from fuzzywuzzy import fuzz
import sys
import json
import unicodedata
import re
pd.options.mode.chained_assignment = None  

## MODIFICATION DU FICHIER EXCEL
# On regarde si la liste des termes acronymes choisis est présente dans les affiliations
def terme_aff(texte, liste=None) :
    for aff in liste :
        if aff in texte.lower() :
            return True
    return False
# Il faut également que l'affiliation qu'on veut homogénéiser ait un terme de la liste


#correction of the cities : remove the department numbers and transformation into acronyms
def remove_department_numbers_and_acronyms(city_name) :
    # department numbers: Grenoble - 38 => Grenoble
    sep = ' -'
    stripped = city_name.split(sep, 1)[0]
    stripped = stripped.replace("-"," ")

    # acronyms
    stripped = stripped.replace("Saint","St")
    stripped = stripped.replace("Mont","Mt")

    return stripped

# correction of the cities : remove accents
def remove_accents(city_name) :
    normalized_text = unicodedata.normalize("NFD", city_name)
    text_with_no_accent = re.sub("[\u0300-\u036f]", '', normalized_text)
    return text_with_no_accent

# On repère/marque les lignes dont l'affiliation contient la ville
def aff_ville(ville,texte):
    if ville.lower() in texte.lower() :
        return True
    return False

def fuzzywuzzy(affiliation,text = None):
    return fuzz.ratio(affiliation,text)

def base(texte,df):
    acro = ["chr","chr ","chu ","chu,","chru","ap hp","ap hm","aphp","aphm","hosp","hop ","hop,"," serv","clcc","ctr lutte canc","antoine lacassagne","eugene marquis","baclesse",
    "georges françois leclerc","gf leclerc","jf leclerc","henri becquerel","h becquerel","jean perrin","leon berard","ctr oscar lambret",
    "oscar lambret comprehens","oscar lambret canc","clcc oscar lambret","ctr lutte canc oscar lambret","gustave roussy","bergonie",
    "inst curie","curie inst","rene gauducheau","inst cancerol ouest","inst cancerol lorraine","alexis vautrin","inst reg canc montpellier",
    "icans","paul strauss","paul str","paoli calmettes","inst j paoli i calmettes","claudius regaud","claudius rigaud","inst univ canc toulouse oncopole",
    "inst univ cancerol oncopole","iuct oncopole","ctr lutte contre canc","ico canc","icm, montpellier canc inst","univ inst canc toulouse oncopole","rothschild"]
    
    Orga = ""
    for ac in acro:
        if ac in texte.lower():
            # APPEL DES FONCTIONS
            df['Terme Affiliation'] = df["Affiliation"].apply(terme_aff,liste=acro)
            df['Ville_canonique_Dpt'] = df['Ville_canonique_Dpt'].apply(remove_department_numbers_and_acronyms)
            df['Ville_canonique_Dpt'] = df['Ville_canonique_Dpt'].apply(remove_accents)
            if len(df[df['Terme Affiliation'] == True]) != 0:
                df2 = df[df['Terme Affiliation'] == True]
                df2["Ville_présente"] = df2['Ville_canonique_Dpt'].apply(aff_ville,texte=texte)
                if len(df2[df2["Ville_présente"] == True]) != 0:
                    df3 = df2[df2["Ville_présente"] == True]
                    df3["ratio"] = df3["Affiliation"].apply(fuzzywuzzy,text=texte)
                    Orga = df3["Orga NonCnrs Acorriger"][df3["ratio"].idxmax()]
                else:
                    Orga = "N.C"
            else:
                Orga = "N.C"
            break
    if Orga == "":
        Orga = "N.C"
    return Orga

df = pd.read_csv('hospital_affiliations.csv', sep=";")


for line in sys.stdin:
    data = json.loads(line)
    texte=data['value']
    data['value']=base(texte,df)
    sys.stdout.write(json.dumps(data))
    sys.stdout.write('\n')