2017-07-15 56 views
-1

我有串:有没有lib或一些旧的方法从字符串中获取名称?

var fullText = 'John,victor and Mike and not Rudie'; 

是否有JS或图书馆摆脱这样的字符串名字? 更新:如果我知道除'和',',''以外的其他文字都是名称。 在此先感谢。

+0

如何定义名称?这个星球上的每个人都可能拥有什么名字? – PeterMader

+2

不,没有,因为**你不能做任何关于名称**的假设:http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ – Dai

+0

更新的问题也许这将有助于? – Serhiy

回答

0

var fullText = 'John,victor and Mike and not Rudie'; 
 
fullText = fullText.replace(" and ", ","); 
 
fullText = fullText.replace(" and not ", ","); 
 
fullText = fullText.split(","); 
 
console.log("name1: " + fullText[0] + " name2: " + fullText[1] + " name3: " + fullText[2] + " name4: " + fullText[3]);

使用replacesplit 如果名字的数量是不明就可以做同样的事情刚加入forwhile

例子:

var fullText = 'John,victor and Mike and not Rudie'; 
 
fullText = fullText.replace(" and ", ","); 
 
fullText = fullText.replace(" and not ", ","); 
 
fullText = fullText.split(","); 
 

 
for (var i = 0; i < fullText.length; i++) { 
 
    console.log("name" + (i + 1) + ": " + fullText[i]); 
 
}

+0

谢谢。一开始就为我工作。 – Serhiy

+0

不客气:)我很高兴我的帮助 –

0

我不认为有这样一个库的原因这需要NLP这是不容易实现。但是,您可以定义一个列表(如文件或枚举中的名称)(根据您的名称定义),然后检查标记是否为名称。

0

更新:如果你想提取姓名出文章,例如,而不是从一个固定的文本式的;

有研究认为值得考虑:

谷歌为:人类名字解析器,命名实体识别(NER),人的名字解析器API

Onyxfish Humaniformat

Human Name parsing

我想在所有情况下应该有一本字典。

这一次也很有意思:

https://www.nameapi.org/

的API来获取姓名和性别。

相关问题