TEST(FirstPass, get_label_name) { char a[] = "NAME: .string \"Linus\""; char b[] = ".data +7,-57, 17 , 9"; char c[] = "DATA: .data +7,-57, 17 , 9"; char d[] = ".entry HELLO"; EXPECT_STREQ(get_label_name(a), "NAME"); EXPECT_STREQ(get_label_name(b), NULL); EXPECT_STREQ(get_label_name(c), "DATA"); EXPECT_STREQ(get_label_name(d), NULL); }
/** * The save an instance of TextToClassify structure in an XML file. * The method use the class labels, and has_data lists in order to * assign class labels {-1, 0, +1} * */ bool Classifier::text2xml(TextToClassify* text, list<bool> hasdata, list<pred> labels, string output_fpath) { // Get the number of texts TextToClassify* first_text = text; unsigned int texts_count = 0; bool texts_has_no_id = true; while(text){ texts_has_no_id = texts_has_no_id && (text->iID == 0); text = text->pNext; texts_count++; } //printf("#hasdata=%d, #labels=%d, #texts=%d\n", hasdata.size(), labels.size(), texts_count); // Data are consistent -> write output if((hasdata.size() == texts_count) && (labels.size() == texts_count)){ // Open output file FILE* output_file = fopen(output_fpath.c_str(), "w"); if (!output_file){ printf("Error: Cannot open the file '%s'.\n", output_fpath.c_str()); return false; } // Write each text as an XML element const char* original_text; string lemmas_text; int label; double confidence; int text_num = 1; text = first_text; list<pred>::iterator labels_it = labels.begin(); list<bool>::iterator hasdata_it = hasdata.begin(); fprintf(output_file, "<%s>\n", ROOT_TAG); while(text && labels_it != labels.end() && hasdata_it != hasdata.end()){ label = (*hasdata_it ? (*labels_it).category : NEGATIVE_CLASS_I); confidence = (*hasdata_it ? (*labels_it).probability : 0); fprintf(output_file, "<%s %s='%ld' %s='%s' %s='%0.4f'>\n", TEXT_TAG, ID_ATT, (texts_has_no_id ? text_num : text->iID), CLASS_ATT, get_label_name(label).c_str(), CONFIDENCE_ATT, confidence); original_text = (text->sText ? text->sText : ""); fprintf(output_file, "<%s>%s</%s>\n", ORIGINAL_TAG, original_text, ORIGINAL_TAG); lemmas_text = (text->pToken ? token2string(text->pToken) : ""); fprintf(output_file, "<%s>%s</%s>\n", LEMMAS_TAG, lemmas_text.c_str(), LEMMAS_TAG); fprintf(output_file, "</%s>\n", TEXT_TAG); // Next text text = text->pNext; labels_it++; hasdata_it++; text_num++; } fprintf(output_file, "</%s>", ROOT_TAG); // Close output file fclose(output_file); return true; } // Input data are not consistent else{ printf("Error: Input data are not consistent. Output file was not written.\n"); return false; } }