示例#1
0
int main(int argc, char *argv[])
{
	if (argc != 4)
	{
		cerr << "Usage: ./ltp_test <type> <test_xml_file> <result_file>" << endl;
		exit(1);
	}

	string type(argv[1]);

	xml4nlp.LoadXMLFromFile(argv[2]);
	if(type == "ws"){
		ltp.crfWordSeg();
	} else if(type == "pos"){
		ltp.postag();
	} else if(type == "ner"){
		ltp.ner();
	} else if(type == "dp"){
		ltp.gparser();
	} else if(type == "srl"){
		ltp.srl();
	} else {
		ltp.srl();
	}

	xml4nlp.SaveDOM(argv[3]);

	xml4nlp.ClearDOM();

	return 0;
}
示例#2
0
文件: ltp_test.cpp 项目: Abioy/ltp
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        cerr << "Usage: ./ltp_test <type> <test_file>" << endl;
        exit(1);
    }

    cout << "Begin ..." << endl;
    string sentence;
    string type(argv[1]);
    ifstream in(argv[2]);
    ofstream log_file("test.log");

    if (!in.is_open())
    {
        cerr << "Cann't open file!" << endl;
        exit(1);
    }

    while(in >> sentence){
        cout << "Input sentence is: " << sentence << endl;

        xml4nlp.CreateDOMFromString(sentence);
        if(type == "ws"){
            ltp.crfWordSeg();
            int wordNum = xml4nlp.CountWordInDocument();
            for (int i = 0; i < wordNum; ++i)
            {
                const char* word = xml4nlp.GetWord(i);
                if (word != NULL)
                {
                    log_file << word << " ";
                }
            }
        } else if(type == "pos"){
            ltp.postag();
        } else if(type == "ner"){
            ltp.ner();
        } else if(type == "dp"){
            ltp.gparser();
        } else if(type == "srl"){
            ltp.srl();
        } else {
            ltp.srl();
        }

        string result;
        xml4nlp.SaveDOM(result);

        cout << "Result is: " << result << endl;
        xml4nlp.ClearDOM();
    }

    return 0;
}
示例#3
0
int main(int argc, char *argv[])
{
    if (argc != 4)
    {
        cerr << "Usage: ./ltp_test <type> <test_file> <result_file>" << endl;
        exit(1);
    }

    string type(argv[1]);
    string in_file(argv[2]);
    string res_file(argv[3]);

    xml4nlp.CreateDOMFromFile(in_file.c_str());

    if (type == "ws") {
        ltp.crfWordSeg(xml4nlp);
    } else if(type == "pos"){
        ltp.postag(xml4nlp);
    } else if(type == "ner"){
        ltp.ner(xml4nlp);
    } else if(type == "dp"){
        ltp.gparser(xml4nlp);
    } else if(type == "srl"){
        ltp.srl(xml4nlp);
    } else {
        ltp.srl(xml4nlp);
    }

    string result;
    xml4nlp.SaveDOM(result);

    ofstream out(res_file.c_str());
    out << result << endl;
    cerr << "Results saved to " << res_file << endl;

    xml4nlp.ClearDOM();

    return 0;
}
示例#4
0
static int Service(struct mg_connection *conn) {
    char *sentence;
    char type[10];
    char xml[10];
    char buffer[POST_LEN];

    string str_post_data;
    string str_type;
    string str_xml;

    const struct mg_request_info *ri = mg_get_request_info(conn);

    if (!strcmp(ri->uri, "/ltp")) {
        int len;
        while((len = mg_read(conn, buffer, sizeof(buffer) - 1)) > 0){
            buffer[len] = 0;
            str_post_data += buffer;
        }

        TRACE_LOG("CDATA: %s", str_post_data.c_str());
        TRACE_LOG("CDATA length: %d", str_post_data.size());

        sentence = new char[str_post_data.size() + 1];

        mg_get_var(str_post_data.c_str(), 
                str_post_data.size(), 
                "s",
                sentence,
                str_post_data.size());

        mg_get_var(str_post_data.c_str(), 
                str_post_data.size(), 
                "t",
                type,
                sizeof(type) - 1);

        mg_get_var(str_post_data.c_str(), 
                str_post_data.size(), 
                "x",
                xml,
                sizeof(xml) - 1);

        // std::cerr << "sentence: " << sentence << std::endl;
        // std::cerr << "type    : " << type << std::endl;
        // std::cerr << "xml     : " << xml << std::endl;
        // std::cerr << "validation check" << std::endl;

        string strSentence = sentence;

        /*
         * validation check
         */
        if (strlen(sentence) == 0 || !isclear(strSentence)) {
            // std::cerr << "Failed validation check" << std::endl;
            WARNING_LOG("Failed string validation check");
            return 0;
        }

        if(strlen(type) == 0) {
            str_type = "";
        } else {
            str_type = type;
        }

        if(strlen(xml) == 0) {
            str_xml = "";
        } else {
            str_xml = xml;
        }

        delete []sentence;

        TRACE_LOG("Input sentence is: %s", strSentence.c_str());

        if(str_xml == "y"){
            xml4nlp.LoadXMLFromString(strSentence);
        } else {
            xml4nlp.CreateDOMFromString(strSentence);
        }

        if(str_type == "ws"){
            engine.wordseg();
        } else if(str_type == "pos"){
            engine.postag();
        } else if(str_type == "ner"){
            engine.ner();
        } else if(str_type == "dp"){
            engine.parser();
        } else if(str_type == "srl"){
            engine.srl();
        } else {
            engine.srl();
        }

        string strResult;
        xml4nlp.SaveDOM(strResult);

        strResult = "HTTP/1.1 200 OK\r\n\r\n" + strResult;

        // cout << "Result is: " << strResult << endl;
        mg_printf(conn, "%s", strResult.c_str());

        xml4nlp.ClearDOM();
    }
    return 1;
}