/* file mkele1.c */

#if !defined(COMPILE_ENVIRONMENT)
#include <gp/stdcenvf.h> /* std compile environment for functions */
#endif

  pcode_element *make_elementr(pcode_element *px, char *symb,
   pcodeset *pc1, TTP_PCDRLS_PARAMETERS *ttp_pcdrls_parms)

  /*************************************************************************/
  /*  Makes a pcode element *px, using spelling <symb>, pcodeset *pc1, and */
  /* TTP rule parameters *ttp_pcdrls_parms.                                */
  /*  Returns px; or NULL to signify fatal error.                          */
  /* Uses protected character strings internally.                          */
  /*************************************************************************/

   {char *proc = "make_elementr";
    PROTECTED_CHR_STR *ttp_input;
    PROTECTED_CHR_STR *ps_pronx;
    PROTECTED_CHR_STR *ps_pron;
    PROTECTED_INT_STR *pns_pron;
    int err, *perr = &err;
    RULESET2 *rset;
/* code */
 db_enter_msg(proc,0); /* debug only */
    *perr = 0;
if (db_level > 1) printf("%s pc1->compositional = %s\n",proc,bool_print(pc1->compositional));
if (db_level > 1) printf("%s pc1->element_type  = %s\n",proc,pc1->element_type);
   /* initialization */
    ttp_input = create_prot_chr_str(133);
    ps_pronx  = create_prot_chr_str(133);
    ps_pron   = create_prot_chr_str(133);
    pns_pron  = create_prot_int_str(35);
    px->ac = strdup_safe(symb,proc);
    px->lc_ext = NULL;
    px->lc_int = NULL;

if (db_level > 0) printf("%s symb to process is '%s'\n",pdb,symb);
if (db_level > 2) printf("%s pc1->desc:[%s]\n",pdb,pc1->desc);

   /* then go */
    if (pc1->compositional)
       {if (streq(pc1->element_type,"word"))
          {if (ttp_pcdrls_parms == NULL) fatal_error(proc,"NULL ttp_pcdrls_parms",-1);
          /* make lower code external rep in ps_pron using text-to-phone rules */
if (db_level > 2) printf("%s processing a compositional word code\n",pdb);
           strcpy_prot_s(ttp_input,"");
           ttp_prep1_prot(ttp_input,px->ac);
           remove_diacritics(ttp_input->str,ttp_input->str);
if (db_level > 2) printf("%s input prepped, calling ttp3s_prot\n",pdb);
           ps_pron = ttp3s_prot(ps_pron,ttp_input->str,ttp_pcdrls_parms,perr);
if (db_level > 2) printf("%s returned from ttp3s_prot\n",pdb);
           if (ps_pron == NULL) fatal_error(proc,"TTP changed ps_pron to NULL!",999);

if (db_level > 2) printf("%s returned from TTP*, output ps_pron->str='%s'\n",pdb,ps_pron->str); 
if (db_level > 2) printf("%s pc1->lower_code->name='%s'\n",pdb,pc1->lower_code->name);

           if (textlen(ps_pron->str) < 1) fprintf(stderr,
              "*WARNING:%s: ttp('%s') returns nothing.\n",proc,px->ac);
         /* adjust ASCII pronunciation in ps_pron */
           if (pc1->lower_code == NULL)
             {fprintf(stderr,"*ERR: %s: a lower-code",proc);
              fprintf(stderr," representation cannot be encoded.\n");
              fprintf(stderr," No lower pcode is defined.\n");
              fprintf(stderr," pcode name='%s', entry='%s' = /%s/\n",
                   pc1->name, px->ac,ps_pronx->str);
              *perr = 13; goto EXIT;
             }
           rset = pc1->lower_code->from_stn_rules;
           if (rset != NULL) /* apply rules to change code from the stn made by ttp* */
             {strcpy_prot_s(ps_pronx,"");
              ps_pronx = apply_rules2_prot(ps_pronx,ps_pron->str,rset,perr);
if (db_level > 2) printf("%s rule output='%s'\n",pdb,ps_pronx->str);
              if (*perr > 0)
                {fprintf(stderr,"*ERR:%s: apply_rules2 err %d\n",proc,*perr);
                 fprintf(stderr," applying ruleset '%s' to:\n",rset->name);
                 fprintf(stderr,"'%s'\n",ps_pron->str);
                 goto EXIT;
                }
              strcpy_prot_p(ps_pron,ps_pronx);
             }
           wbtrim(ps_pron->str); /* remove initial & final word boundaries */
         /* save ps_pron as lc_ext (lower code external (ASCII) representation) */
           px->lc_ext = strdup_safe(ps_pron->str,proc);
         /* encode it to produce lc_int (lower code internal (INT) representation */
           encode3r(px->lc_ext,pc1->lower_code,ttp_pcdrls_parms,pns_pron,perr);
           if (*perr > 0)
             {fprintf(stderr,"*ERR: %s: a lower-code",proc);
              fprintf(stderr," representation cannot be encoded.\n");
              fprintf(stderr," encode3r returns err=%d\n",*perr);
              fprintf(stderr," lower pcode name='%s', entry='%s' = /%s/\n",
                   pc1->lower_code->name, px->ac,px->lc_ext);
              *perr = 11; goto EXIT;
             }
if (db_level > 0) printf("%s added w/rule-pron =[%s]\n",pdb,px->lc_ext);
           px->lc_int = nstrdup_safe(pns_pron->str,proc);
       }  }
  EXIT:
    destroy_prot_chr_str(ttp_input);
    destroy_prot_chr_str(ps_pronx);
    destroy_prot_chr_str(ps_pron);
    destroy_prot_int_str(pns_pron);
 db_leave_msg(proc,0); /* debug only */
    if ((px->ac == NULL)||(*perr > 0)) return NULL;
    else return px;
   }
/* end mkele1r.c */
