コード例 #1
0
ファイル: segment_dll.cpp プロジェクト: 153370771/ltp
  int segment(const char* str, std::vector<std::string> & words) {
    ltp::framework::ViterbiFeatureContext ctx;
    ltp::framework::ViterbiScoreMatrix scm;
    ltp::framework::ViterbiDecoder decoder;
    ltp::segmentor::Instance inst;
 
    int ret = preprocessor.preprocess(str, inst.raw_forms, inst.forms,
      inst.chartypes);

    if (-1 == ret || 0 == ret) {
      words.clear();
      return 0;
    }

    ltp::segmentor::SegmentationConstrain con;
    con.regist(&(inst.chartypes));
    build_lexicon_match_state(lexicons, &inst);
    extract_features(inst, model, &ctx, false);
    calculate_scores(inst, (*model), ctx, true, &scm);

    // allocate a new decoder so that the segmentor support multithreaded
    // decoding. this modification was committed by niuox
    decoder.decode(scm, con, inst.predict_tagsidx);
    build_words(inst.raw_forms, inst.predict_tagsidx, words);

    return words.size();
  }
コード例 #2
0
ファイル: file_analysis.c プロジェクト: davidjulien/ouroboros
/* Extrait d'un fichier une liste de séquences */
t_list_words analyse_file(const char *filepath, int nbr_words, int skip_words,
        t_list_single *allwords, char sorted)
{
    FILE *f;
    t_list_words l;
    char **buffer_words;
    int posbuff;
    int i;
    int count;
    int offset;

    INFO("Analyse du fichier <%s> longueur séquence = %d, décalage = %d", 
            filepath, nbr_words, skip_words);

    /* Ouverture du fichier */
    INFO("Ouverture du fichier %s", filepath);
    f = fopen(filepath, "r");
    if (f==NULL) {
        ERROR("Erreur d'ouverture du fichier %s : %s", filepath, 
                strerror(errno));
        return EMPTY_LIST;
    }

    /* Initialisation d'un buffer contenant les N derniers mots */
    DEBUG("Initialisation d'un buffer pour %d mots", nbr_words);
    buffer_words = malloc(nbr_words*sizeof(char*));
    if (buffer_words == NULL) {
        ERROR("Erreur d'allocation mémoire : %s", strerror(errno));
        exit(100);
    }
    posbuff = nbr_words - 1;

    INFO("Construction de la liste de mots");
    l = EMPTY_LIST;
    *allwords = EMPTY_SL;

    INFO("Amorçage");
    for(i=0;i<nbr_words-1;i++) {
        buffer_words[i] = extract_word(f, &islatin1, &latin1_to_ascii, &offset);
        *allwords = cons_single(buffer_words[i], *allwords);
    }

    INFO("Itération");
    count = 0;
    while((buffer_words[posbuff] = extract_word(f, &islatin1, &latin1_to_ascii,
                    &offset))) {
        *allwords = cons_single(buffer_words[posbuff], *allwords);

        if (count == 0) {
            char **words = malloc(nbr_words*sizeof(char *));
            if (words == NULL) {
                FATAL("Erreur d'allocation mémoire : %s",strerror(errno));
                exit(100);
            }

            for(i = 0; i < nbr_words; i++) {
                words[i] = buffer_words[(posbuff+1+i)%nbr_words];
            }

            if (sorted) {
                add_sorted(build_words(nbr_words, words), &l);
            } else {
                l = cons_new_words(nbr_words, words, l);
            }

            count = skip_words;
        }

        posbuff = (posbuff+1)%nbr_words;
        count -= 1;
    }
    INFO("Fin de la construction de la liste de mots");

    /* Libération de la mémoire */
    DEBUG("Libération du buffer de mots");
    free(buffer_words);

    /* Fermeture du fichier */
    INFO("Fermeture du fichier %s", filepath);
    fclose(f);

    DEBUG("Nombre de séquences : %d", length_list(l));

    return l;
}