【问题标题】:Reduce messy words into word seed将杂乱的单词减少为单词种子
【发布时间】:2017-09-20 15:37:42
【问题描述】:

例如,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']      

三个列表代表三首歌曲的流派。但是这样的流派看起来很混乱,我可以很容易地“提取”出“流派种子”,即三首歌曲是

rock
rap
pop

分别

我怎样才能把这些乱七八糟的词变成词种子? 谢谢

【问题讨论】:

  • 您需要在流派和“流派种子”之间进行某种映射。
  • 你已经有一个有限的种子词列表了吗?
  • 是的,我确实有诸如“pop”“rock”之类的种子词列表

标签: python api seed


【解决方案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

【讨论】:

    猜你喜欢
    • 2021-07-10
    • 1970-01-01
    • 2011-02-12
    • 2014-04-05
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多