?? exemplar.c
字號:
/**************************************************************************
**************************************************************************
*** ***
*** EXEMPLAR.C ***
*** ***
*** Notice: ***
*** This code is copyright (C) 1995 by David M. Skapura. It may ***
*** be used as is, or modified to suit the requirements of a ***
*** specific application without permission of the author. ***
*** There are no royalty fees for use of this code, as long ***
*** as the original author is credited as part of the final ***
*** work. In exchange for this royalty free use, the user ***
*** agrees that no guarantee or warantee is given or implied. ***
*** ***
*** ***
**************************************************************************
**************************************************************************
The code in this file provides a standard interface to an exemplar
source file. The file is expected to have the following format:
1. Data is text only.
2. Only text contained inside curly braces is interpreted by
the program. Anything outside of curly braces is comment.
3. At the beginning of the file, you may optionally define
translation characters for the exemplars. This is helpful
when visualizing the data patterns is necessary.
4. Every non-blank symbol inside parenthesis is interpreted.
Symbols must be assigned at the beginning of the file, or
they must be convertable to floating point numbers.
5. Although you can intermix symbols and floating point
values for pattern components, be aware that the characters
"0123456789.+-eE" are reserved for floating point number
conversion. If you define one of these characters as a
symbol for another value, do not use floating point number
values as pattern components.
The following example shows the beginning of a typical exemplar file.
{ X = 0.9 } Assigns the character 'X' to be numerically 0.9 (active)
{ . = 0.0 } Assigns the character '.' to be numerically 0.1 (inactive)
{ 1 = 1.0 } Assigns the character '1' to be numerically 1.0 (active)
{ 0 = -1.0 } Assigns the character '0' to be numerically -1.0 (inactive}
What follows is the first exemplar of the training set. The input
pattern is enclosed by parentheses inside the curly braces, as is
the output pattern. Text inside the curly braces, not contained
inside parentheses is ignored. For applications that do not require
an output pattern, use () to denote the target output.
{ (..X..
.X.X.
XX.XX
X...X
XXXXX
X...X
X...X)
ABCDEFGHIJKLMNOPQRSTUVWXYZ
(X.........................)
}
Other exemplars would be defined similarly. The code in this file
interprets data patterns until the end-of-file is encountered. The
number of exemplars defined is indefinate, but each must have the
same number of input and output components as every other exemplar.
*************************************************************************/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define MAX_ALIASES 10
typedef struct
{
int dimX;
int dimY;
float **invecs;
float **outvecs;
} iop;
typedef struct
{
char c;
float v;
} aka;
aka **alias; /* array of pointers to aliases */
int akacnt; /* global holds aka count */
int position (char c, char *s) /* locate first c in s */
{
int i;
for (i=0; i<strlen(s); i++)
if (s[i] == c) return (i);
return (-1);
}
int is_float (char *s)
{
int i=0;
while (s[i] <= ' ') i++;
for (; position (s[i], "0123456789.+-eE") >= 0; i++) {}
if ((i >= strlen(s)) || (position (s[i], " )\0") >= 0))
{
s[i] = '\0';
return (1);
}
return (0);
}
int is_assignment (char *s)
{
return (position ('=', s) > -1);
}
int is_pattern (char *s)
{
int rp, lp;
rp = position ('(', s);
lp = position (')', s);
return ((rp < lp) && (rp > -1));
}
int is_alias (char c)
{
int i;
for (i=0; i<akacnt; i++)
if (alias[i]->c == c) return (1);
return (0);
}
char *substr (char *s, int pos, int bytes)
{
char *result;
result = (char *) calloc (bytes, sizeof(char));
strncpy (result, (char *)s[pos], bytes);
return (result);
}
float getalias (char c)
{
int i;
for (i=0; i<akacnt; i++)
if (alias[i]->c == c) return (alias[i]->v);
return (0);
}
int translate (char *s, float *f)
{
int index=0;
while (s[index] <= ' ') index++;
if (is_alias (s[index]))
{
*f = getalias (s[index]);
return (index+1);
}
if (is_float (s))
{
index = strlen(s);
*f = (float) strtod (s, NULL);
return (index+1);
}
printf ("\nERROR: Could not convert value %s", s);
exit (0);
return (0); /* dummy instruction to avoid compiler warning */
}
int dimension (char *s)
{
float f;
int i, len, valid, count=0;
len = strlen (s) - 1;
for (i=1, valid=0; i<len;)
{
valid = translate ((char *)&s[i], &f);
if (valid)
{
count += 1;
i += valid;
}
else i++;
}
return (count);
}
aka *interpret_assignment (char *s)
{
aka *result;
int i, eqflag=0;
result = (aka *) calloc (1, sizeof(aka));
for (i=1; (i<strlen(s)-1) && !eqflag; i++)
{
if (s[i] == '=') eqflag = i;
if ((s[i] > ' ') && !eqflag) result->c = s[i];
}
if (is_float ((char *)&s[eqflag+1]))
result->v = (float) atof ((char *)&s[eqflag+1]);
else result->v = 0;
return (result);
}
float *interpret_pattern (char *s, int *dim)
{
int i, index;
float *result;
if (*dim < 0) *dim = dimension (s);
else
if (*dim != dimension (s))
{
printf ("\nERROR: Pattern \"%s\" dimension different than previous examples.", s);
exit (0);
}
result = (float *) calloc (*dim, sizeof(float));
for (i=0, index=1; i<*dim; i++)
index += translate ((char *)&s[index], &result[i]);
return (result);
}
int read_statement (FILE *fp, char *result)
{
char c=' ';
int i, sflag=1;
while (!(feof(fp)) && (c != '{')) c = fgetc (fp);
for (i=0; (c != '}') && (!feof(fp));)
{
if (position (c, " \n\t") < 0)
{
result[i++] = toupper(c);
sflag = 1;
}
else
if ((c == ' ') && sflag)
{
result[i++] = c;
sflag = 0;
}
c = fgetc(fp);
}
result[i++] = c;
result[i] = '\0';
return (c == '}');
}
int extract (char *s, char *invec, char *outvec)
{
int i, j, len;
char c=' ';
len = strlen(s);
for (i=0; (c != '(') && (i < len); i++) c = s[i];
for (j=0; (c != ')') && (i < len); i++, j++)
{
invec[j] = c;
c = s[i];
}
invec[j++] = c;
invec[j] = '\0';
for (; (c != '(') && (i < len); i++) c = s[i];
for (j=0; (c != ')') && (i < len); i++, j++)
{
outvec[j] = c;
c = s[i];
}
outvec[j++] = c;
outvec[j] = '\0';
return (i<=len);
}
int interpret (char *s, float **inval, float **outval, int *dX, int *dY)
{
char invec[2048], outvec[2048];
if (is_assignment (s))
{
alias[akacnt++] = interpret_assignment (s);
return (0);
}
if (extract (s, (char *)&invec, (char *)&outvec))
{
*inval = interpret_pattern (invec, dX);
*outval = interpret_pattern (outvec, dY);
return (1);
}
return (0);
}
int init_patterns (FILE *fp)
{
int i=0;
char buffer[4096], *s=(char *)&buffer[0];
alias = (aka **) calloc (MAX_ALIASES, sizeof(aka *));
akacnt = 0;
while (!(feof(fp)) && (read_statement (fp, s)))
if (!(is_assignment (s)) && (position ('(', s) >= 0)) i++;
rewind (fp);
return (i);
}
int load_exemplars (char *filename, iop *pattern)
{
FILE *fp;
char buffer[4096], *s=(char *)&buffer[0];
int iopairs=0, how_many;
float **invecs, **outvecs;
if (!(fp = fopen (filename, "r")))
{
printf ("\nERROR: Could not open exemplar file \"%s\"", filename);
exit (0);
}
how_many = init_patterns (fp);
pattern->invecs = (float **) calloc (how_many, sizeof (float *));
pattern->outvecs = (float **) calloc (how_many, sizeof (float *));
while (!(feof (fp)) && (read_statement (fp, s)))
{
if (interpret (s, &pattern->invecs[iopairs], &pattern->outvecs[iopairs], &pattern->dimX, &pattern->dimY)) iopairs++;
if (akacnt >= MAX_ALIASES)
{
printf ("\nERROR: Too many assignment statements in exemplar file.");
fclose (fp);
exit (0);
}
}
fclose (fp);
return (iopairs);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -