我一直在探索使用預訓練的MITIE模型進行命名實體提取。無論如何,我可以看看他們的實際模型而不是使用預訓練模型嗎?該模型是否可用作開源軟件?MITIE ner model
回答
設置事情:
對於初學者來說,你可以下載哪些 包含註釋文本的語料庫從一個巨大的轉儲文件中English Language Model稱爲 total_word_feature_extractor.dat。
之後,從他們的 官方Git下載/克隆MITIE-Master Project。
如果您正在運行Windows O.S然後下載CMake。
如果您正在運行基於x64的Windows O.S,然後安裝適用於C++編譯器的Visual Studio 2015社區版。
下載後,將上述內容全部提取到文件夾中。
爲VS,從2015年開始>應用程序> Visual Studio的開發者打開命令提示符,然後導航到Tools文件夾,你會看到5裏面的子文件夾。
下一步是建立ner_conll,ner_stream,train_freebase_relation_detector和wordrep包,使用下面的CMake在Visual Studio開發人員命令提示符命令。
像這樣:
對於ner_conll:
cd "C:\Users\xyz\Documents\MITIE-master\tools\ner_conll"
ⅰ)mkdir build
ⅱ)cd build
ⅲ)cmake -G "Visual Studio 14 2015 Win64" ..
ⅳ)cmake --build . --config Release --target install
對於ner_stream:
cd "C:\Users\xyz\Documents\MITIE-master\tools\ner_stream"
ⅰ)mkdir build
ⅱ)cd build
ⅲ)cmake -G "Visual Studio 14 2015 Win64" ..
ⅳ)cmake --build . --config Release --target install
對於train_freebase_relation_detector:
cd "C:\Users\xyz\Documents\MITIE-master\tools\train_freebase_relation_detector"
ⅰ)mkdir build
ⅱ)cd build
ⅲ)cmake -G "Visual Studio 14 2015 Win64" ..
IV)cmake --build . --config Release --target install
對於wordrep:
cd "C:\Users\xyz\Documents\MITIE-master\tools\wordrep"
我)mkdir build
II)cd build
III)cmake -G "Visual Studio 14 2015 Win64" ..
IV)cmake --build . --config Release --target install
後您建立他們,你會得到一些警告150-160,別擔心。
現在,導航到"C:\Users\xyz\Documents\MITIE-master\examples\cpp\train_ner"
做一個JSON文件「data.json」使用Visual Studio代碼手動註釋文本,這樣的事情:
{
"AnnotatedTextList": [
{
"text": "I want to travel from New Delhi to Bangalore tomorrow.",
"entities": [
{
"type": "FromCity",
"startPos": 5,
"length": 2
},
{
"type": "ToCity",
"startPos": 8,
"length": 1
},
{
"type": "TimeOfTravel",
"startPos": 9,
"length": 1
}
]
}
]
}
您可以添加更多的話語和註釋他們,訓練數據越多,預測精度就越好。
這個帶註釋的JSON也可以通過jQuery或Angular等前端工具創建。但爲了簡潔起見,我手工創建了它們。
現在,解析我們帶註釋的JSON文件並將其傳遞給ner_training_instance的add_entity方法。
但是C++不支持反射來反序列化JSON,這就是爲什麼你可以使用這個庫Rapid JSON Parser。從他們的Git頁面下載軟件包並將其放在"C:\Users\xyz\Documents\MITIE-master\mitielib\include\mitie"
下。
現在我們必須自定義train_ner_example.cpp文件,以解析我們註釋的自定義實體JSON並將其傳遞給MITIE進行訓練。
#include "mitie\rapidjson\document.h"
#include "mitie\ner_trainer.h"
#include <iostream>
#include <vector>
#include <list>
#include <tuple>
#include <string>
#include <map>
#include <sstream>
#include <fstream>
using namespace mitie;
using namespace dlib;
using namespace std;
using namespace rapidjson;
string ReadJSONFile(string FilePath)
{
ifstream file(FilePath);
string test;
cout << "path: " << FilePath;
try
{
std::stringstream buffer;
buffer << file.rdbuf();
test = buffer.str();
cout << test;
return test;
}
catch (exception &e)
{
throw std::exception(e.what());
}
}
//Helper function to tokenize a string based on multiple delimiters such as ,.;:- or whitspace
std::vector<string> SplitStringIntoMultipleParameters(string input, string delimiter)
{
std::stringstream stringStream(input);
std::string line;
std::vector<string> TokenizedStringVector;
while (std::getline(stringStream, line))
{
size_t prev = 0, pos;
while ((pos = line.find_first_of(delimiter, prev)) != string::npos)
{
if (pos > prev)
TokenizedStringVector.push_back(line.substr(prev, pos - prev));
prev = pos + 1;
}
if (prev < line.length())
TokenizedStringVector.push_back(line.substr(prev, string::npos));
}
return TokenizedStringVector;
}
//Parse the JSON and store into appropriate C++ containers to process it.
std::map<string, list<tuple<string, int, int>>> FindUtteranceTuple(string stringifiedJSONFromFile)
{
Document document;
cout << "stringifiedjson : " << stringifiedJSONFromFile;
document.Parse(stringifiedJSONFromFile.c_str());
const Value& a = document["AnnotatedTextList"];
assert(a.IsArray());
std::map<string, list<tuple<string, int, int>>> annotatedUtterancesMap;
for (int outerIndex = 0; outerIndex < a.Size(); outerIndex++)
{
assert(a[outerIndex].IsObject());
assert(a[outerIndex]["entities"].IsArray());
const Value &entitiesArray = a[outerIndex]["entities"];
list<tuple<string, int, int>> entitiesTuple;
for (int innerIndex = 0; innerIndex < entitiesArray.Size(); innerIndex++)
{
entitiesTuple.push_back(make_tuple(entitiesArray[innerIndex]["type"].GetString(), entitiesArray[innerIndex]["startPos"].GetInt(), entitiesArray[innerIndex]["length"].GetInt()));
}
annotatedUtterancesMap.insert(pair<string, list<tuple<string, int, int>>>(a[outerIndex]["text"].GetString(), entitiesTuple));
}
return annotatedUtterancesMap;
}
int main(int argc, char **argv)
{
try {
if (argc != 3)
{
cout << "You must give the path to the MITIE English total_word_feature_extractor.dat file." << endl;
cout << "So run this program with a command like: " << endl;
cout << "./train_ner_example ../../../MITIE-models/english/total_word_feature_extractor.dat" << endl;
return 1;
}
else
{
string filePath = argv[2];
string stringifiedJSONFromFile = ReadJSONFile(filePath);
map<string, list<tuple<string, int, int>>> annotatedUtterancesMap = FindUtteranceTuple(stringifiedJSONFromFile);
std::vector<string> tokenizedUtterances;
ner_trainer trainer(argv[1]);
for each (auto item in annotatedUtterancesMap)
{
tokenizedUtterances = SplitStringIntoMultipleParameters(item.first, " ");
mitie::ner_training_instance *currentInstance = new mitie::ner_training_instance(tokenizedUtterances);
for each (auto entity in item.second)
{
currentInstance -> add_entity(get<1>(entity), get<2>(entity), get<0>(entity).c_str());
}
// trainingInstancesList.push_back(currentInstance);
trainer.add(*currentInstance);
delete currentInstance;
}
trainer.set_num_threads(4);
named_entity_extractor ner = trainer.train();
serialize("new_ner_model.dat") << "mitie::named_entity_extractor" << ner;
const std::vector<std::string> tagstr = ner.get_tag_name_strings();
cout << "The tagger supports " << tagstr.size() << " tags:" << endl;
for (unsigned int i = 0; i < tagstr.size(); ++i)
cout << "\t" << tagstr[i] << endl;
return 0;
}
}
catch (exception &e)
{
cerr << "Failed because: " << e.what();
}
}
的add_entity接受3個參數,所述標記化的字符串可以是矢量,自定義實體類型名稱,一個字的在句子的起始索引和字的範圍內。
現在我們必須通過在Developer Command Prompt Visual Studio中使用以下命令來構建ner_train_example.cpp。
1)cd "C:\Users\xyz\Documents\MITIE-master\examples\cpp\train_ner"
2)mkdir build
3)cd build
4)cmake -G "Visual Studio 14 2015 Win64" ..
5)cmake --build . --config Release --target install
6)cd Release
7)train_ner_example "C:\\Users\\xyz\\Documents\\MITIE-master\\MITIE-models\\english\\total_word_feature_extractor.dat" "C:\\Users\\xyz\\Documents\\MITIE-master\\examples\\cpp\\train_ner\\data.json"
在成功地執行上述我們將得到一個new_ner_model.dat文件,這是我們的話語的序列化和訓練版本。
現在,該.dat文件可以傳遞給RASA或獨立使用。
對於它傳遞給RASA:
充分利用config.json文件,如下所示:
{
"project": "demo",
"path": "C:\\Users\\xyz\\Desktop\\RASA\\models",
"response_log": "C:\\Users\\xyz\\Desktop\\RASA\\logs",
"pipeline": ["nlp_mitie", "tokenizer_mitie", "ner_mitie", "ner_synonyms", "intent_entity_featurizer_regex", "intent_classifier_mitie"],
"data": "C:\\Users\\xyz\\Desktop\\RASA\\data\\examples\\rasa.json",
"mitie_file" : "C:\\Users\\xyz\\Documents\\MITIE-master\\examples\\cpp\\train_ner\\Release\\new_ner_model.dat",
"fixed_model_name": "demo",
"cors_origins": ["*"],
"aws_endpoint_url": null,
"token": null,
"num_threads": 2,
"port": 5000
}
- 1. 數據集訓練MITIE模型
- 2. NER干擾REGEXNER
- 3. 斯坦福NER
- 4. Stanford NLP - NER&Models
- 5. Display Stanford NER confidence score
- 6. NER天真算法
- 7. openNLP java - 多項葡萄牙語NER
- 8. NLTK NER:持續學習
- 9. NoSuchFieldError異常斯坦福NER
- 10. NER的標籤數量
- 11. NER學術用工具
- 12. 加速使用MITIE和Rasa的模型訓練
- 13. Model
- 14. Model
- 15. 在Spacer NER模型中的評估
- 16. 自定義NER模型 - 失敗
- 17. 斯坦福大學NLP - NER - 列車NER名稱有多個標記
- 18. 此前被標記爲NER用於訓練的語料庫NER分類器
- 19. Spring MVC Model model life-cicle
- 20. model from model返回NULL
- 21. Symfony FORMS:{'model'=> $ this-> getRelatedModelName('model')}和{'model'=>'model'}在setWidgets中的差異
- 22. before_create model
- 23. 從Model
- 24. 從Model
- 25. Codeigniter Model
- 26. 斯坦福NLP訓練n-gram NER
- 27. 自定義NER和POS標記
- 28. 如何NER添加標籤功能
- 29. 如何在Apache Spark中使用Stanford NER
- 30. Stanford NER,輸出編碼問題