static vindex read_terms(FILE *fp, si_t si) { size_t i = 0, nsize = 10; vindex v = make_vindex(nsize); si_index term; while ((term = read_cat(fp, si))) { if (i >= nsize) { nsize *= 2; vindex_resize(v, nsize); } assert(i < nsize); vindex_ref(v,i++) = term; } if (i > 0) { v->n = i; vindex_resize(v, v->n); return (v); } else { vindex_free(v); return(NULL); } }
Ut::Ut(QWidget *parent) : QDialog(parent) { qDebug() << "start Ut"; ui.setupUi(this); connect(ui.convert, SIGNAL(clicked()), this, SLOT(read_write())); connect(ui.file, SIGNAL(textChanged(QString)), this, SLOT(newfile())); connect(ui.cat, SIGNAL(textChanged(QString)), this, SLOT(newcat())); connect(ui.observatory, SIGNAL(currentIndexChanged(int)), this, SLOT(observatory(int))); connect(ui.dir, SIGNAL(textChanged(QString)), this, SLOT(newdir())); connect(ui.convert, SIGNAL(pressed()), this, SLOT(processing())); connect(ui.LT2UT, SIGNAL(clicked()), this, SLOT(inpfilename())); connect(ui.ST2UT, SIGNAL(clicked()), this, SLOT(inpfilename())); connect(ui.JD2UT, SIGNAL(clicked()), this, SLOT(inpfilename())); red.setColor(QPalette::Base,QColor(Qt::red)); white.setColor(QPalette::Base,QColor(Qt::white)); // filename = "..\\data\\" + ui.file->text() + "maindata_lt.txt"; int index = 0; read_cfg(index); newfile(); read_cat(); observatory(index); read_dst(); inpfilename(); qDebug() << "stop Ut"; }
/********************** slots *****************************************/ void Ut::newfile() { QString s = ui.dir->text() + ui.file->text() + ui.label4->text(); qDebug() << "newfile:" << s; QFileInfo f(s); if (f.isFile()) { ui.file->setPalette(white); if (read_cat()) { ui.convert->setDisabled(false); filename = s; QString ss; if (ui.LT2UT->isChecked()) ss = "_lt"; else if (ui.JD2UT->isChecked()) ss = "_jd"; else ss = "_st"; s = ui.file->text() + ui.label4->text(); ui.outfile->setText(s.replace(ss, "_ut")); ui.outfile->update(); read_dst(); } else ui.convert->setDisabled(true); } else { ui.file->setPalette(red); ui.convert->setDisabled(true); } }
int main(int argc, char** argv) { int64 nside = 4096; double radius_arcsec = -1; int64 maxmatch=1; int print_dist=0; int verbose=0; const char* file = process_args(argc, argv, &nside, &radius_arcsec, &maxmatch, &print_dist, &verbose); if (verbose) { if (radius_arcsec > 0) wlog("radius: %0.1lf arcsec\n", radius_arcsec); wlog("nside: %ld\n", nside); wlog("maxmatch: %ld\n", maxmatch); wlog("file: %s\n", file); } struct cat* cat = read_cat(file, nside, radius_arcsec, verbose); if (verbose) wlog("processing stream\n"); struct matchstack* matches = matchstack_new(); size_t index=0; double ra=0, dec=0; while (2 == fscanf(stdin,"%lf %lf", &ra, &dec)) { process_radec(cat, ra, dec, matches); print_matches(index, matches, maxmatch, print_dist); index++; } if (verbose) wlog("processed %lu from stream.\n", index); }
int main(int argc, char **argv) { int i; t_opt opts; if (my_opts(argc, argv, &opts)) return (-1); if (argc == 1 || only_opts(argc, argv)) read_cat(opts); for (i = 1; i < argc; i++) { if (argv[i] && argv[i][0] != '-') my_cat(argv[i], opts); else if (argv[i][0] == '-' && my_strlen(argv[i]) == 1) read_cat(opts); } return (0); }
void Ut::newdir() { qDebug() << "newdir"; if(read_cat()) { newfile(); } else { ui.convert->setDisabled(true); ui.convert->update(); } }
static vindex read_terms(int weighted_yields_flag, FILE *fp, si_t si, float *yieldweight) { *yieldweight = 1.0; if (weighted_yields_flag) { int result = fscanf(fp, "%g", yieldweight); if (result == EOF) return(NULL); if (result != 1) { fprintf(stderr, "Error in read_terms in expected_counts.c: " "Unable to read a weight for yield\n"); exit(EXIT_FAILURE); } } { size_t i = 0, nsize = 10; vindex v = make_vindex(nsize); si_index term; while ((term = read_cat(fp, si))) { if (i >= nsize) { nsize *= 2; vindex_resize(v, nsize); } assert(i < nsize); vindex_ref(v,i++) = term; } if (i > 0) { v->n = i; vindex_resize(v, v->n); return (v); } else { vindex_free(v); return(NULL); } } }
/* Manage the whole stuff. */ void makeit() { checkstruct *check; picstruct *dfield, *field,*pffield[MAXFLAG], *wfield,*dwfield; catstruct *imacat; tabstruct *imatab; patternstruct *pattern; static time_t thetime1, thetime2; struct tm *tm; unsigned int modeltype; int nflag[MAXFLAG], nparam2[2], i, nok, ntab, next, ntabmax, forcextflag, nima0,nima1, nweight0,nweight1, npsf0,npsf1, npat,npat0; /* Install error logging */ error_installfunc(write_error); /* Processing start date and time */ dtime = counter_seconds(); thetimet = time(NULL); tm = localtime(&thetimet); sprintf(prefs.sdate_start,"%04d-%02d-%02d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday); sprintf(prefs.stime_start,"%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); NFPRINTF(OUTPUT, ""); QPRINTF(OUTPUT, "----- %s %s started on %s at %s with %d thread%s\n\n", BANNER, MYVERSION, prefs.sdate_start, prefs.stime_start, prefs.nthreads, prefs.nthreads>1? "s":""); /* Initialize globals variables */ initglob(); NFPRINTF(OUTPUT, "Setting catalog parameters"); readcatparams(prefs.param_name); useprefs(); /* update things accor. to prefs parameters */ /* Check if a specific extension should be loaded */ if ((nima0=selectext(prefs.image_name[0])) != RETURN_ERROR) { forcextflag = 1; ntabmax = next = 1; } else forcextflag = 0; /* Do the same for other data (but do not force single extension mode) */ nima1 = selectext(prefs.image_name[1]); nweight0 = selectext(prefs.wimage_name[0]); nweight1 = selectext(prefs.wimage_name[1]); if (prefs.dpsf_flag) { npsf0 = selectext(prefs.psf_name[0]); npsf1 = selectext(prefs.psf_name[1]); } else npsf0 = selectext(prefs.psf_name[0]); for (i=0; i<prefs.nfimage_name; i++) nflag[i] = selectext(prefs.fimage_name[i]); if (prefs.psf_flag) { /*-- Read the first PSF extension to set up stuff such as context parameters */ NFPRINTF(OUTPUT, "Reading PSF information"); if (prefs.dpsf_flag) { thedpsf = psf_load(prefs.psf_name[0],nima0<0? 1 :(npsf0<0? 1:npsf0)); thepsf = psf_load(prefs.psf_name[1], nima1<0? 1 :(npsf1<0? 1:npsf1)); } else thepsf = psf_load(prefs.psf_name[0], nima0<0? 1 :(npsf0<0? 1:npsf0)); /*-- Need to check things up because of PSF context parameters */ updateparamflags(); useprefs(); } if (prefs.prof_flag) { #ifdef USE_MODEL fft_init(prefs.nthreads); /* Create profiles at full resolution */ NFPRINTF(OUTPUT, "Preparing profile models"); modeltype = (FLAG(obj2.prof_offset_flux)? MODEL_BACK : MODEL_NONE) |(FLAG(obj2.prof_dirac_flux)? MODEL_DIRAC : MODEL_NONE) |(FLAG(obj2.prof_spheroid_flux)? (FLAG(obj2.prof_spheroid_sersicn)? MODEL_SERSIC : MODEL_DEVAUCOULEURS) : MODEL_NONE) |(FLAG(obj2.prof_disk_flux)? MODEL_EXPONENTIAL : MODEL_NONE) |(FLAG(obj2.prof_bar_flux)? MODEL_BAR : MODEL_NONE) |(FLAG(obj2.prof_arms_flux)? MODEL_ARMS : MODEL_NONE); theprofit = profit_init(thepsf, modeltype); changecatparamarrays("VECTOR_MODEL", &theprofit->nparam, 1); changecatparamarrays("VECTOR_MODELERR", &theprofit->nparam, 1); nparam2[0] = nparam2[1] = theprofit->nparam; changecatparamarrays("MATRIX_MODELERR", nparam2, 2); if (prefs.dprof_flag) thedprofit = profit_init(thedpsf, modeltype); if (prefs.pattern_flag) { npat0 = prefs.prof_disk_patternvectorsize; if (npat0<prefs.prof_disk_patternmodvectorsize) npat0 = prefs.prof_disk_patternmodvectorsize; if (npat0<prefs.prof_disk_patternargvectorsize) npat0 = prefs.prof_disk_patternargvectorsize; /*---- Do a copy of the original number of pattern components */ prefs.prof_disk_patternncomp = npat0; pattern = pattern_init(theprofit, prefs.pattern_type, npat0); if (FLAG(obj2.prof_disk_patternvector)) { npat = pattern->size[2]; changecatparamarrays("DISK_PATTERN_VECTOR", &npat, 1); } if (FLAG(obj2.prof_disk_patternmodvector)) { npat = pattern->ncomp*pattern->nfreq; changecatparamarrays("DISK_PATTERNMOD_VECTOR", &npat, 1); } if (FLAG(obj2.prof_disk_patternargvector)) { npat = pattern->ncomp*pattern->nfreq; changecatparamarrays("DISK_PATTERNARG_VECTOR", &npat, 1); } pattern_end(pattern); } QPRINTF(OUTPUT, "Fitting model: "); for (i=0; i<theprofit->nprof; i++) { if (i) QPRINTF(OUTPUT, "+"); QPRINTF(OUTPUT, "%s", theprofit->prof[i]->name); } QPRINTF(OUTPUT, "\n"); if (FLAG(obj2.prof_concentration)|FLAG(obj2.prof_concentration)) { thepprofit = profit_init(thepsf, MODEL_DIRAC); theqprofit = profit_init(thepsf, MODEL_EXPONENTIAL); } #else error(EXIT_FAILURE, "*Error*: model-fitting is not supported in this build.\n", " Please check your configure options"); #endif } if (prefs.filter_flag) { NFPRINTF(OUTPUT, "Reading detection filter"); getfilter(prefs.filter_name); /* get the detection filter */ } if (FLAG(obj2.sprob)) { NFPRINTF(OUTPUT, "Initializing Neural Network"); neurinit(); NFPRINTF(OUTPUT, "Reading Neural Network Weights"); getnnw(); } if (prefs.somfit_flag) { int margin; thesom = som_load(prefs.som_name); if ((margin=(thesom->inputsize[1]+1)/2) > prefs.cleanmargin) prefs.cleanmargin = margin; if (prefs.somfit_vectorsize>thesom->neurdim) { prefs.somfit_vectorsize = thesom->neurdim; sprintf(gstr,"%d", prefs.somfit_vectorsize); warning("Dimensionality of the SOM-fit vector limited to ", gstr); } } /* Prepare growth-curve buffer */ if (prefs.growth_flag) initgrowth(); /* Allocate memory for multidimensional catalog parameter arrays */ alloccatparams(); useprefs(); if (!(imacat = read_cat(prefs.image_name[0]))) error(EXIT_FAILURE, "*Error*: cannot open ", prefs.image_name[0]); close_cat(imacat); imatab = imacat->tab; if (!forcextflag) { ntabmax = imacat->ntab; /*-- Compute the number of valid input extensions */ next = 0; for (ntab = 0 ; ntab<imacat->ntab; ntab++, imatab = imatab->nexttab) { /*---- Check for the next valid image extension */ if ((imatab->naxis < 2) || !strncmp(imatab->xtension, "BINTABLE", 8) || !strncmp(imatab->xtension, "ASCTABLE", 8)) continue; next++; } } thecat.next = next; /*-- Init the CHECK-images */ if (prefs.check_flag) { checkenum c; NFPRINTF(OUTPUT, "Initializing check-image(s)"); for (i=0; i<prefs.ncheck_type; i++) if ((c=prefs.check_type[i]) != CHECK_NONE) { if (prefs.check[c]) error(EXIT_FAILURE,"*Error*: 2 CHECK_IMAGEs cannot have the same ", " CHECK_IMAGE_TYPE"); prefs.check[c] = initcheck(prefs.check_name[i], prefs.check_type[i], next); } } NFPRINTF(OUTPUT, "Initializing catalog"); initcat(); /* Initialize XML data */ if (prefs.xml_flag || prefs.cat_type==ASCII_VO) init_xml(next); /* Go through all images */ nok = 0; for (ntab = 0 ; ntab<ntabmax; ntab++, imatab = imatab->nexttab) { /*-- Check for the next valid image extension */ if (!forcextflag && ((imatab->naxis < 2) || !strncmp(imatab->xtension, "BINTABLE", 8) || !strncmp(imatab->xtension, "ASCTABLE", 8))) continue; nok++; /*-- Initial time measurement*/ time(&thetime1); thecat.currext = nok; dfield = field = wfield = dwfield = NULL; if (prefs.dimage_flag) { /*---- Init the Detection and Measurement-images */ dfield = newfield(prefs.image_name[0], DETECT_FIELD, nima0<0? ntab:nima0); field = newfield(prefs.image_name[1], MEASURE_FIELD, nima1<0? ntab:nima1); if ((field->width!=dfield->width) || (field->height!=dfield->height)) error(EXIT_FAILURE, "*Error*: Frames have different sizes",""); /*---- Prepare interpolation */ if (prefs.dweight_flag && prefs.interp_type[0] == INTERP_ALL) init_interpolate(dfield, -1, -1); if (prefs.interp_type[1] == INTERP_ALL) init_interpolate(field, -1, -1); } else { field = newfield(prefs.image_name[0], DETECT_FIELD | MEASURE_FIELD, nima0<0? ntab:nima0); /*---- Prepare interpolation */ if ((prefs.dweight_flag || prefs.weight_flag) && prefs.interp_type[0] == INTERP_ALL) init_interpolate(field, -1, -1); /* 0.0 or anything else */ } /*-- Init the WEIGHT-images */ if (prefs.dweight_flag || prefs.weight_flag) { weightenum wtype; PIXTYPE interpthresh; if (prefs.nweight_type>1) { /*------ Double-weight-map mode */ if (prefs.weight_type[1] != WEIGHT_NONE) { /*-------- First: the "measurement" weights */ wfield = newweight(prefs.wimage_name[1],field,prefs.weight_type[1], (nima1<0 && prefs.image_name[1])? ntab : (nweight1<0?1:nweight1)); wtype = prefs.weight_type[1]; interpthresh = prefs.weight_thresh[1]; /*-------- Convert the interpolation threshold to variance units */ weight_to_var(wfield, &interpthresh, 1); wfield->weight_thresh = interpthresh; if (prefs.interp_type[1] != INTERP_NONE) init_interpolate(wfield, prefs.interp_xtimeout[1], prefs.interp_ytimeout[1]); } /*------ The "detection" weights */ if (prefs.weight_type[0] != WEIGHT_NONE) { interpthresh = prefs.weight_thresh[0]; if (prefs.weight_type[0] == WEIGHT_FROMINTERP) { dwfield=newweight(prefs.wimage_name[0],wfield,prefs.weight_type[0], nima0<0? ntab : (nweight0<0? 1 :nweight0)); weight_to_var(wfield, &interpthresh, 1); } else { dwfield = newweight(prefs.wimage_name[0], dfield?dfield:field, prefs.weight_type[0], nima0<0? ntab : (nweight0<0?1:nweight0)); weight_to_var(dwfield, &interpthresh, 1); } dwfield->weight_thresh = interpthresh; if (prefs.interp_type[0] != INTERP_NONE) init_interpolate(dwfield, prefs.interp_xtimeout[0], prefs.interp_ytimeout[0]); } } else { /*------ Single-weight-map mode */ wfield = newweight(prefs.wimage_name[0], dfield?dfield:field, prefs.weight_type[0], nima0<0? ntab : (nweight0<0?1:nweight0)); wtype = prefs.weight_type[0]; interpthresh = prefs.weight_thresh[0]; /*------ Convert the interpolation threshold to variance units */ weight_to_var(wfield, &interpthresh, 1); wfield->weight_thresh = interpthresh; if (prefs.interp_type[0] != INTERP_NONE) init_interpolate(wfield, prefs.interp_xtimeout[0], prefs.interp_ytimeout[0]); } } /*-- Init the FLAG-images */ for (i=0; i<prefs.nimaflag; i++) { pffield[i] = newfield(prefs.fimage_name[i], FLAG_FIELD, nima0<0? ntab : (nflag[i]<0?1:nflag[i])); if ((pffield[i]->width!=field->width) || (pffield[i]->height!=field->height)) error(EXIT_FAILURE, "*Error*: Incompatible FLAG-map size in ", prefs.fimage_name[i]); } /*-- Compute background maps for `standard' fields */ QPRINTF(OUTPUT, dfield? "Measurement image:" : "Detection+Measurement image: "); makeback(field, wfield, prefs.wscale_flag[1]); QPRINTF(OUTPUT, (dfield || (dwfield&&dwfield->flags^INTERP_FIELD))? "(M) " "Background: %-10g RMS: %-10g / Threshold: %-10g \n" : "(M+D) " "Background: %-10g RMS: %-10g / Threshold: %-10g \n", field->backmean, field->backsig, (field->flags & DETECT_FIELD)? field->dthresh: field->thresh); if (dfield) { QPRINTF(OUTPUT, "Detection image: "); makeback(dfield, dwfield? dwfield : (prefs.weight_type[0] == WEIGHT_NONE?NULL:wfield), prefs.wscale_flag[0]); QPRINTF(OUTPUT, "(D) " "Background: %-10g RMS: %-10g / Threshold: %-10g \n", dfield->backmean, dfield->backsig, dfield->dthresh); } else if (dwfield && dwfield->flags^INTERP_FIELD) { makeback(field, dwfield, prefs.wscale_flag[0]); QPRINTF(OUTPUT, "(D) " "Background: %-10g RMS: %-10g / Threshold: %-10g \n", field->backmean, field->backsig, field->dthresh); } /*-- For interpolated weight-maps, copy the background structure */ if (dwfield && dwfield->flags&(INTERP_FIELD|BACKRMS_FIELD)) copyback(dwfield->reffield, dwfield); if (wfield && wfield->flags&(INTERP_FIELD|BACKRMS_FIELD)) copyback(wfield->reffield, wfield); /*-- Prepare learn and/or associations */ if (prefs.assoc_flag) init_assoc(field); /* initialize assoc tasks */ /*-- Update the CHECK-images */ if (prefs.check_flag) for (i=0; i<MAXCHECK; i++) if ((check=prefs.check[i])) reinitcheck(field, check); if (!forcextflag && nok>1) { if (prefs.psf_flag) { /*------ Read other PSF extensions */ NFPRINTF(OUTPUT, "Reading PSF information"); psf_end(thepsf, thepsfit); if (prefs.dpsf_flag) { psf_end(thedpsf, thedpsfit); thedpsf = psf_load(prefs.psf_name[0], nok); thepsf = psf_load(prefs.psf_name[1], nok); } else thepsf = psf_load(prefs.psf_name[0], nok); } #ifdef USE_MODEL if (prefs.prof_flag) { /*------ Create profiles at full resolution */ profit_end(theprofit); theprofit = profit_init(thepsf, modeltype); if (prefs.dprof_flag) { profit_end(thedprofit); thedprofit = profit_init(thedpsf, modeltype); } if (prefs.pattern_flag) { pattern = pattern_init(theprofit, prefs.pattern_type, npat0); pattern_end(pattern); } if (FLAG(obj2.prof_concentration)|FLAG(obj2.prof_concentration)) { profit_end(thepprofit); profit_end(theqprofit); thepprofit = profit_init(thepsf, MODEL_DIRAC); theqprofit = profit_init(thepsf, MODEL_EXPONENTIAL); } } #endif } /*-- Initialize PSF contexts and workspace */ if (prefs.psf_flag) { psf_readcontext(thepsf, field); psf_init(); if (prefs.dpsf_flag) { psf_readcontext(thepsf, dfield); psf_init(); } } /*-- Copy field structures to static ones (for catalog info) */ if (dfield) { thefield1 = *field; thefield2 = *dfield; } else thefield1 = thefield2 = *field; if (wfield) { thewfield1 = *wfield; thewfield2 = dwfield? *dwfield: *wfield; } else if (dwfield) thewfield2 = *dwfield; reinitcat(field); /*-- Start the extraction pipeline */ NFPRINTF(OUTPUT, "Scanning image"); scanimage(field, dfield, pffield, prefs.nimaflag, wfield, dwfield); /*-- Finish the current CHECK-image processing */ if (prefs.check_flag) for (i=0; i<MAXCHECK; i++) if ((check=prefs.check[i])) reendcheck(field, check); /*-- Final time measurements*/ if (time(&thetime2)!=-1) { if (!strftime(thecat.ext_date, 12, "%d/%m/%Y", localtime(&thetime2))) error(EXIT_FAILURE, "*Internal Error*: Date string too long ",""); if (!strftime(thecat.ext_time, 10, "%H:%M:%S", localtime(&thetime2))) error(EXIT_FAILURE, "*Internal Error*: Time/date string too long ",""); thecat.ext_elapsed = difftime(thetime2, thetime1); } reendcat(); /* Update XML data */ if (prefs.xml_flag || prefs.cat_type==ASCII_VO) update_xml(&thecat, dfield? dfield:field, field, dwfield? dwfield:wfield, wfield); /*-- Close ASSOC routines */ end_assoc(field); for (i=0; i<prefs.nimaflag; i++) endfield(pffield[i]); endfield(field); if (dfield) endfield(dfield); if (wfield) endfield(wfield); if (dwfield) endfield(dwfield); QPRINTF(OUTPUT, " Objects: detected %-8d / sextracted %-8d \n\n", thecat.ndetect, thecat.ntotal); } if (nok<=0) error(EXIT_FAILURE, "Not enough valid FITS image extensions in ", prefs.image_name[0]); free_cat(&imacat, 1); NFPRINTF(OUTPUT, "Closing files"); /* End CHECK-image processing */ if (prefs.check_flag) for (i=0; i<MAXCHECK; i++) { if ((check=prefs.check[i])) endcheck(check); prefs.check[i] = NULL; } if (prefs.filter_flag) endfilter(); if (prefs.somfit_flag) som_end(thesom); if (prefs.growth_flag) endgrowth(); #ifdef USE_MODEL if (prefs.prof_flag) { profit_end(theprofit); if (prefs.dprof_flag) profit_end(thedprofit); if (FLAG(obj2.prof_concentration)|FLAG(obj2.prof_concentration)) { profit_end(thepprofit); profit_end(theqprofit); } fft_end(); } #endif if (prefs.psf_flag) psf_end(thepsf, thepsfit); if (prefs.dpsf_flag) psf_end(thedpsf, thedpsfit); if (FLAG(obj2.sprob)) neurclose(); /* Processing end date and time */ thetimet2 = time(NULL); tm = localtime(&thetimet2); sprintf(prefs.sdate_end,"%04d-%02d-%02d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday); sprintf(prefs.stime_end,"%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); prefs.time_diff = counter_seconds() - dtime; /* Write XML */ if (prefs.xml_flag) write_xml(prefs.xml_name); endcat((char *)NULL); if (prefs.xml_flag || prefs.cat_type==ASCII_VO) end_xml(); return; }
grammar read_grammar(FILE *fp, si_t si) { sihashbrs left_brules_ht = make_sihashbrs(NLABELS); sihashurs child_urules_ht = make_sihashurs(NLABELS); sihashf parent_weight_ht = make_sihashf(NLABELS); brihashbr brihtbr = make_brihashbr(NLABELS); int n; double weight; urule ur; sihashbrsit bhit; sihashursit uhit; size_t root_label = 0, lhs, cat, rhs[MAXRHS]; while ((n = fscanf(fp, " %lg ", &weight)) == 1) { /* read the count */ lhs = read_cat(fp, si); assert(weight > 0); assert(lhs); if (!root_label) root_label = lhs; fscanf(fp, " " REWRITES); /* read the rewrites symbol */ for (n=0; n<MAXRHS; n++) { /* read the rhs, n is length of rhs */ cat = read_cat(fp, si); if (!cat) break; rhs[n] = cat; } if (n >= MAXRHS) { fprintf(stderr, "read_grammar() in grammar.c: rule rhs too long\n"); exit(EXIT_FAILURE); } switch (n) { case 0: fprintf(stderr, "read_grammar() in grammar.c: rule with empty rhs\n"); exit(EXIT_FAILURE); break; case 1: ur = make_urule(weight, lhs, rhs[0]); push_urule(child_urules_ht, ur->child, ur); sihashf_inc(parent_weight_ht, ur->parent, weight); break; case 2: add_brule(left_brules_ht, brihtbr, weight, lhs, rhs[0], rhs[1]); sihashf_inc(parent_weight_ht, lhs, weight); break; default: { int start, i, j; char bcat[MAXBLABELLEN], *s; si_index bparent, left, right; right = rhs[n-1]; /* rightmost category */ for (start=n-2; start>=1; start--) { i = 0; /* i is index into bcat[] */ for (j=start; j<n; j++) { /* j is index into rhs[] */ if (j!=start) { bcat[i++] = BINSEP; assert(i < MAXBLABELLEN); } s = si_index_string(si, rhs[j]); while (*s) { bcat[i++] = *s++; assert(i < MAXBLABELLEN); }} bcat[i] = '\0'; bparent = si_string_index(si, bcat); left = rhs[start]; add_brule(left_brules_ht, brihtbr, weight, bparent, left, right); sihashf_inc(parent_weight_ht, bparent, weight); right = bparent; } add_brule(left_brules_ht, brihtbr, weight, lhs, rhs[0], right); sihashf_inc(parent_weight_ht, lhs, weight); }}} free_brihashbr(brihtbr); /* free brindex hash table */ { int i; /* normalize grammar rules */ for (bhit = sihashbrsit_init(left_brules_ht); sihashbrsit_ok(bhit); bhit = sihashbrsit_next(bhit)) for (i=0; i<bhit.value.n; i++) bhit.value.e[i]->prob /= sihashf_ref(parent_weight_ht, bhit.value.e[i]->parent); for (uhit = sihashursit_init(child_urules_ht); sihashursit_ok(uhit); uhit = sihashursit_next(uhit)) for (i=0; i<uhit.value.n; i++) uhit.value.e[i]->prob /= sihashf_ref(parent_weight_ht, uhit.value.e[i]->parent); } free_sihashf(parent_weight_ht); { grammar g; g.urs = child_urules_ht; g.brs = left_brules_ht; g.root_label = root_label; return g; } }