data normalization
This commit is contained in:
parent
fc55c7b5dc
commit
9b00bafaa0
2 changed files with 47 additions and 11 deletions
|
|
@ -1,33 +1,69 @@
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from pandas.api.types import CategoricalDtype
|
from pandas.api.types import CategoricalDtype
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
import re
|
||||||
|
import unicodedata
|
||||||
class DataFrame:
|
class DataFrame:
|
||||||
def __init__(self,es):
|
def __init__(self,es):
|
||||||
self.es = es
|
self.es = es
|
||||||
|
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
|
||||||
|
self.df_eng["name"] = self.df_eng["name"].str.lower().apply(lambda n:self.normalize(n))
|
||||||
|
|
||||||
|
self.df_eng["category"] = self.df_eng["category"].str.lower().apply(lambda c:self.normalize(c))
|
||||||
|
|
||||||
|
self.df_eng=self.df_eng.drop_duplicates()
|
||||||
|
|
||||||
|
def normalize(self,doc):
|
||||||
|
|
||||||
|
# Remove html tags
|
||||||
|
soup = BeautifulSoup(doc, 'html.parser')
|
||||||
|
text =soup.get_text()
|
||||||
|
text = (re.sub('[-\W]+', ' ', text))
|
||||||
|
text = (re.sub('(?<=\d) (?=\d)', '', text))
|
||||||
|
return ''.join(
|
||||||
|
c for c in unicodedata.normalize('NFD', text)
|
||||||
|
if unicodedata.category(c) != 'Mn')
|
||||||
|
# text= text.replace('-', '')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def create(self):
|
def create(self):
|
||||||
df_eng = pd.DataFrame(columns=['name','category'])
|
self.df_eng = pd.DataFrame(columns=['name','category'])
|
||||||
resp=self.es.getFeatures()
|
resp=self.es.getFeatures()
|
||||||
|
|
||||||
for hit in resp['hits']['hits']:
|
for hit in resp['hits']['hits']:
|
||||||
list_row = dict (name=None,category=None)
|
list_row_en = dict (name=None,category=None)
|
||||||
|
list_row_de = dict (name=None,category=None)
|
||||||
for name in hit['_source']['nameSource']:
|
for name in hit['_source']['nameSource']:
|
||||||
if (name["language"]=="en"):
|
if (name["language"]=="en"):
|
||||||
list_row["name"]=name["value"]
|
list_row_en["name"]=name["value"]
|
||||||
|
else:
|
||||||
|
list_row_de["name"]=name["value"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for cats in hit['_source']['categoriesSource']:
|
for cats in hit['_source']['categoriesSource']:
|
||||||
if (cats["language"]=="en"):
|
if (cats["language"]=="en"):
|
||||||
list_row["category"]=cats["label"]
|
list_row_en["category"]=cats["label"]
|
||||||
|
else:
|
||||||
|
list_row_de["category"]=cats["label"]
|
||||||
|
|
||||||
|
|
||||||
if(list_row["name"]!=None and list_row["category"]!=None):
|
if(list_row_en["name"]!=None and list_row_en["category"]!=None):
|
||||||
new_row = pd.Series(list_row)
|
new_row = pd.Series(list_row_en)
|
||||||
df_eng=pd.concat([df_eng, new_row.to_frame().T], ignore_index=True)
|
self.df_eng=pd.concat([self.df_eng, new_row.to_frame().T], ignore_index=True)
|
||||||
# df_eng.loc[len(df_eng)] = list_row
|
# df_eng.loc[len(df_eng)] = list_row
|
||||||
|
|
||||||
return df_eng
|
self.clean()
|
||||||
|
return self.df_eng
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,15 +11,15 @@ def main():
|
||||||
|
|
||||||
df_eng=df.create()
|
df_eng=df.create()
|
||||||
|
|
||||||
# print(df_eng)
|
print(df_eng.head())
|
||||||
|
|
||||||
# df_eng["category"].astype("category")
|
# df_eng["category"].astype("category")
|
||||||
|
|
||||||
# df =df_eng["category"].isnull()
|
# df =df_eng["category"].isnull()
|
||||||
|
|
||||||
# bool_name_series = pd.notnull(df_eng["name"])
|
# bool_name_series = pd.notnull(df_eng["name"])
|
||||||
bool_cat_series = pd.isnull(df_eng["name"])
|
# bool_cat_series = pd.isnull(df_eng["name"])
|
||||||
print(df_eng[bool_cat_series])
|
# print(df_eng[bool_cat_series])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue