簡體   English   中英

如何在node.js應用程序中使用Apache OpenNLP

[英]How to use Apache OpenNLP in a node.js application

將Apache Open NLP與node.js一起使用的最佳方法是什么?

具體來說,我想使用Name Entity Extraction API。 以下是關於它的說法 - 文檔很糟糕(我認為是新項目):

http://opennlp.apache.org/documentation/manual/opennlp.html#tools.namefind

來自文檔:

要在生產系統中使用名稱查找器,強烈建議將其直接嵌入到應用程序中,而不是使用命令行界面。 首先,名稱查找器模型必須從磁盤或其他來源加載到內存中。 在下面的示例中,它從磁盤加載。

InputStream modelIn = new FileInputStream("en-ner-person.bin");

try {
  TokenNameFinderModel model = new TokenNameFinderModel(modelIn);
}
catch (IOException e) {
  e.printStackTrace();
}
finally {
  if (modelIn != null) {
    try {
      modelIn.close();
    }
    catch (IOException e) {
    }
  }
}

模型加載失敗的原因有很多:

底層I / O的問題

該模型的版本與OpenNLP版本不兼容

模型被加載到錯誤的組件中,例如,令牌化器模型加載了TokenNameFinderModel類。

由於某些其他原因,模型內容無效

加載模型后,可以實例化NameFinderME。

NameFinderME nameFinder = new NameFinderME(model);

現在完成初始化並且可以使用名稱查找器。 NameFinderME類不是線程安全的,只能從一個線程調用它。 要使用多個線程,可以創建共享同一模型實例的多個NameFinderME實例。 輸入文本應分為文檔,句子和標記。 為了執行實體檢測,應用程序為文檔中的每個句子調用find方法。 在每個文檔之后必須調用clearAdaptiveData以清除特征生成器中的自適應數據。 不調用clearAdaptiveData會導致幾個文檔后檢測率急劇下降。 以下代碼說明:

for (String document[][] : documents) {

  for (String[] sentence : document) {
    Span nameSpans[] = find(sentence);
    // do something with the names
  }

  nameFinder.clearAdaptiveData()
}

the following snippet shows a call to find


String sentence = new String[]{
    "Pierre",
    "Vinken",
    "is",
    "61",
    "years"
    "old",
    "."
    };

Span nameSpans[] = nameFinder.find(sentence);

nameSpans數組現在只包含一個Span,標志着Pierre Vinken的名字。 開始和結束偏移之間的元素是名稱標記。 在這種情況下,begin偏移量為0,結束偏移量為2.Span對象也知道實體的類型。 在這種情況下,它的人(由模型定義)。 可以通過調用Span.getType()來檢索它。 除了統計名稱查找器,OpenNLP還提供字典和正則表達式名稱查找器實現。

簽出這個NodeJS庫。
https://github.com/mbejda/Node-OpenNLP
https://www.npmjs.com/package/opennlp

只需要NPM安裝opennlp

看看Github上的例子。

var nameFinder = new openNLP().nameFinder;
nameFinder.find(sentence, function(err, results) {
    console.log(results)
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM