Javascript 从句子中提取艺术家和歌曲标题的技巧?
对于上下文,我尝试做的是制作一个小的webapp,在其中可以粘贴一个Reddit讨论线程,并将线程中引用的歌曲名称转换为Spotify播放列表 我正试图找到一种从自然语言中提取艺术家/歌曲名称的方法,格式为艺术家-歌曲名称或艺术家的歌曲名称 例如,假设我有以下字符串:Javascript 从句子中提取艺术家和歌曲标题的技巧?,javascript,string,parsing,delimited-text,Javascript,String,Parsing,Delimited Text,对于上下文,我尝试做的是制作一个小的webapp,在其中可以粘贴一个Reddit讨论线程,并将线程中引用的歌曲名称转换为Spotify播放列表 我正试图找到一种从自然语言中提取艺术家/歌曲名称的方法,格式为艺术家-歌曲名称或艺术家的歌曲名称 例如,假设我有以下字符串: The Funeral by the Band of Horses is my favorite song. you should check out the Acoustic version of Foo Fighters -
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...
}
}
}
有一个有趣的文本解析库:。js似乎没有用于解析艺术家或歌曲的插件,但它似乎应该很容易实现。请参阅:如果您使用的是自然语言,那么如何区分乐队/艺人名称与其他提到的同一单词 造一个句子有无数种方法,你必须抓住所有可能的方法 另一种方法是对照存储艺术家和乐队名称的数据库检查字符串的逐字和多字
否则,您肯定会错过文本中的一些名称。不确定为什么投票结果过于宽泛-我要问一个非常具体的问题,如何使用两种不同的可能格式从字符串中解析出一个值。。。