Exemplo n.º 1
0
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;
}
Exemplo n.º 2
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;
}