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

📄 hikerwbfunc.cpp

📁 自己用Markov模型做的一个整句物笔输入法的原型
💻 CPP
📖 第 1 页 / 共 4 页
字号:
  #include "stdafx.h"
#include <afxtempl.h>
#include "HikerWBFunc.h"
#include "softkeyboard.h"
#include <sys/TIMEB.H>
#include "ProgWnd.h"


#define PPINC		1
#define TPINC		0.100f
#define CPINC		0.100f
#define	TPSUMLMT	0.950f


//struct of code table item
struct CTItem
{
	CString		code;
	int			wnum;
	int			wnum2;
	CTItem (){
		code = "";
		wnum = 0;
		wnum2 = 0;
	}

	CTItem (CString c, int n, int n2){
		code = c;
		wnum = n;
		wnum2 = n2;
	}
};

//struct of word table item
struct WTItem
{
	CString		word;
	float		prior;
	WTItem (){
		word = "";
		prior = 0.0f;
	}
	WTItem (CString w, float p){
		word = w;
		prior = p;
	}
};

//struct of code-word table item
struct CWTItem
{
	CString		code;
	CString		word;
	CWTItem (){
		code = "";
		word = "";
	}
	CWTItem (CString c, CString w){
		code = c;
		word = w;
	}
};

//struct of word-code table item (conditional probabilitie table)
struct CPTItem
{
	CString		word;
	CString		code;
	float		cp;
	float		cp2;
	CPTItem (){
		word = "";
		code = "";
		cp = 0.0;
		cp2 = 0.0;
	}
	CPTItem (CString w, CString c, float p, float p2){
		word = w;
		code = c;
		cp = p;
		cp2 = p2;
	}
};

//struct of word-word table item (transition probabilitie table)
struct TPTItem
{
	CString		word;
	CString		wordc;
	float		tp;
	TPTItem (){
		word = "";
		wordc = "";
		tp = 0.0;
	}
	TPTItem (CString w, CString wc, float p){
		word = w;
		wordc = wc;
		tp = p;
	}
};

//struct of pinyin code-word table item
struct PYTItem
{
	CString		code;
	CString		word;
	PYTItem (){
		code = "";
		word = "";
	}
	PYTItem (CString c, CString w){
		code = c;
		word = w;
	}
};

static CList<CTItem, CTItem&> CT;			//code table
static CArray<POSITION, POSITION&> CIdx;	//index table for code

CList<WTItem, WTItem&> WT;					//word table
static CArray<POSITION, POSITION&> WIdx;	//index table for word

CList<CWTItem, CWTItem&> CWT;				//code-word table
static CArray<POSITION, POSITION&> CWIdx;	//index table for code-word

CList<CPTItem, CPTItem&> CPT;				//conditional probability table
static CArray<POSITION, POSITION&> CPIdx;	//index table for conditional probabilities

CList<TPTItem, TPTItem&> TPT;				//transition probability table
static CArray<POSITION, POSITION&> TPIdx;	//index table for transition probabilities

CList<PYTItem, PYTItem&> PYT;				//pinyin code-word table
static CArray<POSITION, POSITION&> PYIdx;	//index of pinyin table


static unsigned char *pcode[SENTLEN];		//the following variales only work as temporary variable
static unsigned char *pword[SENTLEN];
static double *b[DUPWORD];
static double *a[DUPWORD];
static double sumwordfre;	//sum of all word's frequency
//static int	 wordnum;
static int maxcodelen;
static int charset = 0;		// 0-GB2312, 1-GBK

extern HINSTANCE hInst;
static HHOOK hHook;			//钩子句柄
static HHOOK hHookAct;			//钩子句柄
static BOOL gbKeyProcSign = FALSE;
static HANDLE ghThread;
static BOOL gbKeyProcReturn;


CProgWnd* CreateProgWnd()
{
	CProgWnd *progWnd = new CProgWnd;
	LPCTSTR cname = AfxRegisterWndClass(0);
	//progWnd.Create(cname, "ProgWnd", WS_VISIBLE|WS_POPUPWINDOW|WS_CLIPSIBLINGS, rt, AfxGetMainWnd(), 0);
	progWnd->CreateEx(WS_EX_PALETTEWINDOW|WS_EX_LEFT|WS_EX_TOOLWINDOW, cname, "ProgWnd",
			WS_OVERLAPPED|WS_THICKFRAME|WS_CLIPSIBLINGS, 0, 0, 0, 0, 0, 0, 0);

	CString csCap;
	AfxGetMainWnd()->GetWindowText(csCap);
	progWnd->SetWindowText(csCap);
	progWnd->Show();	

	return progWnd;
}

void DeleteProgWnd(CProgWnd* progWnd)
{
	progWnd->DestroyWindow();
	delete progWnd;
}
//CMap<CString, CString&, int, int&> W2WIDMap;
//CMap<int, int&, CString, CString&> WID2WMap;
//CMap<int, int&, float, float&> WID2PPMap;
//CMap<int, int&, int, int&> WID2CPIMap;
//CArray<int, int&> CPICodeArr;
//CArray<float, float&> CPICPArr;

//int FindCodeID(CString code)
//{
//	int codeid;
//
//	if (C2CIDMap.Lookup(code, codeid))
//		return codeid;
//	else
//		return 0;		//the code doesn't exist
//}
//
//
//BOOL FindWord(int wordid, CString word)
//{
//	if (WID2WMap.Lookup(wordid, word))
//		return TRUE;
//	else
//		return FALSE;		//the word id doesn't exist
//}
//
//BOOL Code2CodeID(unsigned char* pc, int pclen, int* pcid, int& numc)
//{
//	int lenc = 0;
//	unsigned char code[5];
//	int codeid;
//
//	numc = 0;
//	memset(code, 0, 5);
//
//	for (int i = 0; i < lenc; i++)
//	{
//		if (pc[i] == 32)
//		{
//			if (lenc > 0)
//			{
//				codeid = FindCodeID(code);
//				pcid[numc] = codeid;		//! if the code not exist, the codeid will be 0;
//
//				memset(code, 0, 5);
//				lenc = 0;
//				numc++;
//			}
//		}
//		else
//		{
//			code[lenc++] = pc[i];
//		}
//
//	}
//	return TRUE;
//}

//assign each code a unique id(assumed all codes's length is no more than 4)
static int GetCodeId(char* code)
{
	char c;
	int id = 0;
	int len = min(/*code.GetLength()*/strlen(code), 4);		//note!, 4 can not be changed to maxcodelen
	for (int i = 0; i < len; i++)
	{
		c = /*code.GetAt(i)*/code[i] - 'a'+1;
		id += (c << (5*i));
	}

	return id;
}

//unsigned maxwid[4] = {0, 0, 0, 0};				////////////
//unsigned minwid[4] = {0xFF, 0xFF, 0xFF, 0xFF};	////////////

//assign each word a id. the id is generated according to the first Chinese characters.
static int GetWordId(char* word)
{
	unsigned char c;
	int id = 0;
	//int len = min(word.GetLength(), 2);
	int len = min(2, strlen(word));
	for (int i = 0; i < len; i++)
	{
		c = word[i];
		id += (c << (i<<3));		//8*i

//		if (c > maxwid[i])				///////////
//			maxwid[i] = c;
//		if (c < minwid[i])
//			minwid[i] = c;				///////////
	}

	return id;
}

static POSITION CFind(CString code)
{
	int id = GetCodeId((LPSTR)(LPCSTR)code);
	return CIdx[id];

//	for (int i = 0; i < C1Arr.GetSize(); i++)
//	{
//		if (C1Arr[i] == code)
//			return i;
//	}
//
//	return -1;
}

//find the word's position in word table
static POSITION WFind(CString word)
{
	int id = GetWordId((LPSTR)(LPCSTR)word);
	if (WIdx[id] == 0)
		return 0;

	POSITION pos = WIdx[id];
	CString csIdxWord = WT.GetAt(pos).word.Left(2);
	CString csWord;
	for (; pos; WT.GetNext(pos))
	{
		csWord = WT.GetAt(pos).word;
		if (csWord.Left(2) != csIdxWord)
			break;
		else if (csWord == word)
			return pos;
	}

	return 0;
}

static BOOL WLocate(CString word, POSITION& pos)
{
	pos = 0;
	int id = GetWordId((LPSTR)(LPCSTR)word);
	if (WIdx[id] == 0)
		return FALSE;

	pos = WIdx[id];
	CString csIdxWord = WT.GetAt(pos).word.Left(2);
	CString csWord;
	for (; pos; WT.GetNext(pos))
	{
		csWord = WT.GetAt(pos).word;
		if (csWord.Left(2) != csIdxWord)
		{
			WT.GetPrev(pos);
			
			return FALSE;
		}
		else if (csWord == word)
			return TRUE;
	}
	pos = WT.GetTailPosition();

	return FALSE;
}

//find the code's first position in code-word table
static POSITION CWFind(CString code)
{
	int id = GetCodeId((LPSTR)(LPCSTR)code);
	return CWIdx[id];

//	for (int i = 0; i < CW1Arr.GetSize(); i++)
//	{
//		if (CW1Arr[i] == code)
//			return i;
//	}
//
//	return -1;
}

//find the code's iword-th position in code-word table
static POSITION CWFind(CString code, int iword)
{
	int id = GetCodeId((LPSTR)(LPCSTR)code);

	if (CWIdx[id] == 0)
		return 0;

	POSITION pos = CWIdx[id];
	for( int i = 0; i < iword; i++)
	{
		CWT.GetNext(pos);
	}
	return pos;

//	BOOL FindCode = 0;
//	int iw = 0;
//
//	for (int i = 0; i < CW1Arr.GetSize(); i++)
//	{
//		if (!FindCode && CW1Arr[i] == code)
//		{
//			FindCode = 1;
//			iw = 0;
//		}
//		if (FindCode)
//		{
//			if (CW1Arr[i] != code)
//				break;
//			else if (iword == iw)
//				return i;
//
//			iw++;
//		}
//	}
//
//	return -1;
}

//find the code-word item in code-word table.
//if the item not found, return FALSE, outherwise, TRUE
//posloc gives the position when found, 
//if the code exists, posloc gives the code's the last appearing position,
//otherwise, it gives last appearing position+1, or 0;
static BOOL CWLocate(CString code, CString word, POSITION& posloc)
{	
	CWTItem item;
	posloc = 0;
	int id = GetCodeId((LPSTR)LPCSTR(code));
	posloc = CWIdx[id];
	if (posloc == 0)
		return FALSE;

	for (; posloc; CWT.GetNext(posloc))
	{
		item = CWT.GetAt(posloc);
		if ( item.code != code)
		{
			CWT.GetPrev(posloc);

			return FALSE;
		}
		else if (item.word == word)
			return TRUE;
	}
	posloc = CWT.GetTailPosition();

	return FALSE;
}


//find the word's first position in word-code table
static POSITION CPFind(CString word)
{
	CString csWord;
	int id = GetWordId((LPSTR)(LPCSTR)word);
	if (CPIdx[id] == 0)
		return 0;

	for (POSITION pos = CPIdx[id]; pos; CPT.GetNext(pos))	
	{
		csWord = CPT.GetAt(pos).word;

		if (csWord.Left(2) != word.Left(2))
			return 0;
		else if (csWord == word)
			return pos;
	}

	return 0;
}

//find the word-code's position in word-cope table
static POSITION CPFind(CString word, CString code)
{
	int id = GetWordId((LPSTR)(LPCSTR)word);

	if (CPIdx[id] == 0)
		return 0;

	CPTItem item;
	BOOL bHasLoc = FALSE;
	for (POSITION pos = CPIdx[id]; pos; CPT.GetNext(pos))	
	{
		item = CPT.GetAt(pos);
		if (item.word == word)
			bHasLoc = TRUE;

		if (bHasLoc)
		{
			if (item.word != word)
				break;
			else if (item.code == code) 
				return pos;
		}
		else if (item.word.Left(2) != word.Left(2))
		{
			break;
		}
	}

	return 0;

//	BOOL FindWord = 0;
//
//	for (int i = 0; i < CP1Arr.GetSize(); i++)
//	{
//		if (!FindWord && CP1Arr[i] == word)
//		{
//			FindWord = 1;
//		}
//		if (FindWord)
//		{
//			if (CP1Arr[i] != word)
//				break;
//			else if (CP2Arr[i] == code) 
//				return i;
//		}
//	}
//
//	return -1;
}

//find the word-code item in word-code table.
//if the item not found, return FALSE, outherwise, TRUE
//posloc gives the position when found, 
//if the word exists, posloc gives the code's the last appearing position,
//otherwise, it gives last appearing position+1, or 0;
static BOOL CPLocate(CString word, CString code, POSITION& pos)
{
	int id = GetWordId((LPSTR)(LPCSTR)word);
	pos = CPIdx[id];
	if (pos == 0)
		return FALSE;

	CPTItem item;
	BOOL bHasLoc = FALSE;
	for (; pos; CPT.GetNext(pos))
	{
		item = CPT.GetAt(pos);

		if (item.word == word)
			bHasLoc = TRUE;

		if (bHasLoc)
		{
			if (item.word != word)
			{
				CPT.GetPrev(pos);

				return FALSE;
			}
			else if (item.code == code) 
				return TRUE;
		}
		else if (item.word.Left(2) != word.Left(2))
		{
			CPT.GetPrev(pos);

			return FALSE;
		}
	}
	pos = CPT.GetTailPosition();

	return FALSE;
}

//find first char of a chinese-character's code
static BOOL Find1CCode(CString csWord, CString& csCode)
{
	csCode = "";
	POSITION pos = CPFind(csWord);
	CPTItem item;

	if (pos)
	{
		item = CPT.GetAt(pos);
		csCode = item.code.Left(1);

		return TRUE;
	}

	return FALSE;
}

//find 2 lead char of a chinese-character's code
static BOOL Find2CCode(CString csWord, CString& csCode)
{
	csCode = "";
	POSITION pos = CPFind(csWord);
	CPTItem item;

	for (; pos; CPT.GetNext(pos))
	{
		item = CPT.GetAt(pos);
		if (item.word != csWord)
			break;
		else
		{
			if (item.code.GetLength() >= 2)
			{
				csCode = item.code.Left(2);
				return TRUE;
			}
		}
	}

	return FALSE;
}

//find the first words position in word-word table
static POSITION TPFind(CString word)
{
	int id = GetWordId((LPSTR)(LPCSTR)word);

	if (TPIdx[id] == 0)
		return 0;

	return TPIdx[id];
}

//find the word-word's position in word-word table
static POSITION TPFind(CString word, CString wordc)
{
	int id = GetWordId((LPSTR)(LPCSTR)word);

	if (TPIdx[id] == 0)
		return 0;

	BOOL bHasLoc = FALSE;
	for (POSITION pos = TPIdx[id]; pos; TPT.GetNext(pos))
	{
		if (TPT.GetAt(pos).word == word)
			bHasLoc = TRUE;

		if (bHasLoc)
		{
			if (TPT.GetAt(pos).word != word)
				break;
			else if (TPT.GetAt(pos).wordc == wordc)
				return pos;
		}
	}

	return 0;

//	BOOL FindWord = 0;
//
//	for (int i = 0; i < TP1Arr.GetSize(); i++)
//	{
//		if (!FindWord && TP1Arr[i] == word)
//		{
//			FindWord = 1;
//		}
//		if (FindWord)
//		{
//			if (TP1Arr[i] != word)
//				break;
//			else if (TP2Arr[i] == wordc)
//				return i;
//		}
//	}
//
//	return -1;
}


//find the word-word's position in word-word table
static double TPSum(CString word, int& wordcnum)
{
	TPTItem itemtp;
	double tp = 0.0;
	POSITION pos = TPFind(word);
	wordcnum = 0;

	for (; pos; )		//sum all existing item's tp, note the sum must be less than 1
	{
		itemtp = TPT.GetNext(pos);
		if (itemtp.word != word)
			break;

⌨️ 快捷键说明

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