Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
42 lines (33 sloc) 1.41 KB
"""A best effort transformer to determine browser device characteristics from a user-agent string"""
# Custom transformer: UserAgent
# UserAgent column should have one of the following names: ua, user-agent, user_agent, useragent
import datatable as dt
import numpy as np
from h2oaicore.transformer_utils import CustomTransformer
class UserAgent(CustomTransformer):
_modules_needed_by_name = ["user-agents==2.0"]
def get_default_properties():
return dict(col_type="text", min_cols=1, max_cols=1, relative_importance=1)
def do_acceptance_test():
return False
def fit_transform(self, X: dt.Frame, y: np.array = None):
return self.transform(X)
def transform(self, X: dt.Frame):
def get_ua_info(ua_string):
from user_agents import parse
ua = parse(ua_string)
return,,, ua.is_mobile, ua.is_tablet
ua_column_names = ['ua', 'user-agent', 'user_agent', 'useragent']
col_name = X.names[0]
if col_name in ua_column_names:
newnames = ("browser", "os", "device", "is_mobile", "is_tablet")
Y = X[col_name].to_list()[0]
Z = dt.Frame([get_ua_info(x) for x in Y], names=[f"{col_name}_{s}" for s in newnames])
return X
return X.to_pandas().iloc[:, 0]
You can’t perform that action at this time.