为了快速验证概念,您可以使用预训练的嵌入器,即 LASER。这是非官方的pypi包。它工作得很好。不过,请记住,嵌入器适用于相当短的文本块。将单个语义含义分配给多个句子是没有意义的。Embedder 产生数值向量。一旦你嵌入了两首歌曲的歌词,你就可以计算它们之间的距离度量,即欧几里得。它应该有点开箱即用,但不要指望有突破性的东西。
例子
from laserembeddings import Laser
from scipy.spatial.distance import euclidean
laser = Laser()
beatles_lyrics = """
I love you, 'cause you tell me things I want to know
And it's true that it really only goes to show
That I know that I, I, I, I
Should never, never, never be blue
"""
joy_division_lyrics = """
I've been waiting for a guide to come and take me by the hand,
Could these sensations make me feel the pleasures of a normal man?
These sensations barely interest me for another day,
I've got the spirit, lose the feeling, take the shock away.
"""
beatles_lyrics_embedded = laser.embed_sentences([beatles_lyrics], lang='en')[0]
joy_division_lyrics_embedded = laser.embed_sentences(
[joy_division_lyrics],
lang='en'
)[0]
similarity = euclidean(beatles_lyrics_embedded, joy_division_lyrics_embedded)
print(similarity)
在这里您可以找到更多距离指标:https ://docs.scipy.org/doc/scipy-0.14.0/reference/spatial.distance.html
答案
我遇到过词嵌入和其他东西,但它们的工作对我来说并不完全清楚。
嵌入将超维文本编码到低维数字空间。它以某种方式训练,来自不同语言的语义相似的句子彼此更接近。
此外,我认为他们没有考虑可用的类似歌曲的功能:类型令牌比率、情绪等级、单词密度(每句话的平均单词数)等。
Embedder 经过预训练以学习特征表示本身。它们是高级功能,但完全是黑盒。
这就是深度学习的全部意义所在。我们很难在这样的空间中手动设计特征。相反,我们训练深度模型的方式是学习这些特征本身的表示。
如果您想从文本中学习情感,嵌入器绝对是您的最佳选择。您只需要为每首歌词定义情绪。