/* FILE ADDTTP4_EXPORT.C; exportized version of: */
/* addttp4.c - filter to add TTP pron to a word.                   */
/* (stress marking is not yet implemented.)                        */
/*                                                                 */
/* Synopsis: addttp4 <parms> < x.txt > y.txt,  where:              */
char *parms = "<format_out> <fname_ttp_pcdrls> (-s) (<db_level>)";
/* Process: Applies ttp3s() to words from STDIN, creating a pronlex */
/* entry which is then written out to STDOUT.  Format_out specifies*/
/* the output format for the lexical item.  If the 3rd command line*/
/* parameter is  "-s", some stress marks may be included in the    */
/* pron; otherwise, no stresses are marked.                        */
/* command-line parameters:                                        */
/* par #   contents                                                */
/*   1     FORMAT_OUT ( "SORT1","SORT2", "PCD", or "CMU")          */
/*   2     fname_ttprls (name of file holding ttprls)              */
/*   3     -s for stressed prons (optional)                        */
/*   4     db_level (level of de-bug printf's to do) (optional)    */
/*                                                                 */
/* Author: W.M. Fisher                                             */
/* Created: 4/4/98                                                 */
/* Modified 6/5/98 by WMF to use new version of ttp2s().           */
/* Modified 5/25/00 by WMF to use ttp3s and protected char strings.*/
/*******************************************************************/

/* standard compile environment files: */
/* #include <gp/stdcenvp.h> */ /* NIST version only */
#include "./hdrs/gp/stdcenvp.h" /* export version only */
/* #include <plex/stdcenvp.h> */ /* NIST version only */
#include "./hdrs/plex/stdcenvp.h" /* export version only */

int main(int argc, char *argv[])
 {char *proc = "ADDTTP4 Ver. 1.1";
  time_t t_sec;
  PROTECTED_CHR_STR *rec, *ps_key, *pa, *pb, *pc, *sx, *ttp_input, *ttp_pron;
  char *vocab_comment_flag = "##";
  char *lex_format_out, *fname_ttp_pcdrls, *path, tgbound='.';
  int dbi, err, *perr = &err;
  boolean stress, plus_foreign_loans;
  pcodeset pcdrls_data, *pcdrls = &pcdrls_data;
  struct SYL_PARMS syl_parm_data, *syl_parms = &syl_parm_data;
  struct BPATH bpath_data, *bpath = &bpath_data;
  LEX_ITEM3 itemx, *item = &itemx;
  TTP_PCDRLS_PARAMETERS ttparmx, *ttp_pcdrls_parms = &ttparmx;

/*   code   */
/* initialize, report major parameters */
  t_sec = time(&t_sec);      /* ANSI, Turbo C */
  pdb = strcpy(pdb,"*DB: ");
  rec = create_prot_chr_str(133);
  ps_key = create_prot_chr_str(133);
  pa = create_prot_chr_str(133);
  pb = create_prot_chr_str(133);
  pc = create_prot_chr_str(133);
  sx = create_prot_chr_str(133);
  ttp_input = create_prot_chr_str(133);
  ttp_pron = create_prot_chr_str(133);
  stress = F; /* default value */
  if (argc < 3)
    {fprintf(stderr,"%s: Invalid command line parameters.\n",proc);
     return -1;
    }
  lex_format_out   = argv[1];
  fname_ttp_pcdrls = argv[2];
  dbi = 3;
  if (argc > dbi) /* there are some more parameters */
    {if (*argv[dbi] == '-')
       {if (strstr(argv[dbi],"ns") != NULL)     stress = F;
        else if (strstr(argv[dbi],"s") != NULL) stress = T;
        dbi += 1;
    }  }
  if (argc > dbi) db_level = atoi(argv[dbi]); else db_level = 0;
  if (db_level > 0)
    {fprintf(stderr,"%s Program %s\n",pdb,proc);
     fprintf(stderr," lex_format_out   = '%s'\n",lex_format_out);
     fprintf(stderr," fname_ttp_pcdrls = '%s'\n",fname_ttp_pcdrls);
     fprintf(stderr," stress = '%s'\n",bool_print(stress));
     fprintf(stderr," db_level = %d\n",db_level);
    }

/* get TTP parameters */
  ttp_pcdrls_parms->desc = strdup_safe("Standard TTP3S Parameters",proc);
 /* get ttp rules from ttp pcdrls file */
  make_pcd_from_rls_file(pcdrls,fname_ttp_pcdrls,path="",tgbound,perr);
  if (*perr > 0) fatal_error(proc,"make_pcd_from_rls_file fails",*perr);
  ttp_pcdrls_parms->pcdrls = pcdrls;
 /* get ttp backoff path */
  get_bpath_from_pcdrls1(bpath,pcdrls,tgbound,perr);
  if (*perr > 0) fatal_error(proc,"error return from get_bpath...",*perr);
  if (db_level > 0) dump_bpaths(bpath);
  ttp_pcdrls_parms->bpath = bpath;
 /* get syllabification parameters */
  init_syl_parms(syl_parms,plus_foreign_loans='T', perr);
  if (*perr > 0) fatal_error(proc,"error return from init_syl_parms()",*perr);
  if (db_level > 0) dump_syl_parms(syl_parms);
  ttp_pcdrls_parms->syl_parms = syl_parms;

/* loop on vocabulary words from stdin */
  /*  initialize non-varying fields of lex item struct */
  item->nprons = 1;
  item->pron[1].cats = "*";
  item->pron[1].tags = "";
  item->pron[1].status = "TTP";
  item->tags = "";
  item->status = "RGEN";
  while (fgets_prot(rec,stdin) != NULL)
    {if (!valid_data_line(rec->str,vocab_comment_flag)) continue;
     pltrim(prtrim2(del_eol(rec->str)));
     strcpy_prot_s(ttp_input,"");
     ttp_prep1_prot(ttp_input,rec->str);
     remove_diacritics(ttp_input->str,ttp_input->str);
     ttp_pron = ttp3s_prot(ttp_pron,ttp_input->str,ttp_pcdrls_parms,perr);
     if (ttp_pron == NULL) fatal_error(proc,"TTP changed ttp_pron to NULL!",999);
     if (err > 0) fprintf(stdout,"*ERR: ttp3s_prot() returns err=%d\n",err);
     else
       {item->orth = rec->str;
        orth2key1_prot(ps_key,item->orth,&err);
        if (err > 0) fprintf(stdout,"*ERR: orth2key1_prot() returns err=%d\n",err);
        item->key = ps_key->str;
        item->pron[1].phon = ttp_pron->str;
        write_lex_item(item,stdout,lex_format_out,&err);
        if (err > 0) fprintf(stdout,"*ERR: write_lex_item() returns err=%d\n",err);
    }  }
/* free allocated memory */
  free(ttp_pcdrls_parms->desc);
  free_pcode(ttp_pcdrls_parms->pcdrls);
  free_syl_parms2(syl_parms);
  free_bpath1(bpath);
  destroy_prot_chr_str(rec);
  destroy_prot_chr_str(ps_key);
  destroy_prot_chr_str(pa);
  destroy_prot_chr_str(pb);
  destroy_prot_chr_str(pc);
  destroy_prot_chr_str(sx);
  destroy_prot_chr_str(ttp_input);
  destroy_prot_chr_str(ttp_pron);
  return 0;
 } /* end main */

/******* CODE FOR FUNCTIONS USED, FOR EXPORT VERSION: ************************/
/* (you may uncomment these lines to include the code directly)              */
/***********/
#include "./fcns/gp/addpcd.c"
#include "./fcns/gp/addstres_prot.c"
#include "./fcns/gp/adjcase.c"
#include "./fcns/gp/aprules2.c"
#include "./fcns/gp/aprules2_prot.c"
#include "./fcns/gp/atobool.c"
#include "./fcns/gp/boolpr1.c"
#include "./fcns/gp/build_key3.c"
#include "./fcns/gp/calloc2.c"
#include "./fcns/gp/char_name.c"
#include "./fcns/gp/char_name_prot.c"
#include "./fcns/gp/cocoon1.c"
#include "./fcns/gp/conspace.c"
#include "./fcns/gp/dbpaths.c"
#include "./fcns/gp/dbpkg1.c"
#include "./fcns/gp/decode2.c"
#include "./fcns/gp/del_eol.c"
#include "./fcns/gp/dhpars1.c"
#include "./fcns/gp/dpron2.c"
#include "./fcns/gp/drules2.c"
#include "./fcns/gp/dsylbs1.c"
#include "./fcns/gp/dsylparms.c"
#include "./fcns/gp/dwords1.c"
#include "./fcns/gp/encode2.c"
#include "./fcns/gp/encode2r.c"
#include "./fcns/gp/encode3r.c"
#include "./fcns/gp/expenv.c"
#include "./fcns/gp/fatalerr.c"
#include "./fcns/gp/fbpath1.c"
#include "./fcns/gp/fgets_prot.c"
#include "./fcns/gp/find_key3.c"
#include "./fcns/gp/fntoke1.c"
#include "./fcns/gp/frele.c"
#include "./fcns/gp/frpcode.c"
#include "./fcns/gp/frstr1.c"
#include "./fcns/gp/frules2.c"
#include "./fcns/gp/fsylparms2.c"
#include "./fcns/gp/ftiers2.c"
#include "./fcns/gp/gbpath1.c"
#include "./fcns/gp/gcomflag.c"
#include "./fcns/gp/gnstrtab.c"
#include "./fcns/gp/gpcode3.c"
#include "./fcns/gp/gpcaux.c"
#include "./fcns/gp/gppf2.c"
#include "./fcns/gp/grules2.c"
#include "./fcns/gp/hashfcn.c"
#include "./fcns/gp/ipcdrls.c"
#include "./fcns/gp/isylparms.c"
#include "./fcns/gp/letfcns.c"
#include "./fcns/gp/make_pcd_from_rls_file.c"
#include "./fcns/gp/malloc2.c"
#include "./fcns/gp/mfname1.c"
#include "./fcns/gp/mkele1.c"
#include "./fcns/gp/mkele1r.c"
#include "./fcns/gp/mlower.c"
#include "./fcns/gp/mpron4.c"
#include "./fcns/gp/mupper.c"
#include "./fcns/gp/no_wbndl.c"
#include "./fcns/gp/no_wbndr.c"
#include "./fcns/gp/nstrfcns.c"
#include "./fcns/gp/okfcc2.c"
#include "./fcns/gp/okicc2.c"
#include "./fcns/gp/parse_rkey.c"
#include "./fcns/gp/pchmeth1.c"
#include "./fcns/gp/pcindex2.c"
#include "./fcns/gp/pcindex2r.c"
#include "./fcns/gp/pcindex5r.c"
#include "./fcns/gp/pcindex_bsearch.c"
#include "./fcns/gp/pcindex_char_lookup.c"
#include "./fcns/gp/pcindex_lsearch.c"
#include "./fcns/gp/pcsort1.c"
#include "./fcns/gp/pltrim.c"
#include "./fcns/gp/pltrimf.c"
#include "./fcns/gp/possible_mandarin_byte.c"
#include "./fcns/gp/prand2.c"
#include "./fcns/gp/prtrim.c"
#include "./fcns/gp/prtrim2.c"
#include "./fcns/gp/pronphon.c"
#include "./fcns/gp/prot_nstr_fcns.c"
#include "./fcns/gp/prot_str_fcns.c"
#include "./fcns/gp/rdiacrit.c"
#include "./fcns/gp/remcomm.c"
#include "./fcns/gp/repladd2.c"
#include "./fcns/gp/spmode_prot.c"
#include "./fcns/gp/sptiers2_prot.c"
#include "./fcns/gp/ss_len.c"
#include "./fcns/gp/ss_to_s.c"
#include "./fcns/gp/sstok.c"
#include "./fcns/gp/sstok2.c"
#include "./fcns/gp/sstok4.c"
#include "./fcns/gp/sstok_lr_s.c"
#include "./fcns/gp/str_eq.c"
#include "./fcns/gp/strcmpi.c"
#include "./fcns/gp/strdup2.c"
#include "./fcns/gp/strcutr.c"
#include "./fcns/gp/strmap1.c"
#include "./fcns/gp/strncmpi.c"
#include "./fcns/gp/strseg.c"
#include "./fcns/gp/sylpreds.c"
#include "./fcns/gp/textlen.c"
#include "./fcns/gp/ttp1.c"
#include "./fcns/gp/ttp1s.c"
#include "./fcns/gp/ttp3s_prot.c"
#include "./fcns/gp/ttp_pcdrls2ss_prot.c"
#include "./fcns/gp/ttp_prep1.c"
#include "./fcns/gp/ttp_prep1_prot.c"
#include "./fcns/gp/ttpj1.c"
#include "./fcns/gp/ttpspan2.c"
#include "./fcns/gp/valdata.c"
#include "./fcns/gp/warn_add.c"
#include "./fcns/gp/wbtrim.c"
#include "./fcns/gp/zapchar.c"

#include "./fcns/plex/orth2key1.c"
#include "./fcns/plex/orth2key1_prot.c"
#include "./fcns/plex/s_encode.c"
#include "./fcns/plex/seporth2.c"
#include "./fcns/plex/wrlex1.c"
#include "./fcns/plex/wrlex2.c"
/******/

