2017-07-15 64 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來獲取姓名和性別。

相關問題