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

📄 pageinit.cpp

📁 自己用Markov模型做的一个整句物笔输入法的原型
💻 CPP
📖 第 1 页 / 共 3 页
字号:

		//write word table
		if (strw != last_strw)
		{
			if (!itemwc.c2style)
				posw = listw.AddTail(WLItem(strw, fpp));
			else
				posw = listw.AddTail(WLItem(strw, fpp2));

			idw = GetWordId(strw);
			if (widx[idw] == 0)
				widx[idw] = posw; 

			last_strw = strw;
			wordnum ++;
			if (strw.GetLength() == 2)
				chcharnum ++;
		}

		cMessWnd->m_prog.StepIt();
	}

	//generate word-word table
	posw = listw.GetHeadPosition();
	CString strw1, strw2;
	for (; posw; listw.GetNext(posw))
	{
		strw = listw.GetAt(posw).word;

		for (;strw.GetLength() > 2; )
		{
			strw1 = strw.Left(2);
			strw = strw.Right(strw.GetLength()-2);
			strw2 = strw.Left(2);

			if (WWLocate(strw1, strw2, &listww, &wwidx, posww))
			{
				itemww = listww.GetAt(posww);
				itemww.tp ++;
				listww.SetAt(posww, itemww);
			}
			else
			{
				if (posww)		//strw1 exist
					listww.InsertAfter(posww, WWLItem(strw1, strw2, 1));
				else
				{
					idw = GetWordId(strw1);
					posww = listww.AddTail(WWLItem(strw1, strw2, 1));
					wwidx.SetAt(idw, posww);
				}
			}
		}

		cMessWnd->m_prog.StepIt();
	}

	//and calculate chinses character transition probabilities from word table
	cMessWnd->SetMessage("计算转移概率");
	cMessWnd->m_prog.SetRange32(0, listww.GetCount()*2);
	cMessWnd->m_prog.SetStep(1);
	cMessWnd->m_prog.SetPos(0);

	posww = listww.GetHeadPosition();
	last_strw = "";
	sump = 0.0f;
	state = 1;
	for (; posww; )
	{
		itemww = listww.GetAt(posww);
		strw = itemww.word;
		strw2 = itemww.wordc;

		if (strw != last_strw)
		{
			if (state == 0)
			{
				posww = posww0;		//if state == 0, return to the first item of this word and calc post prob.
				state = 1;

				itemww = listww.GetAt(posww);
				strw = itemww.word;
				strw2 = itemww.wordc;
				listw.SetAt(posw, itemw);
			}
			else
			{
				posww0 = posww;
				sump = 0.0f;
				state = 0;

				idw = GetWordId(strw);
				posw = 0;
				widx.Lookup(idw, posw);
				itemw = listw.GetAt(posw);
			}
		}

		if (state == 0)
		{
			sump += itemww.tp;
			itemw.pp++;
		}
		else
		{
			itemww.tp /= sump;
			itemww.tp *= TRANS_SUMLMT;
			listww.SetAt(posww, itemww);
		}
		last_strw = strw;
		listww.GetNext(posww);
		if (posww == 0 && state == 0)
		{
			posww = posww0;
			last_strw = "";
		}

		cMessWnd->m_prog.StepIt();
	}


	//append special code
	//add space key 
	CString csSpcCode = CString("zaaa");
	CString csSpcWord = CString(32);
	nw = 1;
	fcp = 1.0f;
	listc.AddTail(CLItem(csSpcCode,nw, nw));
	listcw.AddTail(CWLItem(csSpcCode, csSpcWord, 0.0f, 0.0f, FALSE));
	listwc.AddTail(WCLItem(csSpcWord, csSpcCode, fcp, fcp, FALSE));
	listw.AddTail(WLItem(csSpcWord, fpp));

	NextSpcCode(csSpcCode);

	csSpcWord = CString(" ");		//chinses space
	nw = 1;
	fcp = 1.0f;
	listc.AddTail(CLItem(csSpcCode, nw, nw));
	listcw.AddTail(CWLItem(csSpcCode,csSpcWord, 0.0f, 0.0f, FALSE));
	listwc.AddTail(WCLItem(csSpcWord, csSpcCode, fcp, fcp, FALSE));
	listw.AddTail(WLItem(csSpcWord, fpp));

	NextSpcCode(csSpcCode);

	//add other soft keyboard characters 
	for (int i = 0; i < SOFTKEYBOARD_NUM*2; i++)
	{
		for (n = 0; n < SOFTKEYBOARD_LEN; n+=2)
		{
			if (gKeyArr[i][n+1] != 0)			//for soft pc-keyboard, this byte is 0, if adding this byte to csSpcWord, will make csSpcWords length 2
				csSpcWord = CString(gKeyArr[i][n]) + CString(gKeyArr[i][n+1]);
			else
				csSpcWord = CString(gKeyArr[i][n]);

			if ("" == csSpcWord)
				continue;

			nw = 1;
			fcp = 1.0f;
			listc.AddTail(CLItem(csSpcCode, nw, nw));
			listcw.AddTail(CWLItem(csSpcCode, csSpcWord, 0.0f, 0.0f, FALSE));
			listwc.AddTail(WCLItem(csSpcWord, csSpcCode, fcp, fcp, FALSE));
			listw.AddTail(WLItem(csSpcWord, fpp));

			NextSpcCode(csSpcCode);
		}
	}

	//append error code
	for (n = 0; n < 4; n++)
	{
		CString csErrCode = CString('z', n+1);
		CString csErrWord = CString(15, n+1);
		nw = 1;
		fcp = 1.0f;
		listc.AddTail(CLItem(csErrCode, nw, nw));
		listcw.AddTail(CWLItem(csErrCode, csErrWord, 0.0f, 0.0f, FALSE));
		listwc.AddTail(WCLItem(csErrWord, csErrCode, fcp, fcp, FALSE));
		listw.AddTail(WLItem(csErrWord, fpp));
	}

	//--write file
	cMessWnd->SetMessage("保存文件");
	cMessWnd->m_prog.SetRange32(0, listc.GetCount()+listw.GetCount()
				+listcw.GetCount()+listwc.GetCount()+listww.GetCount());
	cMessWnd->m_prog.SetStep(1);
	cMessWnd->m_prog.SetPos(0);

	//write code file
	for (posc = listc.GetHeadPosition(); posc != NULL; )
	{
		CLItem itemc = listc.GetNext(posc);
		strc = itemc.code;
		nw = itemc.numw;
		nw2 = itemc.numw2;
		//filecd.Write(&ic, 4);
		lenc = strc.GetLength();
		filec.Write(&lenc, 4);
		filec.Write(strc, lenc);
		filec.Write(&nw, 4);
		filec.Write(&nw2, 4);

		cMessWnd->m_prog.StepIt();
	}

	//write word file
	for (posw = listw.GetHeadPosition(); posw != NULL; )
	{
		itemw = listw.GetNext(posw);
		strw = itemw.word;
		fpp = itemw.pp;

		lenw = strw.GetLength();

		filew.Write(&lenw, 4);
		filew.Write(strw, lenw);
		filew.Write(&fpp, 4);

		cMessWnd->m_prog.StepIt();
	}

	//write cwmap file
	for (poscw = listcw.GetHeadPosition(); poscw != NULL; )
	{
		itemcw = listcw.GetNext(poscw);
		strc = itemcw.code;
		strw = itemcw.word;

		lenc = strc.GetLength();
		lenw = strw.GetLength();

		filecw.Write(&lenc, 4);
		filecw.Write(strc, lenc);
		filecw.Write(&lenw, 4);
		filecw.Write(strw, lenw);

		cMessWnd->m_prog.StepIt();
	}

	//write wcmap file
	//iw = 1;
	for (poswc = listwc.GetHeadPosition(); poswc != NULL; )
	{
		itemwc = listwc.GetNext(poswc);
		strw = itemwc.word;
		strc = itemwc.code;
		fcp = itemwc.cp;
		fcp2 = itemwc.cp2;

		lenw = strw.GetLength();
		lenc = strc.GetLength();

		filewc.Write(&lenw, 4);
		filewc.Write(strw, lenw);
		filewc.Write(&lenc, 4);
		filewc.Write(strc, lenc);
		filewc.Write(&fcp, 4);
		filewc.Write(&fcp2, 4);

		cMessWnd->m_prog.StepIt();
	}

	//write wwmap file
	for (posww = listww.GetHeadPosition(); posww != NULL; )
	{
		itemww = listww.GetNext(posww);
		strw = itemww.word;
		strw2 = itemww.wordc;
		ftp = itemww.tp;

		lenw = strw.GetLength();
		fileww.Write(&lenw, 4);
		fileww.Write(strw, lenw);

		lenw = strw2.GetLength();
		fileww.Write(&lenw, 4);
		fileww.Write(strw2, lenw);

		fileww.Write(&ftp, 4);

		cMessWnd->m_prog.StepIt();
	}

	filemb.Close();
	filec.Close();
	filew.Close();
	filecw.Close();
	filewc.Close();
	fileww.Close();

	return TRUE;
}

static BOOL ConvertPY(CString csMBFileName, CMessWnd* CMessWnd)
{
	CFile filemb;
	CFile filecw;
	CFileException e;

	if (!filemb.Open(csMBFileName, CFile::modeRead, &e))
	{
		MessageBox(NULL, "打开码表文件失败!", "错误", MB_ICONSTOP|MB_OK);
		return FALSE;
	}

	filecw.Open("..\\mb\\pinyin.dat", CFile::modeCreate|CFile::modeWrite, &e);
	

	CMap<int, int&, POSITION, POSITION&> cwidx;		//index for wcmap word
	CList<CWLItem, CWLItem&> listcw;

	unsigned char buffl[LINE_LEN*6];
	unsigned char buffc[CODE_LEN];
	unsigned char buffw[WORD_LEN];
	POSITION poscw;
	CString strw, strc, last_strw, last_strc, str1, str2;
	unsigned char newl[2] = {13, 10};
	int stop = 0;
	int numl;		//line number of origin mb-file
	int lenl, lenc, lenw;
	int idc;
	CWLItem itemcw;

	numl = CalcLineNum(&filemb);
	CMessWnd->SetMessage("转换拼音码表文件");
	CMessWnd->m_prog.SetRange32(0, numl);
	CMessWnd->m_prog.SetStep(1);

	//ic = 1;
	for ( ;; )
	{
		memset(buffl, 0, LINE_LEN*6);
		lenl = ReadLine(&filemb, buffl);
		if (lenl == 0)
			break;

		unsigned char* blp = buffl;
		memset(buffc, 0, CODE_LEN);
		lenc = ReadCode(blp, buffc);
		if (lenc == 0)
			break;

		idc = GetCodeId(CString(buffc));

		for( ; ; )
		{
			memset(buffw, 0, WORD_LEN);
			lenw = ReadWord(blp, buffw);

			if (lenw == 0)
				break;
			
			if (lenw%2 == 1)		// a word' length must be integral times of 2
			{
				MessageBox(NULL, CString("转换码表时发生错误,错误码")+CString(buffc), "错误", MB_ICONSTOP|MB_OK);
				stop = 1;
				break;
			}

			//add code-word item into CWT
			poscw = listcw.AddTail(CWLItem(buffc, buffw, 0.0, 0.0, FALSE));
//			if (0 == cwidx[idc])
//				cwidx[idc] = poscw;
		}

		if (stop)
			break;
	}

	if (stop)
		return FALSE;

	//write PINYIN file
	for (poscw = listcw.GetHeadPosition(); poscw != NULL; )
	{
		itemcw = listcw.GetNext(poscw);
		strc = itemcw.code;
		strw = itemcw.word;

		lenc = strc.GetLength();

		filecw.Write(&lenc, 4);
		filecw.Write(strc, lenc);
		filecw.Write(strw, 2);
	}

	return TRUE;
}

void CPageInit::OnButtonConvert()
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	CDC dc;
	dc.CreateDC("DISPLAY", NULL, NULL, NULL);
	int iw = dc.GetDeviceCaps(HORZRES);
	int ih = dc.GetDeviceCaps(VERTRES);
	dc.DeleteDC();

	messWnd.Create(IDD_WINDOW_SHOWMESS, 0);
	CRect rt, rtnew;
	messWnd.GetWindowRect(&rt);
	rtnew.left = iw/2-rt.Width()/2.0;
	rtnew.left = iw/2+rt.Width()/2.0;
	rtnew.top = ih/2-rt.Height()/2.0;
	rtnew.bottom = ih/2+rt.Height()/2.0;

	messWnd.SetWindowPos(&wndTopMost, rt.left, rt.top, rt.Width(), rt.Height(), SWP_SHOWWINDOW);
	messWnd.UpdateData(FALSE);	

	if (m_csFileName != "")
		Convert(m_csFileName, &messWnd);
	if (m_csFileName2 != "")
		ConvertPY(m_csFileName2, &messWnd);

	messWnd.DestroyWindow();
}

void CPageInit::OnButtonFileopen() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE);
	dlg.DoModal();

	m_csFileName = dlg.GetPathName();
	UpdateData(FALSE);
}


void CPageInit::OnButtonFileopen2() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE);
	dlg.DoModal();

	m_csFileName2 = dlg.GetPathName();
	UpdateData(FALSE);	
}

void CPageInit::OnChangeEditFilename() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CPropertyPage::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData();
	
}

void CPageInit::OnChangeEditFilename2() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CPropertyPage::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData();
}

BOOL CPageInit::OnSetActive() 
{
	// TODO: Add your specialized code here and/or call the base class
	CancelToClose();
	
	return CPropertyPage::OnSetActive();
}

⌨️ 快捷键说明

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