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

📄 zryy1.cpp

📁 字字之间的转移概率算法
💻 CPP
字号:
// zryy1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
int main(int argc, char* argv[])
{

int hz[0x10000];
int i;
for (i=0; i<0x10000; i++) 
hz[i]=0;

unsigned short buffer;

FILE * fp;
fp=fopen("2000.txt","rb");

fseek(fp,0,SEEK_END);    //将指针置在离文件尾长度为0处
int len=ftell(fp);           //文件长度
printf("%c",len);
rewind(fp);
while (!feof(fp) )
{   buffer=fgetwc(fp);
    hz[buffer]++;
	//printf("%d ",buffer);
}
//rewind(fp);

for (i=1; i<0x10000; i++)
 hz[i]=hz[i-1]+hz[i]; //将hz[i]的内容改为hz[0]到hz[i]的总和

unsigned short *succ=new unsigned short [len/2];
int total=len/2;

 for (i=0; i<total; i++) 
    succ[i]=0;

rewind(fp);

unsigned short k;
unsigned short j;
  k=fgetwc(fp);

for (i=0; i<total; i++) 
{
 j=k; 
 k=fgetwc(fp);
 succ[hz[j]-1]=k; 
 hz[j]--;
}

fclose(fp);

//打开write文件FBigram;  //结构数组文件,写入转移概率的右字和转移概率的对数相反数
//打开write文件FHzType;
//*数组文件,写入每个左字在结构数组文件FBigram中对应的右字区的首地址,它是Fbigram的索引。*/
short  LogLeftTotal;              //存放一个左字的右字总次数对数值的百倍取整
int itotal;                      //存放汉字i的右字总数
unsigned short HzType=0;  
int p,q;
                      /*计数器,记录每个左字对应的右字结构数组文件区的首地址,准备存入FhzType;*/
int HzRight[0x10000]; //用于右字字频统计的全汉字数组
//int m,n;
char str[16];
FILE * fp1;
fp1=fopen("FhzType.txt","wb");
FILE * fp2;
fp2=fopen("FBigram.txt","wb");
               
for(i=0; i<65535; i++)    
                                        //大循环,统计右字频次并把右字字码和转移概率对数相反数写入文件
{
fprintf(fp1,"%d  ",HzType); //写出汉字i的右字结构数组文件区的首地址
       
itotal=hz[i+1]-hz[i];                   //右字字例出现的总次数
if (!itotal) continue;                  //无右字
LogLeftTotal=short(log10(itotal)*100);         //汉字i的右字总数的对数值的百倍取整
 for( int m=0; m<0x10000; m++) 
	 HzRight[m]=0;    //全汉字数组初始化
 for(int n=hz[i]; n<hz[i+1]; n++) 
	 HzRight[succ[n]]++;  //以i为左字的右字频次统计
for(p=0; p<65535; p++)                       //往FBigram中压缩存储汉字i的右字字码和频次
 {
  if (HzRight[p]==0) continue; //该字不是i的右字

  fprintf(fp2,"%d ",p);  //写出右字字码
  //q=HzRight[p];
  q=LogLeftTotal-short(log10(HzRight[p])*100); //转移概率对数百倍的相反数(取整)
  fprintf(fp2,"%d   ",q);                     //写出转移概率对数百倍的相反数(取整)
  HzType++; 

  if(i==25105&&p==20204)
  printf("我们  %d",q);
  
  if(i==36364&&p==36487)
  printf("踌躇  %d",q);

}                                 //结构数组的索引指针增值
}

//关闭write文件FBigram;
//关闭write文件FHztype;
fclose(fp1);
fclose(fp2);

  return 0;

}

⌨️ 快捷键说明

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