2017-04-24 31 views
0

例如,Spotify的API歌曲類型:減少凌亂的字到詞種子

['alternative rock', 'comic', 'funk rock', 'garage rock', 'indie rock', 'pop rock', 'post-grunge', 'rock'] 

['g funk', 'gangster rap', 'hip hop', 'pop rap', 'rap', 'west coast rap'] 

['canadian pop', 'dance pop', 'pop', 'pop christmas']  

三個列表代表三種歌曲的genres.But這種風格看起來很凌亂,我可以很容易地「提取」的「流派種子」 ,這是三首歌曲是

rock 
rap 
pop 
分別

我怎麼能減少這種混亂的話變成文字的種子? thx

+0

您需要某種類型的流派和'流派種子'之間的映射。 –

+0

你已經有一個有限的種子單詞列表了嗎? – JacobIRR

+0

是的,我確實有種類詞彙表,如「流行」「搖滾」 – user815408

回答

1

那麼,如果你有一個種子列表,我們可以,例如,計算每個種子的種類的出現次數,並返回最大權重的種子。 假設種子列表被稱爲「種子」,種類列表被稱爲「種類」。我們應該對所有種子類型組合進行交叉覈對,併爲某些結構增加權重。

def max_seed_return (seeds, genres): 
    # appending weigths to dictionary 
    weights= {seed:0 for seed in seeds} 
    for genre in genres: 
     for seed in seeds: 
      if seed in genre: 
      weights[seed]+=1 
    max_weight, result = 0, None 
    # getting result genre with biggest weigth 
    for seed, seed_weight in weights.items: 
     if seed_weight>max_weight: 
      max_weight=seed_weight 
      result=seed 
    #returns it or None if no seeds is found in genres 
    return result