diff options
author | Ulrich Germann <ugermann@inf.ed.ac.uk> | 2015-05-11 02:39:45 +0300 |
---|---|---|
committer | Ulrich Germann <ugermann@inf.ed.ac.uk> | 2015-05-11 02:39:45 +0300 |
commit | 25ec936adf6dd2d9ab5ceeda3b5da356d8206a5d (patch) | |
tree | 63771a5b737a4784ff67e464abb2b6aea437c0d7 | |
parent | 7da7ce52dab34fda4f2fcae2b37a7696eb773b2c (diff) | |
parent | 4f02f19ab43f8f566bc621943a2be8fa95edf45c (diff) |
Merge branch 'master' of https://github.com/moses-smt/mosesdecoder
-rw-r--r-- | moses/ContextScope.h | 89 | ||||
-rw-r--r-- | moses/FF/LexicalReordering/LexicalReordering.cpp | 19 | ||||
-rwxr-xr-x | scripts/ems/experiment.perl | 3 | ||||
-rwxr-xr-x | scripts/training/train-model.perl | 12 | ||||
-rw-r--r-- | symal/cmd.c | 849 | ||||
-rw-r--r-- | symal/cmd.h | 42 | ||||
-rw-r--r-- | symal/symal.cpp | 207 |
7 files changed, 597 insertions, 624 deletions
diff --git a/moses/ContextScope.h b/moses/ContextScope.h index cd12d6d32..e1173cbdc 100644 --- a/moses/ContextScope.h +++ b/moses/ContextScope.h @@ -20,20 +20,20 @@ namespace Moses { class ContextScope { - protected: - typedef std::map<void const*, boost::shared_ptr<void> > scratchpad_t; - typedef scratchpad_t::iterator iter_t; - typedef scratchpad_t::value_type entry_t; - typedef scratchpad_t::const_iterator const_iter_t; - scratchpad_t m_scratchpad; +protected: + typedef std::map<void const*, boost::shared_ptr<void> > scratchpad_t; + typedef scratchpad_t::iterator iter_t; + typedef scratchpad_t::value_type entry_t; + typedef scratchpad_t::const_iterator const_iter_t; + scratchpad_t m_scratchpad; #ifdef WITH_THREADS - mutable boost::shared_mutex m_lock; + mutable boost::shared_mutex m_lock; #endif - public: - // class write_access - // { - // boost::unique_lock<boost::shared_mutex> m_lock; - // public: +public: + // class write_access + // { + // boost::unique_lock<boost::shared_mutex> m_lock; + // public: // write_access(boost::shared_mutex& lock) // : m_lock(lock) @@ -50,53 +50,48 @@ class ContextScope // return write_access(m_lock); // } - template<typename T> - boost::shared_ptr<void> const& - set(void const* const key, boost::shared_ptr<T> const& val) - { + template<typename T> + boost::shared_ptr<void> const& + set(void const* const key, boost::shared_ptr<T> const& val) { #ifdef WITH_THREADS - boost::unique_lock<boost::shared_mutex> lock(m_lock); + boost::unique_lock<boost::shared_mutex> lock(m_lock); #endif - return (m_scratchpad[key] = val); - } + return (m_scratchpad[key] = val); + } - template<typename T> - boost::shared_ptr<T> const - get(void const* key, bool CreateNewIfNecessary=false) - { -#ifdef WITH_THREADS - using boost::shared_mutex; - using boost::upgrade_lock; - // T const* key = reinterpret_cast<T const*>(xkey); - upgrade_lock<shared_mutex> lock(m_lock); - #endif - iter_t m = m_scratchpad.find(key); - boost::shared_ptr< T > ret; - if (m != m_scratchpad.end()) - { - if (m->second == NULL && CreateNewIfNecessary) - { + template<typename T> + boost::shared_ptr<T> const + get(void const* key, bool CreateNewIfNecessary=false) { #ifdef WITH_THREADS - boost::upgrade_to_unique_lock<shared_mutex> xlock(lock); + using boost::shared_mutex; + using boost::upgrade_lock; + // T const* key = reinterpret_cast<T const*>(xkey); + upgrade_lock<shared_mutex> lock(m_lock); #endif - m->second.reset(new T); - } - ret = boost::static_pointer_cast< T >(m->second); - return ret; - } - if (!CreateNewIfNecessary) return ret; + iter_t m = m_scratchpad.find(key); + boost::shared_ptr< T > ret; + if (m != m_scratchpad.end()) { + if (m->second == NULL && CreateNewIfNecessary) { #ifdef WITH_THREADS - boost::upgrade_to_unique_lock<shared_mutex> xlock(lock); + boost::upgrade_to_unique_lock<shared_mutex> xlock(lock); #endif - ret.reset(new T); - m_scratchpad[key] = ret; + m->second.reset(new T); + } + ret = boost::static_pointer_cast< T >(m->second); return ret; } + if (!CreateNewIfNecessary) return ret; +#ifdef WITH_THREADS + boost::upgrade_to_unique_lock<shared_mutex> xlock(lock); +#endif + ret.reset(new T); + m_scratchpad[key] = ret; + return ret; + } ContextScope() { } - ContextScope(ContextScope const& other) - { + ContextScope(ContextScope const& other) { #ifdef WITH_THREADS boost::unique_lock<boost::shared_mutex> lock1(this->m_lock); boost::unique_lock<boost::shared_mutex> lock2(other.m_lock); diff --git a/moses/FF/LexicalReordering/LexicalReordering.cpp b/moses/FF/LexicalReordering/LexicalReordering.cpp index 74ba3bf33..fc8258631 100644 --- a/moses/FF/LexicalReordering/LexicalReordering.cpp +++ b/moses/FF/LexicalReordering/LexicalReordering.cpp @@ -140,17 +140,14 @@ SetCache(TranslationOption& to) const if (to.GetLexReorderingScores(this)) return; // Scores were were set already (e.g., by sampling phrase table) - if (m_table) - { - Phrase const& sphrase = to.GetInputPath().GetPhrase(); - Phrase const& tphrase = to.GetTargetPhrase(); - to.CacheLexReorderingScores(*this, this->GetProb(sphrase,tphrase)); - } - else // e.g. OOV with Mmsapt - { - Scores vals(GetNumScoreComponents(), 0); - to.CacheLexReorderingScores(*this, vals); - } + if (m_table) { + Phrase const& sphrase = to.GetInputPath().GetPhrase(); + Phrase const& tphrase = to.GetTargetPhrase(); + to.CacheLexReorderingScores(*this, this->GetProb(sphrase,tphrase)); + } else { // e.g. OOV with Mmsapt + Scores vals(GetNumScoreComponents(), 0); + to.CacheLexReorderingScores(*this, vals); + } } LRModel const& diff --git a/scripts/ems/experiment.perl b/scripts/ems/experiment.perl index 8e23b7b18..5d68e409c 100755 --- a/scripts/ems/experiment.perl +++ b/scripts/ems/experiment.perl @@ -2438,9 +2438,8 @@ sub get_config_tables { my $mmsapt = &get("TRAINING:mmsapt"); if (defined($mmsapt)) { $ptImpl = 11; # mmsapt - $mmsapt =~ s/num-features=(\d+) // || die("ERROR: mmsapt setting needs to set num-features"); $numFF = $1; - $cmd .= "-mmsapt '$mmsapt' "; + $cmd .= "-mmsapt "; } # additional settings for factored models diff --git a/scripts/training/train-model.perl b/scripts/training/train-model.perl index 4c355479c..e6eacfd98 100755 --- a/scripts/training/train-model.perl +++ b/scripts/training/train-model.perl @@ -228,7 +228,7 @@ $_HELP = 1 'osm-setting=s' => \$_OSM_FACTORS, 'post-decoding-translit=s' => \$_POST_DECODING_TRANSLIT, 'transliteration-phrase-table=s' => \$_TRANSLITERATION_PHRASE_TABLE, - 'mmsapt=s' => \$_MMSAPT, + 'mmsapt' => \$_MMSAPT, 'max-lexical-reordering' => \$_MAX_LEXICAL_REORDERING, 'lexical-reordering-default-scores=s' => \$_LEXICAL_REORDERING_DEFAULT_SCORES, 'do-steps=s' => \$_DO_STEPS, @@ -2115,11 +2115,13 @@ sub create_ini { # sum up... $feature_spec .= "$phrase_table_impl_name name=TranslationModel$i num-features=$basic_weight_count path=$file input-factor=$input_factor output-factor=$output_factor"; - $feature_spec .= " L1=$___F L2=$___E ".$_MMSAPT if defined($_MMSAPT); # extra settings for memory mapped suffix array phrase table + $feature_spec .= " L1=$___F L2=$___E " if defined($_MMSAPT); # extra settings for memory mapped suffix array phrase table $feature_spec .= "\n"; - $weight_spec .= "TranslationModel$i="; - for(my $j=0;$j<$basic_weight_count;$j++) { $weight_spec .= " 0.2"; } - $weight_spec .= "\n"; + unless ($phrase_table_impl==11) { # suffix array provides its weights at first iteration + $weight_spec .= "TranslationModel$i="; + for(my $j=0;$j<$basic_weight_count;$j++) { $weight_spec .= " 0.2"; } + $weight_spec .= "\n"; + } $i++; } diff --git a/symal/cmd.c b/symal/cmd.c index 149fc7290..8d5f7a241 100644 --- a/symal/cmd.c +++ b/symal/cmd.c @@ -1,6 +1,7 @@ // $Id$ +#include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> @@ -13,7 +14,15 @@ # define pclose _pclose #endif -static Enum_T BoolEnum[] = { +typedef struct { + enum CommandType Type; + const char *Name, + *ArgStr; + void *Val; + const void *p; +} Cmd_T; + +static const Enum_T BoolEnum[] = { { "FALSE", 0 }, { "TRUE", 1 }, { 0, 0 } @@ -29,54 +38,95 @@ char *strdup(); #define LINSIZ 10240 #define MAXPARAM 256 -static char *GetLine(), - **str2array(); -static int Scan(), - SetParam(), - SetEnum(), - SetSubrange(), - SetStrArray(), - SetGte(), - SetLte(), - CmdError(), - EnumError(), - SubrangeError(), - GteError(), - LteError(), - PrintParam(), - PrintEnum(), - PrintStrArray(); - static Cmd_T cmds[MAXPARAM+1]; -static char *SepString = " \t\n"; +static const char *SepString = " \t\n"; -#if defined(__STDC__) -#include <stdarg.h> -int DeclareParams(char *ParName, ...) -#else -#include <varargs.h> -int DeclareParams(ParName, va_alist) -char *ParName; -va_dcl -#endif +/// Return cmd->p, as an int. +static int get_p_int(const Cmd_T *cmd) +{ + return *(const int *)cmd->p; +} + +/// Return cmd->p, as a pointer to a null-terminated array of Enum_T. +static const Enum_T *get_p_enums(const Cmd_T *cmd) +{ + return (const Enum_T *)cmd->p; +} + +/// Return cmd->p, as a pointer to a string. +static const char *get_p_char(const Cmd_T *cmd) +{ + return (const char *)cmd->p; +} + +/// Return cmd->p, as a pointer to an array of two ints. +static const int *get_p_range(const Cmd_T *cmd) +{ + return (const int *)cmd->p; +} + +/// Return cmd->Val, as a pointer to int. +static int *get_val_int_ptr(const Cmd_T *cmd) +{ + return (int *)cmd->Val; +} + +/// Return the int at which cmd->Val points. +static int get_val_int(const Cmd_T *cmd) +{ + return *get_val_int_ptr(cmd); +} + +/// Update the int at which cmd->Val points. +static void update_val_int(const Cmd_T *cmd, int value) +{ + *get_val_int_ptr(cmd) = value; +} + +/// Return cmd->Val, as a pointer to double. +static double *get_val_double_ptr(const Cmd_T *cmd) +{ + return (double *)cmd->Val; +} + +/// Return the double at which cmd->Val points. +static double get_val_double(const Cmd_T *cmd) +{ + return *get_val_double_ptr(cmd); +} + +/// Return cmd->Val as a pointer to a string pointer. +static const char **get_val_char_ptr(const Cmd_T *cmd) +{ + return (const char **)cmd->Val; +} + +/// Return the string pointer at which cmd->Val points. +static const char *get_val_char(const Cmd_T *cmd) +{ + return *get_val_char_ptr(cmd); +} + +/// Update the string pointer at which cmd->Val points. +static void update_val_char(const Cmd_T *cmd, const char *s) +{ + *get_val_char_ptr(cmd) = s; +} + +int DeclareParams(const char *ParName, ...) { va_list args; static int ParamN = 0; - int j, - c; - char *s; -#if defined(__STDC__) va_start(args, ParName); -#else - va_start(args); -#endif for(;ParName;) { + int c, + j = 0; if(ParamN==MAXPARAM) { fprintf(stderr, "Too many parameters !!\n"); break; } - for(j=0,c=1; j<ParamN&&(c=strcmp(cmds[j].Name,ParName))<0; j++) + for(c=1; j<ParamN&&(c=strcmp(cmds[j].Name,ParName))<0; j++) ; if(!c) { fprintf(stderr, @@ -87,25 +137,33 @@ va_dcl cmds[c] = cmds[c-1]; } cmds[j].Name = ParName; - cmds[j].Type = va_arg(args, int); + cmds[j].Type = va_arg(args, enum CommandType); cmds[j].Val = va_arg(args, void *); switch(cmds[j].Type) { case CMDENUMTYPE: /* get the pointer to Enum_T struct */ cmds[j].p = va_arg(args, void *); break; case CMDSUBRANGETYPE: /* get the two extremes */ - cmds[j].p = (void*) calloc(2, sizeof(int)); - ((int*)cmds[j].p)[0] = va_arg(args, int); - ((int*)cmds[j].p)[1] = va_arg(args, int); + { + int *subrange = calloc(2, sizeof(int)); + cmds[j].p = subrange; + subrange[0] = va_arg(args, int); + subrange[1] = va_arg(args, int); + } break; case CMDGTETYPE: /* get lower or upper bound */ case CMDLTETYPE: - cmds[j].p = (void*) calloc(1, sizeof(int)); - ((int*)cmds[j].p)[0] = va_arg(args, int); + { + int *value = calloc(1, sizeof(int)); + cmds[j].p = value; + value[0] = va_arg(args, int); + } break; case CMDSTRARRAYTYPE: /* get the separators string */ - cmds[j].p = (s=va_arg(args, char*)) - ? (void*)strdup(s) : 0; + { + const char *s = va_arg(args, const char *); + cmds[j].p = (s ? strdup(s) : NULL); + } break; case CMDBOOLTYPE: cmds[j].Type = CMDENUMTYPE; @@ -122,280 +180,196 @@ va_dcl exit(1); } ParamN++; - ParName = va_arg(args, char *); + ParName = va_arg(args, const char *); } cmds[ParamN].Name = NULL; va_end(args); return 0; } -int GetParams(n, a, CmdFileName) -int *n; -char ***a; -char *CmdFileName; +static char *GetLine(FILE *fp, int n, char *Line) { - char *Line, - *ProgName; - int argc = *n; - char **argv = *a, - *s; - FILE *fp; - int IsPipe; + int offs=0; -#ifdef MSDOS -#define PATHSEP '\\' - char *dot = NULL; -#else -#define PATHSEP '/' -#endif - - if(!(Line=malloc(LINSIZ))) { - fprintf(stderr, "GetParams(): Unable to alloc %d bytes\n", - LINSIZ); - exit(1); - } - if((ProgName=strrchr(*argv, PATHSEP))) { - ++ProgName; - } else { - ProgName = *argv; - } -#ifdef MSDOS - if(dot=strchr(ProgName, '.')) *dot = 0; -#endif - --argc; - ++argv; for(;;) { - if(argc && argv[0][0]=='-' && argv[0][1]=='=') { - CmdFileName = argv[0]+2; - ++argv; - --argc; - } - if(!CmdFileName) { - break; - } - IsPipe = !strncmp(CmdFileName, "@@", 2); - fp = IsPipe - ? popen(CmdFileName+2, "r") - : strcmp(CmdFileName, "-") - ? fopen(CmdFileName, "r") - : stdin; - if(!fp) { - fprintf(stderr, "Unable to open command file %s\n", - CmdFileName); - exit(1); + int j, l; + if(!fgets(Line+offs, n-offs, fp)) { + return NULL; } - while(GetLine(fp, LINSIZ, Line) && strcmp(Line, "\\End")) { - if(Scan(ProgName, cmds, Line)) { - CmdError(Line); - } + if(Line[offs]=='#') continue; + l = strlen(Line+offs)-1; + Line[offs+l] = 0; + for(j=offs; Line[j] && isspace(Line[j]); j++, l--) + ; + if(l<1) continue; + if(j > offs) { + char *s = Line+offs, + *q = Line+j; + + while((*s++=*q++)) + ; } - if(fp!=stdin) { - if(IsPipe) pclose(fp); else fclose(fp); + if(Line[offs+l-1]=='\\') { + offs += l; + Line[offs-1] = ' '; + } else { + break; } - CmdFileName = NULL; } - while(argc && **argv=='-' && (s=strchr(*argv, '='))) { - *s = ' '; - sprintf(Line, "%s/%s", ProgName, *argv+1); - *s = '='; - if(Scan(ProgName, cmds, Line)) CmdError(*argv); - --argc; - ++argv; - } - *n = argc; - *a = argv; -#ifdef MSDOS - if(dot) *dot = '.'; -#endif - free(Line); - return 0; + return Line; } -int PrintParams(ValFlag, fp) -int ValFlag; -FILE *fp; +static void EnumError(const Cmd_T *cmd, const char *s) { - int i; + const Enum_T *en; - fflush(fp); - if(ValFlag) { - fprintf(fp, "Parameters Values:\n"); - } else { - fprintf(fp, "Parameters:\n"); + fprintf(stderr, + "Invalid value \"%s\" for parameter \"%s\"\n", s, cmd->Name); + fprintf(stderr, "Valid values are:\n"); + for(en=get_p_enums(cmd); en->Name; en++) { + if(*en->Name) { + fprintf(stderr, " %s\n", en->Name); + } } - for(i=0; cmds[i].Name; i++) PrintParam(cmds+i, ValFlag, fp); - fprintf(fp, "\n"); - fflush(fp); - return 0; + fprintf(stderr, "\n"); + exit(1); } -int SPrintParams(a, pfx) -char ***a, - *pfx; +static void GteError(const Cmd_T *cmd, int n) { - int l, - n; - Cmd_T *cmd; + fprintf(stderr, + "Value %d out of range for parameter \"%s\"\n", n, cmd->Name); + fprintf(stderr, "Valid values must be greater than or equal to %d\n", + get_p_int(cmd)); + exit(1); +} - if(!pfx) pfx=""; - l = strlen(pfx); - for(n=0, cmd=cmds; cmd->Name; cmd++) n += !!cmd->ArgStr; - a[0] = calloc(n, sizeof(char*)); - for(n=0, cmd=cmds; cmd->Name; cmd++) { - if(!cmd->ArgStr) continue; - a[0][n] = malloc(strlen(cmd->Name)+strlen(cmd->ArgStr)+l+2); - sprintf(a[0][n], "%s%s=%s", pfx, cmd->Name, cmd->ArgStr); - ++n; - } - return n; +static void LteError(const Cmd_T *cmd, int n) +{ + fprintf(stderr, + "Value %d out of range for parameter \"%s\"\n", n, cmd->Name); + fprintf(stderr, "Valid values must be less than or equal to %d\n", + get_p_int(cmd)); + exit(1); } -static int CmdError(opt) -char *opt; +static void SubrangeError(const Cmd_T *cmd, int n) { - fprintf(stderr, "Invalid option \"%s\"\n", opt); - fprintf(stderr, "This program expectes the following parameters:\n"); - PrintParams(FALSE, stderr); - exit(0); + const int *subrange = get_p_range(cmd); + fprintf(stderr, + "Value %d out of range for parameter \"%s\"\n", n, cmd->Name); + fprintf(stderr, "Valid values range from %d to %d\n", + subrange[0], subrange[1]); + exit(1); } -static int PrintParam(cmd, ValFlag, fp) -Cmd_T *cmd; -int ValFlag; -FILE *fp; +static void SetEnum(Cmd_T *cmd, const char *s) { - fprintf(fp, "%4s", ""); - switch(cmd->Type) { - case CMDDOUBLETYPE: - fprintf(fp, "%s", cmd->Name); - if(ValFlag) fprintf(fp, ": %22.15e", *(double *)cmd->Val); - fprintf(fp, "\n"); - break; - case CMDENUMTYPE: - PrintEnum(cmd, ValFlag, fp); - break; - case CMDINTTYPE: - case CMDSUBRANGETYPE: - case CMDGTETYPE: - case CMDLTETYPE: - fprintf(fp, "%s", cmd->Name); - if(ValFlag) fprintf(fp, ": %d", *(int *)cmd->Val); - fprintf(fp, "\n"); - break; - case CMDSTRINGTYPE: - fprintf(fp, "%s", cmd->Name); - if(ValFlag) { - if(*(char **)cmd->Val) { - fprintf(fp, ": \"%s\"", *(char **)cmd->Val); - } else { - fprintf(fp, ": %s", "NULL"); - } + const Enum_T *en; + + for(en=get_p_enums(cmd); en->Name; en++) { + if(*en->Name && !strcmp(s, en->Name)) { + update_val_int(cmd, en->Idx); + return; } - fprintf(fp, "\n"); - break; - case CMDSTRARRAYTYPE: - PrintStrArray(cmd, ValFlag, fp); - break; - default: - fprintf(stderr, "%s: %s %d %s \"%s\"\n", - "PrintParam", - "Unknown Type", - cmd->Type, - "for parameter", + } + EnumError(cmd, s); +} + +static void SetSubrange(Cmd_T *cmd, const char *s) +{ + int n; + const int *subrange = get_p_range(cmd); + + if(sscanf(s, "%d", &n)!=1) { + fprintf(stderr, + "Integer value required for parameter \"%s\"\n", cmd->Name); exit(1); } - return 0; + if(n < subrange[0] || n > subrange[1]) { + SubrangeError(cmd, n); + } + update_val_int(cmd, n); } -static char *GetLine(fp, n, Line) -FILE *fp; -int n; -char *Line; +static void SetGte(Cmd_T *cmd, const char *s) { - int j, - l, - offs=0; - - for(;;) { - if(!fgets(Line+offs, n-offs, fp)) { - return NULL; - } - if(Line[offs]=='#') continue; - l = strlen(Line+offs)-1; - Line[offs+l] = 0; - for(j=offs; Line[j] && isspace(Line[j]); j++, l--) - ; - if(l<1) continue; - if(j > offs) { - char *s = Line+offs, - *q = Line+j; + int n; - while((*s++=*q++)) - ; - } - if(Line[offs+l-1]=='\\') { - offs += l; - Line[offs-1] = ' '; - } else { - break; - } + if(sscanf(s, "%d", &n)!=1) { + fprintf(stderr, + "Integer value required for parameter \"%s\"\n", + cmd->Name); + exit(1); } - return Line; + if(n<get_p_int(cmd)) { + GteError(cmd, n); + } + update_val_int(cmd, n); } -static int Scan(ProgName, cmds, Line) -char *ProgName, - *Line; -Cmd_T *cmds; +static char **str2array(const char *s, const char *sep) { - char *q, - *p; - int i, - hl, - HasToMatch = FALSE, - c0, - c; + const char *p; + char **a; + int n = 0, + l; - p = Line+strspn(Line, SepString); - if(!(hl=strcspn(p, SepString))) { - return 0; + if(!sep) sep = SepString; + p = s += strspn(s, sep); + while(*p) { + p += strcspn(p, sep); + p += strspn(p, sep); + ++n; } - if((q=strchr(p, '/')) && q-p<hl) { - *q = 0; - if(strcmp(p, ProgName)) { - *q = '/'; - return 0; - } - *q = '/'; - HasToMatch=TRUE; - p = q+1; + a = calloc(n+1, sizeof(char *)); + p = s; + n = 0; + while(*p) { + l = strcspn(p, sep); + a[n] = malloc(l+1); + memcpy(a[n], p, l); + a[n][l] = 0; + ++n; + p += l; + p += strspn(p, sep); } - if(!(hl = strcspn(p, SepString))) { - return 0; + return a; +} + +static void SetStrArray(Cmd_T *cmd, const char *s) +{ + *(char***)cmd->Val = str2array(s, get_p_char(cmd)); +} + +static void SetLte(Cmd_T *cmd, const char *s) +{ + int n; + + if(sscanf(s, "%d", &n)!=1) { + fprintf(stderr, + "Integer value required for parameter \"%s\"\n", + cmd->Name); + exit(1); } - c0 = p[hl]; - p[hl] = 0; - for(i=0, c=1; cmds[i].Name&&(c=strcmp(cmds[i].Name, p))<0; i++) - ; - p[hl] = c0; - if(!c) return SetParam(cmds+i, p+hl+strspn(p+hl, SepString)); - return HasToMatch && c; + if(n > get_p_int(cmd)) { + LteError(cmd, n); + } + update_val_int(cmd, n); } -static int SetParam(cmd, s) -Cmd_T *cmd; -char *s; +static void SetParam(Cmd_T *cmd, const char *s) { if(!*s && cmd->Type != CMDSTRINGTYPE) { fprintf(stderr, "WARNING: No value specified for parameter \"%s\"\n", cmd->Name); - return 0; + return; } switch(cmd->Type) { case CMDDOUBLETYPE: - if(sscanf(s, "%lf", (double*)cmd->Val)!=1) { + if(sscanf(s, "%lf", get_val_double_ptr(cmd))!=1) { fprintf(stderr, "Float value required for parameter \"%s\"\n", cmd->Name); @@ -406,7 +380,7 @@ char *s; SetEnum(cmd, s); break; case CMDINTTYPE: - if(sscanf(s, "%d", (int*)cmd->Val)!=1) { + if(sscanf(s, "%d", get_val_int_ptr(cmd))!=1) { fprintf(stderr, "Integer value required for parameter \"%s\"\n", cmd->Name); @@ -414,9 +388,10 @@ char *s; } break; case CMDSTRINGTYPE: - *(char **)cmd->Val = (strcmp(s, "<NULL>") && strcmp(s, "NULL")) + update_val_char(cmd, + (strcmp(s, "<NULL>") && strcmp(s, "NULL")) ? strdup(s) - : 0; + : 0); break; case CMDSTRARRAYTYPE: SetStrArray(cmd, s); @@ -440,163 +415,67 @@ char *s; exit(1); } cmd->ArgStr = strdup(s); - return 0; } -static int SetEnum(cmd, s) -Cmd_T *cmd; -char *s; +static int Scan(const char *ProgName, Cmd_T *cmds, char *Line) { - Enum_T *en; + char *q, + *p; + int i, + hl, + HasToMatch = FALSE, + c0, + c; - for(en=(Enum_T *)cmd->p; en->Name; en++) { - if(*en->Name && !strcmp(s, en->Name)) { - *(int *) cmd->Val = en->Idx; + p = Line+strspn(Line, SepString); + hl = strcspn(p, SepString); + if(!hl) { + return 0; + } + q = strchr(p, '/'); + if(q && q-p<hl) { + *q = 0; + if(strcmp(p, ProgName)) { + *q = '/'; return 0; } + *q = '/'; + HasToMatch=TRUE; + p = q+1; } - return EnumError(cmd, s); -} - -static int SetSubrange(cmd, s) -Cmd_T *cmd; -char *s; -{ - int n; - - if(sscanf(s, "%d", &n)!=1) { - fprintf(stderr, - "Integer value required for parameter \"%s\"\n", - cmd->Name); - exit(1); - } - if(n < *(int *)cmd->p || n > *((int *)cmd->p+1)) { - return SubrangeError(cmd, n); - } - *(int *)cmd->Val = n; - return 0; -} - -static int SetGte(cmd, s) -Cmd_T *cmd; -char *s; -{ - int n; - - if(sscanf(s, "%d", &n)!=1) { - fprintf(stderr, - "Integer value required for parameter \"%s\"\n", - cmd->Name); - exit(1); - } - if(n<*(int *)cmd->p) { - return GteError(cmd, n); + hl = strcspn(p, SepString); + if(!hl) { + return 0; } - *(int *)cmd->Val = n; - return 0; -} - -static int SetStrArray(cmd, s) -Cmd_T *cmd; -char *s; -{ - *(char***)cmd->Val = str2array(s, (char*)cmd->p); - return 0; -} + c0 = p[hl]; + p[hl] = 0; + for(i=0, c=1; cmds[i].Name&&(c=strcmp(cmds[i].Name, p))<0; i++) + ; + p[hl] = c0; -static int SetLte(cmd, s) -Cmd_T *cmd; -char *s; -{ - int n; + if (c) + return HasToMatch && c; - if(sscanf(s, "%d", &n)!=1) { - fprintf(stderr, - "Integer value required for parameter \"%s\"\n", - cmd->Name); - exit(1); - } - if(n > *(int *)cmd->p) { - return LteError(cmd, n); - } - *(int *)cmd->Val = n; + SetParam(cmds+i, p+hl+strspn(p+hl, SepString)); return 0; } -static int EnumError(cmd, s) -Cmd_T *cmd; -char *s; -{ - Enum_T *en; - - fprintf(stderr, - "Invalid value \"%s\" for parameter \"%s\"\n", s, cmd->Name); - fprintf(stderr, "Valid values are:\n"); - for(en=(Enum_T *)cmd->p; en->Name; en++) { - if(*en->Name) { - fprintf(stderr, " %s\n", en->Name); - } - } - fprintf(stderr, "\n"); - exit(1); -} - -static int GteError(cmd, n) -Cmd_T *cmd; -int n; -{ - fprintf(stderr, - "Value %d out of range for parameter \"%s\"\n", n, cmd->Name); - fprintf(stderr, "Valid values must be greater than or equal to %d\n", - *(int *)cmd->p); - exit(1); -} - -static int LteError(cmd, n) -Cmd_T *cmd; -int n; -{ - fprintf(stderr, - "Value %d out of range for parameter \"%s\"\n", n, cmd->Name); - fprintf(stderr, "Valid values must be less than or equal to %d\n", - *(int *)cmd->p); - exit(1); -} - -static int SubrangeError(cmd, n) -Cmd_T *cmd; -int n; +static void PrintEnum(const Cmd_T *cmd, int ValFlag, FILE *fp) { - fprintf(stderr, - "Value %d out of range for parameter \"%s\"\n", n, cmd->Name); - fprintf(stderr, "Valid values range from %d to %d\n", - *(int *)cmd->p, *((int *)cmd->p+1)); - exit(1); -} - -static int PrintEnum(cmd, ValFlag, fp) -Cmd_T *cmd; -int ValFlag; -FILE *fp; -{ - Enum_T *en; + const Enum_T *en; fprintf(fp, "%s", cmd->Name); if(ValFlag) { - for(en=(Enum_T *)cmd->p; en->Name; en++) { - if(*en->Name && en->Idx==*(int *)cmd->Val) { + for(en=get_p_enums(cmd); en->Name; en++) { + if(*en->Name && en->Idx==get_val_int(cmd)) { fprintf(fp, ": %s", en->Name); } } } fprintf(fp, "\n"); - return 0; } -static int PrintStrArray(cmd, ValFlag, fp) -Cmd_T *cmd; -int ValFlag; -FILE *fp; +static void PrintStrArray(const Cmd_T *cmd, int ValFlag, FILE *fp) { char *indent, **s = *(char***)cmd->Val; @@ -614,36 +493,152 @@ FILE *fp; } free(indent); fprintf(fp, "\n"); - return 0; } -static char **str2array(s, sep) -char *s, - *sep; +static void PrintParam(const Cmd_T *cmd, int ValFlag, FILE *fp) { - char *p, - **a; - int n = 0, - l; + fprintf(fp, "%4s", ""); + switch(cmd->Type) { + case CMDDOUBLETYPE: + fprintf(fp, "%s", cmd->Name); + if(ValFlag) fprintf(fp, ": %22.15e", get_val_double(cmd)); + fprintf(fp, "\n"); + break; + case CMDENUMTYPE: + PrintEnum(cmd, ValFlag, fp); + break; + case CMDINTTYPE: + case CMDSUBRANGETYPE: + case CMDGTETYPE: + case CMDLTETYPE: + fprintf(fp, "%s", cmd->Name); + if(ValFlag) fprintf(fp, ": %d", get_val_int(cmd)); + fprintf(fp, "\n"); + break; + case CMDSTRINGTYPE: + fprintf(fp, "%s", cmd->Name); + if(ValFlag) { + const char *value = get_val_char(cmd); + if(value) { + fprintf(fp, ": \"%s\"", value); + } else { + fprintf(fp, ": %s", "NULL"); + } + } + fprintf(fp, "\n"); + break; + case CMDSTRARRAYTYPE: + PrintStrArray(cmd, ValFlag, fp); + break; + default: + fprintf(stderr, "%s: %s %d %s \"%s\"\n", + "PrintParam", + "Unknown Type", + cmd->Type, + "for parameter", + cmd->Name); + exit(1); + } +} - if(!sep) sep = SepString; - p = s += strspn(s, sep); - while(*p) { - p += strcspn(p, sep); - p += strspn(p, sep); - ++n; +static void PrintParams(int ValFlag, FILE *fp) +{ + int i; + + fflush(fp); + if(ValFlag) { + fprintf(fp, "Parameters Values:\n"); + } else { + fprintf(fp, "Parameters:\n"); } - a = calloc(n+1, sizeof(char *)); - p = s; - n = 0; - while(*p) { - l = strcspn(p, sep); - a[n] = malloc(l+1); - memcpy(a[n], p, l); - a[n][l] = 0; - ++n; - p += l; - p += strspn(p, sep); + for(i=0; cmds[i].Name; i++) PrintParam(cmds+i, ValFlag, fp); + fprintf(fp, "\n"); + fflush(fp); +} + +static void CmdError(const char *opt) +{ + fprintf(stderr, "Invalid option \"%s\"\n", opt); + fprintf(stderr, "This program expectes the following parameters:\n"); + PrintParams(FALSE, stderr); + exit(0); +} + +int GetParams(int *n, char ***a, const char *CmdFileName) +{ + char *Line, + *ProgName; + int argc = *n; + char **argv = *a, + *s; + FILE *fp; + int IsPipe; + +#ifdef MSDOS +#define PATHSEP '\\' + char *dot = NULL; +#else +#define PATHSEP '/' +#endif + + if(!(Line=malloc(LINSIZ))) { + fprintf(stderr, "GetParams(): Unable to alloc %d bytes\n", + LINSIZ); + exit(1); } - return a; + if((ProgName=strrchr(*argv, PATHSEP))) { + ++ProgName; + } else { + ProgName = *argv; + } +#ifdef MSDOS + if(dot=strchr(ProgName, '.')) *dot = 0; +#endif + --argc; + ++argv; + for(;;) { + if(argc && argv[0][0]=='-' && argv[0][1]=='=') { + CmdFileName = argv[0]+2; + ++argv; + --argc; + } + if(!CmdFileName) { + break; + } + IsPipe = !strncmp(CmdFileName, "@@", 2); + fp = IsPipe + ? popen(CmdFileName+2, "r") + : strcmp(CmdFileName, "-") + ? fopen(CmdFileName, "r") + : stdin; + if(!fp) { + fprintf(stderr, "Unable to open command file %s\n", + CmdFileName); + exit(1); + } + while(GetLine(fp, LINSIZ, Line) && strcmp(Line, "\\End")) { + if(Scan(ProgName, cmds, Line)) { + CmdError(Line); + } + } + if(fp!=stdin) { + if(IsPipe) pclose(fp); else fclose(fp); + } + CmdFileName = NULL; + } + while(argc && **argv=='-' && (s=strchr(*argv, '='))) { + *s = ' '; + sprintf(Line, "%s/%s", ProgName, *argv+1); + *s = '='; + if(Scan(ProgName, cmds, Line)) CmdError(*argv); + --argc; + ++argv; + } + *n = argc; + *a = argv; +#ifdef MSDOS + if(dot) *dot = '.'; +#endif + free(Line); + return 0; } diff --git a/symal/cmd.h b/symal/cmd.h index 01a00abc0..71b2cd94a 100644 --- a/symal/cmd.h +++ b/symal/cmd.h @@ -5,47 +5,31 @@ #define CMD_H -#define CMDDOUBLETYPE 1 -#define CMDENUMTYPE 2 -#define CMDINTTYPE 3 -#define CMDSTRINGTYPE 4 -#define CMDSUBRANGETYPE 5 -#define CMDGTETYPE 6 -#define CMDLTETYPE 7 -#define CMDSTRARRAYTYPE 8 -#define CMDBOOLTYPE 9 +enum CommandType { + CMDDOUBLETYPE = 1, + CMDENUMTYPE, + CMDINTTYPE, + CMDSTRINGTYPE, + CMDSUBRANGETYPE, + CMDGTETYPE, + CMDLTETYPE, + CMDSTRARRAYTYPE, + CMDBOOLTYPE +}; typedef struct { const char *Name; int Idx; } Enum_T; -typedef struct { - int Type; - char *Name, - *ArgStr; - void *Val, - *p; -} Cmd_T; - #ifdef __cplusplus extern "C" { #endif -#if defined(__STDC__) - int DeclareParams(char *, ...); -#else - int DeclareParams(); -#endif - - int GetParams(int *n, char ***a,char *CmdFileName), - SPrintParams(), - PrintParams(); + int DeclareParams(const char *, ...); + int GetParams(int *n, char ***a, const char *CmdFileName); #ifdef __cplusplus } #endif #endif - - - diff --git a/symal/symal.cpp b/symal/symal.cpp index 927676393..e1108d236 100644 --- a/symal/symal.cpp +++ b/symal/symal.cpp @@ -5,6 +5,7 @@ #include <iostream> #include <fstream> #include <sstream> +#include <stdexcept> #include <string> #include <list> #include <vector> @@ -15,21 +16,23 @@ using namespace std; -#define MAX_WORD 10000 // maximum lengthsource/target strings -#define MAX_M 400 // maximum length of source strings -#define MAX_N 400 // maximum length of target strings +const int MAX_WORD = 10000; // maximum lengthsource/target strings +const int MAX_M = 400; // maximum length of source strings +const int MAX_N = 400; // maximum length of target strings -#define UNION 1 -#define INTERSECT 2 -#define GROW 3 -#define SRCTOTGT 4 -#define TGTTOSRC 5 -#define BOOL_YES 1 -#define BOOL_NO 0 +enum Alignment { + UNION = 1, + INTERSECT, + GROW, + SRCTOTGT, + TGTTOSRC, +}; -#define END_ENUM { (char*)0, 0 } +const Enum_T END_ENUM = {'\0', 0}; -static Enum_T AlignEnum [] = { +namespace +{ +Enum_T AlignEnum [] = { { "union", UNION }, { "u", UNION }, { "intersect", INTERSECT}, @@ -43,18 +46,16 @@ static Enum_T AlignEnum [] = { END_ENUM }; -static Enum_T BoolEnum [] = { - { "true", BOOL_YES }, - { "yes", BOOL_YES }, - { "y", BOOL_YES }, - { "false", BOOL_NO }, - { "no", BOOL_NO }, - { "n", BOOL_NO }, +Enum_T BoolEnum [] = { + { "true", true }, + { "yes", true }, + { "y", true }, + { "false", false }, + { "no", false }, + { "n", false }, END_ENUM }; - - // global variables and constants int* fa; //counters of covered foreign positions @@ -117,7 +118,7 @@ int getals(istream& inp,int& m, int *a,int& n, int *b) } else return 0; -}; +} //compute union alignment @@ -226,7 +227,7 @@ int printsrctotgt(ostream& out,int m,int *a,int n,int* b) //to represent the grow alignment as the unionalignment of a //directed and inverted alignment -int printgrow(ostream& out,int m,int *a,int n,int* b, bool diagonal=false,bool final=false,bool bothuncovered=false) +int printgrow(ostream& out,int m,int *a,int n,int* b, bool diagonal=false,bool isfinal=false,bool bothuncovered=false) { ostringstream sout; @@ -322,7 +323,7 @@ int printgrow(ostream& out,int m,int *a,int n,int* b, bool diagonal=false,bool f } } - if (final) { + if (isfinal) { for (k=unionalignment.begin(); k!=unionalignment.end(); k++) if (A[k->first][k->second]==1) { point.first=k->first; @@ -383,6 +384,7 @@ int printgrow(ostream& out,int m,int *a,int n,int* b, bool diagonal=false,bool f return 1; } +} // namespace //Main file here @@ -395,7 +397,7 @@ int main(int argc, char** argv) char* input= NULL; char* output= NULL; int diagonal=false; - int final=false; + int isfinal=false; int bothuncovered=false; @@ -403,8 +405,8 @@ int main(int argc, char** argv) "alignment", CMDENUMTYPE, &alignment, AlignEnum, "d", CMDENUMTYPE, &diagonal, BoolEnum, "diagonal", CMDENUMTYPE, &diagonal, BoolEnum, - "f", CMDENUMTYPE, &final, BoolEnum, - "final", CMDENUMTYPE, &final, BoolEnum, + "f", CMDENUMTYPE, &isfinal, BoolEnum, + "final", CMDENUMTYPE, &isfinal, BoolEnum, "b", CMDENUMTYPE, &bothuncovered, BoolEnum, "both", CMDENUMTYPE, &bothuncovered, BoolEnum, "i", CMDSTRINGTYPE, &input, @@ -412,9 +414,9 @@ int main(int argc, char** argv) "v", CMDENUMTYPE, &verbose, BoolEnum, "verbose", CMDENUMTYPE, &verbose, BoolEnum, - (char*)NULL); + NULL); - GetParams(&argc, &argv, (char*)NULL); + GetParams(&argc, &argv, NULL); if (alignment==0) { cerr << "usage: symal [-i=<inputfile>] [-o=<outputfile>] -a=[u|i|g] -d=[yes|no] -b=[yes|no] -f=[yes|no] \n" @@ -426,92 +428,91 @@ int main(int argc, char** argv) istream *inp = &std::cin; ostream *out = &std::cout; - if (input) { - fstream *fin = new fstream(input,ios::in); - if (!fin->is_open()) { - cerr << "cannot open " << input << "\n"; - exit(1); + try { + if (input) { + fstream *fin = new fstream(input,ios::in); + if (!fin->is_open()) throw runtime_error("cannot open " + string(input)); + inp = fin; } - inp = fin; - } - if (output) { - fstream *fout = new fstream(output,ios::out); - if (!fout->is_open()) { - cerr << "cannot open " << output << "\n"; - exit(1); + if (output) { + fstream *fout = new fstream(output,ios::out); + if (!fout->is_open()) throw runtime_error("cannot open " + string(output)); + out = fout; } - out = fout; - } - int a[MAX_M],b[MAX_N],m,n; - fa=new int[MAX_M+1]; - ea=new int[MAX_N+1]; + int a[MAX_M],b[MAX_N],m,n; + fa=new int[MAX_M+1]; + ea=new int[MAX_N+1]; - int sents = 0; - A=new int *[MAX_N+1]; - for (int i=1; i<=MAX_N; i++) A[i]=new int[MAX_M+1]; + int sents = 0; + A=new int *[MAX_N+1]; + for (int i=1; i<=MAX_N; i++) A[i]=new int[MAX_M+1]; - switch (alignment) { - case UNION: - cerr << "symal: computing union alignment\n"; - while(getals(*inp,m,a,n,b)) { - prunionalignment(*out,m,a,n,b); - sents++; - } - cerr << "Sents: " << sents << endl; - break; - case INTERSECT: - cerr << "symal: computing intersect alignment\n"; - while(getals(*inp,m,a,n,b)) { - printersect(*out,m,a,n,b); - sents++; + switch (alignment) { + case UNION: + cerr << "symal: computing union alignment\n"; + while(getals(*inp,m,a,n,b)) { + prunionalignment(*out,m,a,n,b); + sents++; + } + cerr << "Sents: " << sents << endl; + break; + case INTERSECT: + cerr << "symal: computing intersect alignment\n"; + while(getals(*inp,m,a,n,b)) { + printersect(*out,m,a,n,b); + sents++; + } + cerr << "Sents: " << sents << endl; + break; + case GROW: + cerr << "symal: computing grow alignment: diagonal (" + << diagonal << ") final ("<< isfinal << ")" + << "both-uncovered (" << bothuncovered <<")\n"; + + while(getals(*inp,m,a,n,b)) + printgrow(*out,m,a,n,b,diagonal,isfinal,bothuncovered); + + break; + case TGTTOSRC: + cerr << "symal: computing target-to-source alignment\n"; + + while(getals(*inp,m,a,n,b)) { + printtgttosrc(*out,m,a,n,b); + sents++; + } + cerr << "Sents: " << sents << endl; + break; + case SRCTOTGT: + cerr << "symal: computing source-to-target alignment\n"; + + while(getals(*inp,m,a,n,b)) { + printsrctotgt(*out,m,a,n,b); + sents++; + } + cerr << "Sents: " << sents << endl; + break; + default: + throw runtime_error("Unknown alignment"); } - cerr << "Sents: " << sents << endl; - break; - case GROW: - cerr << "symal: computing grow alignment: diagonal (" - << diagonal << ") final ("<< final << ")" - << "both-uncovered (" << bothuncovered <<")\n"; - - while(getals(*inp,m,a,n,b)) - printgrow(*out,m,a,n,b,diagonal,final,bothuncovered); - - break; - case TGTTOSRC: - cerr << "symal: computing target-to-source alignment\n"; - - while(getals(*inp,m,a,n,b)) { - printtgttosrc(*out,m,a,n,b); - sents++; + + delete [] fa; + delete [] ea; + for (int i=1; i<=MAX_N; i++) delete [] A[i]; + delete [] A; + + if (inp != &std::cin) { + delete inp; } - cerr << "Sents: " << sents << endl; - break; - case SRCTOTGT: - cerr << "symal: computing source-to-target alignment\n"; - - while(getals(*inp,m,a,n,b)) { - printsrctotgt(*out,m,a,n,b); - sents++; + if (out != &std::cout) { + delete inp; } - cerr << "Sents: " << sents << endl; - break; - default: + } catch (const std::exception &e) { + cerr << e.what() << std::endl; exit(1); } - delete [] fa; - delete [] ea; - for (int i=1; i<=MAX_N; i++) delete [] A[i]; - delete [] A; - - if (inp != &std::cin) { - delete inp; - } - if (out != &std::cout) { - delete inp; - } - exit(0); } |