How to optimize this if-else statement used for tagging sports from title
So I'm trying to auto categorize sports from the event title.
It's working fine, however i think there should be a better and more
reliable method to do this. And for some sports like (FIFA) it outputs its
type as NCAA instead of FIFA same thing with MMA and 1-2 more.
Here's my code (Note: str_contains is a laravel helper function I'm using).
$strTitle = strtolower($title);
if(str_contains($strTitle, 'mlb') || str_contains($strTitle,
'baseball')) {
$category = 'Baseball';
$type = 'MLB';
} elseif (str_contains($strTitle, 'nba') || str_contains($strTitle,
'fiba') || str_contains($strTitle, 'basketball') ||
str_contains($strTitle, 'wnba')) {
$category = 'Basketball';
$type = (str_contains($strTitle, 'nba')) ? 'NBA':
(str_contains($strTitle, 'fiba')) ? 'FIBA':
(str_contains($strTitle, 'wnba')) ? 'WNBA':'Basketball';
} elseif (str_contains($strTitle, 'nhl') || str_contains($strTitle,
'hockey')) {
$category = 'Hockey';
$type = 'NHL';
} elseif (str_contains($strTitle, 'nascar') ||
str_contains($strTitle, 'formula one') || str_contains($strTitle,
'gp2') || str_contains($strTitle, 'gp3') || str_contains($strTitle,
'motogp') || str_contains($strTitle, 'moto2') ||
str_contains($strTitle, 'moto3') || str_contains($strTitle, 'f1')) {
$category = 'Motor Sport';
$type = (str_contains($strTitle, 'nascar')) ? 'NASCAR':
(str_contains($strTitle, 'gp2')) ? 'GP2':
(str_contains($strTitle, 'gp3')) ? 'GP3':
(str_contains($strTitle, 'motogp')) ? 'MotoGP':
(str_contains($strTitle, 'moto2')) ? 'Moto2':
(str_contains($strTitle, 'moto3')) ? 'Moto3':
(str_contains($strTitle, 'f1') ||
str_contains($strTitle, 'formula one')) ?
'F1':'Motor Sport';
} elseif (str_contains($strTitle, 'nfl') || str_contains($strTitle,
'afl') || str_contains($strTitle, 'welsh premier league') ||
str_contains($strTitle, 'fox college') || str_contains($strTitle,
'football') || str_contains($strTitle, 'serie') ||
str_contains($strTitle, 'soccer') || str_contains($strTitle, 'fifa')
|| str_contains($strTitle, 'ncaa')) {
$category = 'Football';
$type = (str_contains($strTitle, 'nfl')) ? 'NFL':
(str_contains($strTitle, 'fifa')) ? 'FIFA':
(str_contains($strTitle, 'afl')) ? 'AFL':
(str_contains($strTitle, 'welsh premier league')) ?
'Welsh Premier League':
(str_contains($strTitle, 'ncaa')) ?
'NCAA':'Football';
} elseif (str_contains($strTitle, 'tennis')) {
$category = 'Tennis';
$type = 'Tennis';
} elseif (str_contains($strTitle, 'golf')) {
$category = 'Golf';
$type = 'Golf';
} elseif (str_contains($strTitle, 'rugby') ||
str_contains($strTitle, 'nrl')) {
$category = 'Rugby';
$type = (str_contains($strTitle, 'nrl')) ? 'NRL' : 'Rugby';
} elseif (str_contains($strTitle, 'sailing') ||
str_contains($strTitle, 'america\'s cup')) {
$category = 'Water Sport';
$type = 'Sailing';
} elseif (str_contains($strTitle, 'boxing') ||
str_contains($strTitle, 'fight night') || str_contains($strTitle,
'fighting') || str_contains($strTitle, 'wwe') ||
str_contains($strTitle, 'smackdown') || str_contains($strTitle,
'raw') || str_contains($strTitle, 'wwe main event') ||
str_contains($strTitle, 'mma') || str_contains($strTitle,
'strikeforce') || str_contains($strTitle, 'tna')) {
$category = 'Boxing';
$type = (str_contains($strTitle, 'ufc')) ? 'UFC' :
(str_contains($strTitle, 'smackdown')) ? 'WWE Smackdown' :
(str_contains($strTitle, 'raw')) ? 'WWE RAW' :
(str_contains($strTitle, 'wwe main event')) ? 'WWE
Main Event':
(str_contains($strTitle, 'wwe')) ? 'WWE':
(str_contains($strTitle, 'mma')) ? 'MMA':
(str_contains($strTitle, 'tna')) ? 'TNA':
(str_contains($strTitle, 'strikeforce')) ?
'Strikeforce':
(str_contains($strTitle, 'fight night')) ?
'Fight Night':
(str_contains($strTitle, 'fighting')) ?
'Fighting':'Boxing';
} elseif (str_contains($strTitle, 'cricket') ||
str_contains($strTitle, 'icc') || str_contains($strTitle, 'mcc') ||
str_contains($strTitle, 'odi') || str_contains($strTitle, 'ipl') ||
str_contains($strTitle, 't20') || str_contains($strTitle,
'twenty20')) {
$category = 'Cricket';
$type = (str_contains($strTitle, 'icc')) ? 'ICC' :
(str_contains($strTitle, 'mcc')) ? 'MCC' :
(str_contains($strTitle, 'odi')) ? 'ODI':
(str_contains($strTitle, 'ipl')) ? 'IPL':
(str_contains($strTitle, 't20')) ? 'T20':
(str_contains($strTitle, 'twenty20')) ?
'Twenty20':'Cricket';
}
Note 2: This isn't the full code and not for all sports, just for the ones
I have atm.
No comments:
Post a Comment