⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pinyin.c

📁 ARM9-2410教学实验系统下Linux下minigui程序
💻 C
📖 第 1 页 / 共 2 页
字号:
      inmd->sysph[i] = sysph = (SysPhrase*)p;#if MGUI_BYTEORDER == MGUI_BIG_ENDIAN      sysph->count = ArchSwap16 (sysph->count);#endif      p = (char*)sysph->phrase;      for(j = 0; j < sysph->count; j++) {         kph = (Phrase*)p;         p += SizeOfPhrase (kph->len,kph->count);  // skip the string       }   }   fclose (stream);   return TRUE;}static void AdjustPhraseFreq (InputModule *inmd){  UsrPhrase *uph;  SysPhrase *sysph;  Phrase *sph;  int i,j,k,index;  char *p;  for(i = 1; i < MAX_PY_NUM; i++)  {     // user phrases     for(uph = inmd->usrph[i]; uph != NULL; uph = uph->next)     {         for(k =0; k < uph->count; k++)         {            index = uph->len+1 + (2*uph->len+1)*k + 2*uph->len;            if (uph->key[index] > 25)                uph->key[index] = 25 + (uph->key[index]-25)/10;         }     }        // system phrases     sysph = inmd->sysph[i];     p = (char*)sysph->phrase;   // count = total pinyin number     for(j = 0; j < sysph->count; j++)     {        sph = (Phrase *)p;        for(k = 0; k < sph->count; k++)        {            index = sph->len+1 + (2*sph->len+1)*k + 2*sph->len;            if (sph->key[index] > 25)                 sph->key[index] = 25 + (sph->key[index] - 25)/10;        }           p += SizeOfPhrase(sph->len,sph->count);     }  }  return;}static BOOL SaveSysPhrase (InputModule *inmd, char *pathname){    FILE *out;    char *p;    if ( (out = fopen( pathname, "wb")) == NULL) {        fprintf (stderr, "IME (Pinyin): %s cant open for saving.\n", pathname);        return FALSE;    }    p = (char*)(inmd->sysph[1]);#if MGUI_BYTEORDER == MGUI_LIL_ENDIAN    fwrite (p, inmd->sys_size, 1, out);    fwrite (&(inmd->sys_size), sizeof(int), 1, out);#else  {    int i, j;    for (i = 1; i < MAX_PY_NUM; i++) {        SysPhrase *sysph = (SysPhrase*)p;        int phrase_size;        MGUI_WriteLE16FP (out, sysph->count);        p = (char*)sysph->phrase;        for (j = 0; j < sysph->count; j++) {            Phrase* kph = (Phrase*)p;            phrase_size = SizeOfPhrase (kph->len, kph->count);            fwrite (p, phrase_size, 1, out);            p += phrase_size;        }    }    MGUI_WriteLE32FP (out, inmd->sys_size);    p = (char*)(inmd->sysph[1]);  }#endif    free (p);    fclose (out);    return TRUE;}static BOOL SaveUsrPhrase (InputModule *inmd, char *pathname){    int i,fsize = 0, tmpcount;    FILE *out;    UsrPhrase *p0,*q0;    u_short total;    u_char len,count;    if ((out = fopen (pathname,"wb")) == NULL) {        fprintf (stderr, "IME (Pinyin): %s cant open for saving.\n", pathname);        return FALSE;    }    for (i=1; i<MAX_PY_NUM; i++) {      total = 0;      fsize += sizeof(u_short); // for total      p0 =  inmd->usrph[i];      if (p0 == NULL) {        MGUI_WriteLE16FP (out, total);        continue;      }      // first, calculate the different pinyin key phrases      total++;      for (p0 = p0->next; p0 != NULL; p0 = p0->next) {         len = p0->len;         for (q0 = inmd->usrph[i]; q0 != p0; q0 = q0->next)            if (q0->len == len && !memcmp(p0->key, q0->key, len+1)) break;         if (p0 == q0) total++;        }      MGUI_WriteLE16FP (out, total);      for (p0 = inmd->usrph[i]; p0 != NULL; p0 = p0->next) {          if (p0->count == 0) continue;          tmpcount = p0->count;          len = p0->len;          for(q0 = p0->next; q0 != NULL; q0 = q0->next)             if (q0->len == len && !memcmp(q0->key,p0->key,len+1))                 tmpcount += q0->count;          if (tmpcount > 255)              tmpcount = 255;          else count = (u_char)tmpcount;           fsize += SizeOfPhrase(len,count);          // u_char count, count must less than 256 phrases                 fwrite(&len,sizeof(len),1,out);          fwrite(&count,sizeof(count),1,out);          fwrite(p0->key,len+1,1,out);          for(q0 = p0; q0 != NULL; q0 = q0->next)             if (q0->len == len && !memcmp(q0->key,p0->key,len+1))             {                 fwrite(q0->key+len+1,q0->count*(2*len+1),1,out);                 q0->count = 0;              }       }    }    MGUI_WriteLE32FP (out, fsize);    fclose (out);    for (i = 1; i < MAX_PY_NUM; i++)       free (inmd->usrph[i]);    return TRUE;}static void SaveUsrPhraseToMem(InputModule *inmd,u_char *str,u_char *key,int len,int freq){  UsrPhrase *kph, *tmp;  short ahead;    if (len<1) return;  ahead = (short)key[1];  ahead |= (key[0] & 0x01) << 8;  /* allocate a new one, not combine, will combine during saving */  if ((kph = (UsrPhrase *)malloc(4+SizeOfPhrase(len,1))) == NULL)      {printf("Not enough memory\n");return;}  kph->len = len;  memcpy(kph->key,key,len+1);  kph->count = 1;   kph->next = NULL;  memcpy(kph->key + len+1,str,len*2);  kph->key[len+1+2*len] = freq;  if (inmd->usrph[ahead] == NULL)       inmd->usrph[ahead] = kph;  else  {      tmp = inmd->usrph[ahead];      while (tmp->next != NULL)         tmp = tmp->next;      tmp->next = kph;  }}static inline u_char *GetPhrase(ChoiceItem *p){   static char temp[2*MAX_PHRASE_LEN+1];   int len = (int)(p->head->len);   if (len > MAX_PHRASE_LEN) {#ifdef _DEBUG       fprintf(stderr, "buffer overrun\n");#endif   }   strncpy(temp,p->head->key + len + 1 + p->index *(2*len+1),2*len);   temp[2*len] = '\0';   return temp;}static int ParsePy(InputModule *inmd, char *pybuf, PYString pinyin[]){  int len, ahead,i, total = 0;  int offset = 0, count, valid;   len = strlen(pybuf);  if (len < 1 || len > MAX_PHRASE_LEN * (MAX_PY_LEN+1) ) return 0;    count = 2;  /* 1 always valid */  while (offset + count <= len)  {      if (pybuf[offset] == '\'')  // delimitor '       {          strcpy(pinyin[total++],"\'");          offset++; count = 2; continue;      }      ahead = pybuf[offset] - 'a';      if (ahead < 0 || ahead > 25) return 0;      // test if this is a valid pinyin prefix      valid = 0;      for(i=0; inmd->pytab[ahead][i].key; i++)      {         if ( !strncmp(pybuf+offset,inmd->pytab[ahead][i].py,count) )         {            valid = 1; break;         }      }      if (valid) count++;     else     {         strncpy(pinyin[total], pybuf+offset, count-1);         pinyin[total++][count-1] = '\0';         offset += count-1;         count = 2;     }  }  // copy the remaining pinyin  if (offset < len)  {    strncpy(pinyin[total], pybuf+offset, count-1);     pinyin[total++][count-1] = '\0';  }  return total;}static void FindMatchPhrase(InputModule *inmd,PYString pinyin[],int lenpy){  int lenkey,keytmp;  int i,j,k;  int ahead,tmplen, count=0;  int pykey[MAX_PHRASE_LEN][MAX_EACH_PY+1];          // MAX_PHRASE_LEN=6, MAX_EACH_PY = 38, a[], b[]  u_char py[MAX_PY_LEN+2];  u_char key[MAX_PHRASE_LEN+1];  u_char keyarr[MAX_PHRASE_LEN][500][MAX_PHRASE_LEN+1];    // temporary array, 500 items  int lenarr[MAX_PHRASE_LEN],result;  char ch;  if (!lenpy)  {    inmd->len = 0;    return;  }  /* first of all, fill the pykey array */  for (i=0; i<lenpy; i++)  {    ch = pinyin[i][0];    if (ch == 'i' || ch == 'u' || ch == 'v' || ch < 'a' || ch > 'z')         continue;  // ignore the i/u/v beginning and non a-z     ahead = pinyin[i][0] - 'a';    lenkey=0;    tmplen=strlen(pinyin[i]);    for(j=0; (keytmp = inmd->pytab[ahead][j].key); j++)    {      if ( tmplen == 1 || !strncmp(pinyin[i],inmd->pytab[ahead][j].py,tmplen))          // prefix match      {        pykey[count][lenkey++] = keytmp;        continue;      }      else if (inmd->FuzzyPinyin && (ch == 'z' || ch == 'c' || ch == 's'))       {          if (pinyin[i][1] != 'h')         {            strncpy(py+1,pinyin[i], MAX_PY_LEN);            py[0] = py[1];             py[1] = 'h';         }         else         {            strncpy(py,pinyin[i]+1, MAX_PY_LEN);            py[0] = ch;         }           if (!strncmp(py,inmd->pytab[ahead][j].py,strlen(py)))            pykey[count][lenkey++] = keytmp;      }    }    pykey[count++][lenkey] = 0;  }  // for i = 1 to lenpy, pykey array filled  if (count > MAX_PHRASE_LEN - 1) {#ifdef _DEBUG      fprintf(stderr, "buffer overrun\n");#endif      return;  }  for(i=0; i<MAX_PHRASE_LEN; i++) lenarr[i]=0;  for(i = 0; i < 8; i++) inmd->seltotal[i] = 0;  /* for the first char */  for(k=0; pykey[0][k]; k++)  {    key[1] = pykey[0][k] & 0xff;    key[0]='\0';    key[0] |= (pykey[0][k] & 0x0100) >> 8;    //  single char phrase    result = QueryPhrase(inmd, key, 1);    if (result > 0)  //save the possible multiple-char phrases         memcpy(keyarr[0][lenarr[0]++],key,2);  }  /* count is the real pinyin number, parse the remaining */  for(i=1; i<count; i++)    for(j=0; j<lenarr[i-1]; j++)  // for all the possible phrase match      for(k=0; pykey[i][k]; k++)      {        memcpy(key,keyarr[i-1][j],i+1);        key[i+1] = pykey[i][k] & 0xff;        key[0] |= (pykey[i][k] & 0x0100) >> (8-i);        result = QueryPhrase(inmd, key, i+1);        if (result > 0)          memcpy(keyarr[i][lenarr[i]++], key,i+2);      }  SortOutput(inmd);}static void SortOutput(InputModule *inmd){  int i,j,k,lenph,totalph = 0;  ChoiceItem phtmp, *ph = inmd->sel;  totalph = 0;  for(i = MAX_PHRASE_LEN-1; i >= 0; i--)  {      lenph = 0;      for(j = 0; j < inmd->seltotal[i]; j++)      {         for(k = 0; k < inmd->tempsel[i][j]->count; k++)         {            inmd->sel[totalph+lenph].head = inmd->tempsel[i][j];            inmd->sel[totalph+lenph].index = k;            lenph++;         }      }     // sort the phrases      for(k=0; k<lenph; k++)     for(j=k; j<lenph; j++)     if (*GetFreq(ph+k) < *GetFreq(ph+j))     {        phtmp = ph[k];        ph[k] = ph[j];        ph[j] = phtmp;     }     totalph += lenph;      ph += lenph;  }  inmd->len = totalph;  // total possible phrase selection}static inline u_char *GetFreq(ChoiceItem *p){   int len = (int)(p->head->len);   return (u_char*)(p->head->key + len + 1 + p->index *(2*len+1)+ 2*len);}static void FillForwardSelection(InputModule *inmd,int startpos){    char *iahzbuf=inmd->iahzbuf;    int i,count;//    int SelAreaWidth = dispInfo.tx_avail - 10 - PINYIN_AREA_WIDTH;    char strtmp[2*MAX_PHRASE_LEN+10];    if (startpos > inmd->len - 1 || startpos < 0)        return ;   // non-forwardable, keep the iahzbuf intact    iahzbuf[0] = '\0';    if (inmd->len < 1) return;  // clear the iahzbuf    count = 0;   // backup the starting position    inmd->startpos = startpos;    inmd->endpos = startpos - 1;    if (inmd->startpos > 0)       snprintf(inmd->iahzbuf,MAX_HZ_BUF,"< ");    else snprintf(inmd->iahzbuf,MAX_HZ_BUF,"  ");    while(inmd->endpos < inmd->len-1 && count < 10)    {       snprintf(strtmp,sizeof(strtmp),"%d%s ",(count+1)%10,             GetPhrase(inmd->sel+inmd->endpos+1));       strcat(iahzbuf,strtmp);       inmd->endpos++;       count++;    }    if (inmd->endpos < inmd->len - 1 && count >= 1)    {      for(i = strlen(iahzbuf); i < SEL_AREA_WIDTH-2; i++)         iahzbuf[i] = ' ';      iahzbuf[MAX_HZ_BUF-2] = '>';      iahzbuf[MAX_HZ_BUF-1] = '\0';    }}static void FillBackwardSelection(InputModule *inmd,int lastpos){    char *iahzbuf=inmd->iahzbuf; //   int SelAreaWidth = dispInfo.tx_avail - 10 - PINYIN_AREA_WIDTH;    int count,ialen;    char strbuf[2*MAX_PHRASE_LEN+10];    if (lastpos < 0 || lastpos > inmd->len-1)        return;   // iahzbuf intact    iahzbuf[0] = '\0';    if (inmd->len < 1) return; // clear iahzbuf    count = 0;    inmd->endpos = lastpos;    ialen = 2;   // leftmost "< " or "  "    inmd->startpos = lastpos+1;    while(inmd->startpos > 0 && count < 10)    {        strncpy(strbuf,GetPhrase(inmd->sel+inmd->startpos-1),sizeof(strbuf));        ialen += strlen(strbuf)+2;        if (ialen+2 > MAX_HZ_BUF) break;        count++;        inmd->startpos--;    }    FillForwardSelection(inmd,inmd->startpos);}static int EffectPyNum(PYString pinyin[],int len){  int i;  char ch;  int count=0;  for(i=0; i<len; i++)  {    ch = pinyin[i][0];    if (ch == 'i' || ch == 'u' || ch == 'v') continue;    if (ch < 'a' || ch >'z') continue;    count++;  }  return count;}static int QueryPhrase(InputModule *inmd, u_char *key, int len){  short ahead;  UsrPhrase *uph;  char *p;  SysPhrase *sysph;  Phrase *sph;  int i;  u_char phkey[MAX_PHRASE_LEN+1];  short mask=0;  int j,count = 0;  if (len<1) return 0;  ahead = (short)key[1];  ahead |= (key[0] & 0x01) << 8;  for(i=0;i<len;i++)    mask += 1<<i;      for( uph = inmd->usrph[ahead]; uph != NULL; uph = uph->next)   {      if (uph->len < len) continue;      if (len > MAX_PHRASE_LEN) {#ifdef _DEBUG          fprintf(stderr, "IME (Pinyin): buffer overrun\n");#endif//          abort();          return -1;      }      memcpy(phkey,uph->key,len+1);      phkey[0] &= mask;      if (!memcmp(phkey,key,len+1))  // match      {          if (uph->len == len )   // exact match          {              inmd->tempsel[len-1][ inmd->seltotal[len-1]++ ] =                    (Phrase*)( ((char*)uph) + 4 );           }          else count++;  // calculate the phrase longer than len      }   }    // search in user phrase lib first, then system phrase libray      sysph = inmd->sysph[ahead];      p = (char*)sysph->phrase;   // count = total pinyin number     for(j = 0; j < sysph->count; j++)     {        sph = (Phrase *)p;        if (sph->len >= len)        {           if (len > MAX_PHRASE_LEN) {#ifdef _DEBUG                fprintf(stderr, "IME (Pinyin): buffer overrun\n");#endif//                abort();                return -1;           }           memcpy(phkey,sph->key,len+1);           phkey[0] &= mask;           if (!memcmp(phkey,key,len+1)) // match           {               if (sph->len == len)                 inmd->tempsel[len-1][ inmd->seltotal[len-1]++ ] = sph;               else count++;           }        }        p += SizeOfPhrase(sph->len,sph->count);  }     return count;} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -