예제 #1
0
// Function to dependency parse all sentence in the given input file
// and write parsed sentences to the given output file
extern "C" void dep_parse_file(const char *sInputFile, const char *sOutputFile, bool tokenize)
{

    std::cerr << "Processing file " <<  sInputFile << std::endl;

    // initialize the input reader
    CSentenceReader input_reader(sInputFile);

    // open the output file
    FILE *outfp = NULL;
    outfp = fopen(sOutputFile, "w");

    // initialize the temporary sentence variables
    CStringVector tokenized_sent[1];
    CTwoStringVector tagged_sent[1];
    CDependencyParse parsed_sent[1];

    // get the tagger and the parser that were stored earlier
    CTagger *tagger = (CTagger *)zpm->tagger;
    CDepParser *depparser = (CDepParser *)zpm->depparser;

    // read in and tokenize the given input file if asked
    bool readSomething;
    if (tokenize) {
        readSomething = input_reader.readSegmentedSentenceAndTokenize(tokenized_sent);
    }
    else {
        readSomething = input_reader.readSegmentedSentence(tokenized_sent);
    }

    while ( readSomething )
    {
        if ( tokenized_sent->back() == "\n" )
        {
            tokenized_sent->pop_back();
        }

        std::string deptree = "";
        if(tokenized_sent->size() < MAX_SENTENCE_SIZE){
            tagger->tag(tokenized_sent, tagged_sent);
            depparser->parse(*tagged_sent, parsed_sent);
            deptree = format_dependency_tree(parsed_sent);
        } else {
            std::cerr << "Sentence too long. Writing empty string. Input:" << tokenized_sent << std::endl;
        }

        fprintf(outfp, "%s\n", deptree.c_str());

        if (tokenize) {
            readSomething = input_reader.readSegmentedSentenceAndTokenize(tokenized_sent);
        }
        else {
            readSomething = input_reader.readSegmentedSentence(tokenized_sent);
        }
    }

    // close the output file
    std::cerr << "Wrote output to " << sOutputFile << std::endl;
    fclose(outfp);
}
예제 #2
0
// Function to dependency parse a sentence
extern "C" char* dep_parse_sentence(void* vzps, const char *input_sentence, bool tokenize)
{
    zparSession_t* zps = static_cast<zparSession_t *>(vzps);

    try {

        // create a temporary string stream from the input char *
        CSentenceReader input_reader(std::string(input_sentence), false);

        // tokenize the sentence
        CStringVector tokenized_sent[1];
        if (tokenize) {
            input_reader.readSegmentedSentenceAndTokenize(tokenized_sent);
        }
        else {
            input_reader.readSegmentedSentence(tokenized_sent);
        }

        if (zps->output_buffer != NULL) {
            delete zps->output_buffer;
            zps->output_buffer = NULL;
        }

        if(tokenized_sent->size() >= MAX_SENTENCE_SIZE){
            // The ZPar code asserts that length < MAX_SENTENCE_SIZE...
            std::cerr << "Sentence too long. Returning empty string. Sentence: " << input_sentence << std::endl;
            zps->output_buffer = new char[1];
            strcpy(zps->output_buffer, "");
        } else {

            // initialize the variable that will hold the tagged and parsed sentences
            CTwoStringVector tagged_sent[1];
            CDependencyParse parsed_sent[1];

            // get the tagger and parser that were stored earlier
            CTagger *tagger = zps->tagger;
            CDepParser *depparser = zps->depparser;

            // tag and parse the sentence
            tagger->tag(tokenized_sent, tagged_sent);
            depparser->parse(*tagged_sent, parsed_sent);

            // now output the formatted dependency tree
            std::string deptree = format_dependency_tree(parsed_sent);
            int deptreelen = deptree.length();
            zps->output_buffer = new char[deptreelen + 1];
            strcpy(zps->output_buffer, deptree.c_str());
        }

    } catch (const std::string &e) {
        std::cerr << e << std::endl;
        zps->output_buffer = new char[1];
        strcpy(zps->output_buffer, "");
    }

    return zps->output_buffer;
}
예제 #3
0
extern "C" void dep_parse_tagged_file(void* vzps, const char *sInputFile, const char *sOutputFile, const char seperator='/')
{

    zparSession_t* zps = static_cast<zparSession_t *>(vzps);

    std::cerr << "Processing file " <<  sInputFile << std::endl;

    // initialize the input reader
    CSentenceReader input_reader(sInputFile);

    // open the output file
    FILE *outfp = NULL;
    outfp = fopen(sOutputFile, "w");

    // initialize the temporary sentence variables
    CTwoStringVector tagged_sent[1];
    CDependencyParse parsed_sent[1];

    // get the parser that was stored earlier
    CDepParser *depparser = zps->depparser;

    // read in and tokenize the given input file if asked
    bool readSomething;
    readSomething = input_reader.readTaggedSentence(tagged_sent, false, seperator);

    while ( readSomething )
    {
        std::string deptree = "";
        if(tagged_sent->size() < MAX_SENTENCE_SIZE){
            depparser->parse(*tagged_sent, parsed_sent);
            deptree = format_dependency_tree(parsed_sent);
        } else {
            std::cerr << "Sentence too long. Writing empty string. Sentence: " << tagged_sent << std::endl;
        }

        fprintf(outfp, "%s\n", deptree.c_str());

        readSomething = input_reader.readTaggedSentence(tagged_sent, false, seperator);
    }

    // close the output file
    std::cerr << "Wrote output to " << sOutputFile << std::endl;
    fclose(outfp);
}