从句子中提取艺术家和歌曲标题的技巧

Techniques to extract artist and song title from a sentence?

本文关键字:标题 句子 提取 艺术家      更新时间:2023-09-26

就上下文而言,我想做的是制作一个小型网络应用程序,你可以在其中粘贴Reddit讨论线程,并将线程中引用的歌曲名称转换为Spotify播放列表。

我正在想办法从一些自然语言中提取艺术家/歌曲的名字,格式为"artist - song name"或"songname by artist"。

例如,假设我有以下字符串:

The Funeral by the Band of Horses is my favorite song.
you should check out the Acoustic version of Foo Fighters - Everlong.
Eminem- Stan. Not a fan of rap but I like this song.

结果输出为:

["The Funeral", "the Band of Horses"],
["Foo Fighters", "Everlong"],
["Eminem", "Stan"]

由于没有API调用,无法知道什么是艺术家,什么是歌曲,因此不需要以任何特定方式存储,我只需要将艺术家和歌曲名称分解为不同的数组部分。

这是否可以在没有任何分隔符的情况下指示歌曲名称的结尾?

以下是我迄今为止所拥有的。。。(半伪代码):

delimiters = [" - ", "-", " by ",];
strings = [
    "The Funeral by the Band of Horses is my favorite song.",
    "you should check out the Acoustic version of Foo Fighters - Everlong.",
    "Eminem- Stan. Not a fan of rap but I like this song."
];
// loop over each string
for (var i=0; i<strings.length; i++ ) {
    // loop through each delimiter possibility
    for (var d=0; d<delimiters.length; d++) {
        if ( strings[i].indexOf(delimiters[d]) > -1 ) {
            // we have a delimiter match
            // now figure out how to get the stuff on either side...
        }
    }
}

有一个有趣的文本解析库:Knwl.js。Knwl..js似乎没有用于解析艺术家或歌曲的插件,但它似乎应该很容易实现。请参阅:插件开发

如果你使用的是自然语言,你如何将Band/Artistname与同一单词的其他提及区分开来。

造句有无数种方法,你必须抓住所有可能的方法。

另一种方法是根据存储艺术家和乐队名称的数据库,按单词和多单词检查字符串。

否则,你肯定会漏掉文本中的一些名字。