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

📄 inputer.h

📁 包含拼音和笔画输入的中文输入法,使用VC.NET2003开发.portable Chinese input method includes phonetic input and stroke, the
💻 H
📖 第 1 页 / 共 2 页
字号:


//
//	Inputer.h
//		汉字输入法
//	Author	:	zhanlc
//	Date	:	2003/9/5
//
#ifndef	__ChineseInputer_H__
#define	__ChineseInputer_H__

#include	<list>
#include	<vector>
#include	<map>
#include	<algorithm>
using	namespace	std;


namespace	Chinese 
{

/**
*	字
*/
class	Word
{
public :
	/**
	*	构造函数
	*/
	Word() 
	{
		Init( TCHAR('\0'), TCHAR('\0') );
	};
		
	/**
	*	根据字符串初始化
	*/
	Word( const TCHAR* szWord ) 
	{
		Init( szWord[0], szWord[1] );
	}
		
	/**
	*	根据字符串初始化
	*/
	Word( const CString& strWord ) 
	{
		Init( strWord [0], strWord [1] );
	}
		
	/**
	*	根据字节直接初始化
	*/	
	Word( TCHAR cLow, TCHAR cHigh ) 
	{
		Init( cLow, cHigh );
	};
		
	/**
	*	转换成字符串	
	*/
	operator CString() 
	{
		CString	strWord( TCHAR('\0'), 3 );
		strWord.SetAt( 0, m_Word[0] );
		strWord.SetAt( 1, m_Word[1] );
		
		return	strWord;
	}
		
	/**
	*	判断是否和字符串相等
	*/
	BOOL	operator==( const TCHAR* szWord ) const
	{
		return	szWord[0] == m_Word[0] && szWord[1] == m_Word[1];
	}
		
	/**
	*	判断是否和字符串相等
	*/ 
	BOOL	operator==( const CString&	szWord ) const
	{
		return	szWord[0] == m_Word[0] && szWord[1] == m_Word[1];
	}
		
	/**
	*	判断是否相等
	*/
	BOOL	operator==( const Word& wd ) const
	{
		return	wd.m_Word[0] == m_Word[0] && wd.m_Word[1] == m_Word[1];
	}

	/**
	*	比较运算符
	*/
	BOOL	operator<( const Word& wd ) const
	{
		//return	m_Word[1] < wd.m_Word[1]  &&  m_Word[0] < wd.m_Word[0];

		return	*( (unsigned short*)&m_Word[0]) < *( (unsigned short*)&wd.m_Word[0]);
	}
		
	/**
	*	获得低字节
	*/
	TCHAR	GetLow() const
	{
		return	m_Word[0];
	}
		
	/**
	*	获得高字节
	*/
	TCHAR	GetHigh() const
	{
		return	m_Word[1];
	}

// I/O
public :
	/**
	*	保存到文件流
	*/
	template	<class	TStream>
	void	operator >> ( TStream& os ) const
	{
		os	<< m_Word[0] << m_Word[1];
	};

	/**
	*	从流中读入
	*/
	template	<class	TStream>
	void	operator<<( TStream& os )
	{
		os	>> m_Word[0]	>> m_Word[1] ;
	}
	
protected :
	/**
	*	初始化
	*/
	void	Init( TCHAR cLow, TCHAR cHigh )
	{
		m_Word[0]	=	cLow;
		m_Word[1]	=	cHigh;
	};
		
protected :
	/**
	*	汉字	
	*/
	TCHAR		m_Word[2];
};

/**
*	笔顺类
*		"12345"	---	"一丨丿丶乙"  
*/
class	StrokesOrder
{
public :
	/**
	*	构造函数
	*/
	StrokesOrder( const TCHAR* sz=_T(""));

	/**
	*	将笔顺转换成数字字符形式
	*/
	const CString&		GetAsDigits() const
	{
		return	m_DigitStroks;
	};

	/**
	*	重载字符转换, 数字字符形式
	*/
	operator const CString&()	const
	{
		return	m_DigitStroks;
	};
	
	/**
	*	将笔顺转换成笔画形式
	*/
	CString		GetAsStroks() const;

	/**
	*	获得笔画长度
	*/
	int			GetLength() const
	{
		return	m_DigitStroks.GetLength();
	};

	/**
	*	比较操作符
	*/
	BOOL	operator==( const StrokesOrder& odr ) const
	{
		return	m_DigitStroks	==	odr.m_DigitStroks;
	}

	/**
	*	比较操作符
	*/
	BOOL	operator==( const TCHAR* szOdr ) const
	{
		return	operator==( StrokesOrder(szOdr) );
	}

	/**
	*	比较操作符
	*/
	BOOL	operator==( const CString& szOdr ) const
	{
		return	operator==( StrokesOrder(szOdr) );
	}

	/**
	*	设置笔顺
	*/
	BOOL	Set( const TCHAR* szStrokesOrder );

	/**
	*	赋值函数
	*/
	BOOL	operator=( const TCHAR* szStrokesOrder )
	{
		return	Set( szStrokesOrder );
	}


public	:
	/**
	*	横
	*/
	static	Word	s_Heng;

	/**
	*	竖
	*/
	static	Word	s_Shu;

	/**
	*	撇
	*/
	static	Word	s_Pie;

	/**
	*	捺
	*/
	static	Word	s_Na;

	/**
	*	折
	*/
	static	Word	s_Zhe;

	/**
	*	横
	*/
	static	TCHAR	s_cHeng;

	/**
	*	竖
	*/
	static	TCHAR	s_cShu;

	/**
	*	撇
	*/
	static	TCHAR	s_cPie;

	/**
	*	捺
	*/
	static	TCHAR	s_cNa;

	/**
	*	折
	*/
	static	TCHAR	s_cZhe;

protected :
	/**
	*	以数字形式表示得笔顺
	*/		
	CString		m_DigitStroks;
};

/**
*	拼音组, 用于将多个拼音组合一个字符串拼音,可以用于多音字
*/
class	SpellGroup
{
public :
	typedef		/*list*/vector<CString>				StringList;
	typedef		StringList::const_iterator	const_iterator;
	typedef		StringList::iterator		iterator;
	
	/**
	*	构造函数
	*/
	SpellGroup();

	/**
	*	构造函数
	*/
	SpellGroup( const TCHAR* szGroup );
	
	/**
	*	获得拼音个数
	*/
	int		GetCount() const;

	/**
	*	获得第i个拼音
	*/
	const CString&	operator[]( int i );

	/**
	*	将所有拼音转换成一个字符串
	*/
	CString	GetAsString() const;

	/**
	*	操作符重载
	*/
	operator CString() const;

	/**
	*	加入拼音
	*/
	void	Add( const TCHAR* szSpell );

	/**
	*	加入组
	*/
	void	AddGroup( const TCHAR* szSpell );

	/**
	*	删除拼音
	*/
	void	Remove( const TCHAR* szSpell );

	/**
	*	删除所有拼音
	*/
	void	RemoveAll();

	/**
	*	开始迭代器
	*/
	iterator	begin() 
	{
		return	m_vSpell.begin();
	}

	/**
	*	开始迭代器
	*/
	iterator	end() 
	{
		return	m_vSpell.end();
	}

	/**
	*	开始迭代器
	*/
	const_iterator	begin() const
	{
		return	m_vSpell.begin();
	}

	/**
	*	开始迭代器
	*/
	const_iterator	end() const
	{
		return	m_vSpell.end();
	}

	/**
	*	包含某个注音
	*/
	BOOL	HaveSpell( const TCHAR* szSpell ) const
	{
		return	find( m_vSpell.begin(), m_vSpell.end(), szSpell) != m_vSpell.end();
	}

	/**
	*	将多个拼音组相加
	*/
	static	CString		Add( const CString& strOne, const CString& strTwo )
	{
		CString		strNew;
		if ( strOne == _T("") )
		{
			strNew = strTwo;
		}
		else
		{
			strNew	=	strOne;
			if ( strTwo != _T("") )
				strNew	+=	_T(":") + strTwo;
		}
		return	strNew;
	}

// I/O
public :
	/**
	*	保存到文件流
	*/
	template	<class	TStream>
	void	operator >> ( TStream& os ) const
	{
		int	nLen	=	m_vSpell.size();
		os	<< nLen;
		for ( int i=0; i<nLen; i++ )
			os << m_vSpell[i];
	};

	/**
	*	从流中读入
	*/
	template	<class	TStream>
	void	operator<<( TStream& os )
	{
		int	nLen;
		os >> nLen ;

		/*
		ASSERT( nLen > 0 );
		m_vSpell.resize(nLen);

		for ( int i=0; i<nLen; i++ )
			os >> m_vSpell[i];
		*/
		if ( nLen > 0 )
		{
			m_vSpell.resize(nLen);
			
			for ( int i=0; i<nLen; i++ )
				os >> m_vSpell[i];
		}
		else
			m_vSpell.clear();
	}
	

protected :
	/**
	*	拼音集合
	*/
	StringList	m_vSpell;
};

/**
*	中文字典
*/
class	ChineseDictionary
{
public :
	/**
	*	部首
	*/
	class	BSWord	:	public Word
	{
	public :
		BSWord()	{}
		/**
		*	根据字符串初始化
		*/
		BSWord( const TCHAR* szWord, 
			    int nStrokes, 
				const TCHAR* szDscr )  : Word(szWord)
		{
		}
		
		/**
		*	根据字符串初始化
		*/
		BSWord( const CString& strWord,
				int nStrokes, 
				const TCHAR* szDscr) : Word(strWord)
		{
		}
		
		/**
		*	根据字节直接初始化
		*/	
		BSWord( TCHAR cLow, TCHAR cHigh,
				int nStrokes, 
				const TCHAR* szDscr ) : Word(cLow, cHigh )
		{
		};

		/**
		*	获得描述
		*/
		const	CString&	GetDescrib() const
		{
			return	m_strDscr;
		}

		/**
		*	获得笔画
		*/
		int		GetStrokes( ) const
		{
			return	m_nStrokes;
		}

	protected :
		/**
		*	初始化	
		*/
		BOOL	Init( int nStrokes, const TCHAR* szDscr )
		{
			m_nStrokes	=	nStrokes;
			m_strDscr	=	szDscr;
			return	TRUE;
		}

	protected :
		/**
		*	描述
		*/
		CString		m_strDscr;

		/**
		*	笔画
		*/
		int			m_nStrokes;
	};

	/**
	*	部首容器
	*/
	class	BSWords
	{
	public :
		/**
		*	部首枚举器
		*/
		typedef		const	BSWord*		iterator;

		/**
		*	获得起始部首
		*/
		iterator	begin() const;

		/**
		*	结束
		*/
		iterator	end() const;
			
		/**
		*	部首数目
		*/
		int			size() const;
	};


	/**
	*	字解释
	*/
	class	WordComment
	{
		friend	class	ChineseDictionary;
	public :
		/**
		*	构造函数
		*/
		WordComment() /*: m_strSpell(_T(""))*/ {};

		/**
		*	构造函数
		*/
		WordComment( Word wBS, unsigned short nStrokes, 
					 const SpellGroup& sgSpell, 
					 const StrokesOrder& sOrder )  : 
			m_BS(wBS), m_nStroks(nStrokes), m_StroksOrder(sOrder),
			m_SpellGroup(sgSpell)
		{
				//m_strSpell	=	sgSpell.GetAsString();
		};

		/**
		*	获得笔画数
		*/
		int		GetStrokes() const
		{
			return	m_nStroks;
		};

		/**
		*	设置笔画数
		*/
		void	SetStrokes( unsigned short nStrokes ) 
		{
			m_nStroks	=	nStrokes;
		};

		/**
		*	获得部首
		*/
		Word	GetBS() const
		{
			return	m_BS;
		};

		/**
		*	设置部首
		*/
		void	SetBS( Word wdBS ) 
		{
			m_BS	=	wdBS;
		};

		/**
		*	获得笔顺
		*/
		const	StrokesOrder& GetStrokesOrder() const
		{
			return m_StroksOrder;
		};

		/**
		*	设置笔顺
		*/
		void	SetStrokesOrder( const StrokesOrder& sodr ) 
		{
			m_StroksOrder	=	sodr;
		};

		/**
		*	获得拼音组
		*/
		const SpellGroup&	GetSpellGroup( ) const
		{
			return	m_SpellGroup;//SpellGroup(m_strSpell);
		}

		/**
		*	设置新的拼音组
		*/
		void	SetSpellGroup( const SpellGroup& sg )
		{
			//m_strSpell	=	sg.GetAsString( );
			m_SpellGroup	=	sg;
		}

		/**
		*	判断是否有某个注音
		*/
		BOOL		HaveSpell( const TCHAR* szSpell ) const
		{
			return	/*SpellGroup(m_strSpell).*/m_SpellGroup.HaveSpell(szSpell);
		}

	// I/O 
	public :
		/**
		*	保存到文件流
		*/
		template	<class	TStream>
		void	operator >> ( TStream& os ) const
		{
			// 部首
			m_BS	>>	os;
			os << m_nStroks;
			//os << /*m_strSpell*/m_SpellGroup << m_StroksOrder.GetAsDigits();
			m_SpellGroup >> os;
			os << m_StroksOrder.GetAsDigits( );
		}

		/**
		*	从流中读入
		*/
		template	<class	TStream>
		void	operator<< ( TStream& os )
		{
			// 部首
			m_BS	<<	os;

			CString	strStrokesOrder;
			os >> m_nStroks;
			//os >> /*m_strSpell*/m_SpellGroup >> strStrokesOrder;
			m_SpellGroup << os;
			os >> strStrokesOrder;
			m_StroksOrder = strStrokesOrder;
		}

	protected :
		/**
		*	部首
		*/
		Word	m_BS;

		/**
		*	笔画数
		*/
		unsigned short	m_nStroks;

		/**
		*	拼音, 多音字用:分割
		*/
		//CString	m_strSpell;
		SpellGroup	m_SpellGroup;

		/**
		*	笔顺
		*/
		StrokesOrder	m_StroksOrder;
	};

	/**
	*	字典条目
	*/
	class	WordItem
	{
		friend	class	ChineseDictionary;

		/**
		*	构造函数
		*/
		WordItem( Word wd, const WordComment* wdcm )  
			: m_Word(wd), m_pComment(wdcm)
		{
		};

	public :
		/**
		*	构造函数
		*/
		WordItem() : m_Word( TCHAR('0'), TCHAR('0') ), m_pComment(NULL)
		{
		};


		/**
		*	获得字
		*/
		Word	GetWord() const
		{
			return	m_Word;
		}

		/**
		*	获得字注解
		*/
		const	WordComment&	GetComment() const
		{
			ASSERT( m_pComment );
			return	*m_pComment;
		}

		/**
		*	比较运算
		*/
		BOOL	operator==( const WordItem& witm ) const
		{
			return	m_Word == witm.m_Word && 
					m_pComment == witm.m_pComment;
		}

		/**
		*		比较运算
		*/
		BOOL	operator!=( const WordItem& witm ) const
		{
			return	!operator==( witm );
		}

		/**
		*	判断是否是合法的词条
		*/
		operator BOOL() const;

	protected :
		/**
		*	字
		*/
		Word		   m_Word;

		/**
		*	字注解
		*/
		const WordComment*    m_pComment;	
	}; // end of word item

	/**
	*	定义字典条目得引用
	*/
	typedef	const	WordItem	WordItemRef;

	/**
	*	非法词条
	*/
	static	WordItem	NullWordItemRef;

	/**
	*	查某个字的解释
	*/
	static	WordItemRef		Get( Word wdLookup );

	/**
	*	加入词条
	*/
	static	void			AddWord( Word wd, const WordComment& wdcm );

	/**
	*	清空所有词条
	*/
	static	void		Clear(  );

	/**
	*	从文件中装入字典
	*/
	static	BOOL	Load( const TCHAR* szFile );

	/**
	*	将字典保存到文件中
	*/
	static	BOOL		Save( const TCHAR* szFile );

//=======================================================================================
//
//	查询器
//
//=======================================================================================
public :

	/**
	*	汉字枚举器
	*/
	typedef		list<Word>	WordEnumerator;

	/**
	*	汉字表
	*/
	typedef	map<Word, WordComment>	WordTable;

	/**
	*	查询器基类
	*/
	class	QueryBase
	{
	protected :
		/**
		*	初始化查询器
		*/
		virtual	BOOL	Initialize( WordTable* pWordTable);

		/**
		*	销毁
		*/
		virtual	void	Uninitialize( );
	protected :
		/**
		*	汉字表
		*/
		WordTable*	m_pWordTbl;
	};

	/**
	*	部首查询器
	*/
	class	BSQuery : public QueryBase
	{
		friend	class	ChineseDictionary;
	public :
		/**
		*	根据输入部首, 查询汉字
		*/
		BOOL	QueryWords( const TCHAR* szBuShou, WordEnumerator& enumer );

	};

	/**
	*	笔画查询器
	*/
	class	StrokesQuery : public QueryBase
	{
		friend	class	ChineseDictionary;
	public :

		/**
		*	根据输入笔画, 查询汉字
		*/
		BOOL	QueryWords( int nStroks, WordEnumerator& enumer );

		/**
		*	根据输入笔画, 查询汉字
		*/
		BOOL	QueryWords( const TCHAR* szStrokes, WordEnumerator& enumer );
	};

	/**
	*	字符索引基类
	*/
	class	StringQueryBase	:	public	QueryBase
	{
	protected :
		//typedef		vector<Word>			Words;
		typedef		list<Word>			Words;
		typedef		map<CString, Words>		String2Words;

	public :  //!by zqp edit 205.06.17  protected edit public
 		/*
		*	初始化查询器
		*/
		BOOL	Initialize( WordTable* pWordTable);

		/**
		*	销毁
		*/
		void	Uninitialize( );

		/**
		*	初始化字条目
		*/
		virtual	void	InitWord( Word wd, const WordComment& wc ) { };

		/**
		*	访问迭代器
		*/
		class	iterator : public String2Words::iterator
		{
			typedef		String2Words::iterator	base_class;
		public :
			iterator( base_class it )  : base_class (it) 
			{
			}

			/**
			*	重载指针操作
			*/
			Words&	operator*( )
			{
				base_class&	it	=	*this;
				Words&	words	=	it->second;

				return	words;
			}
		}; 

		/**
		*	返回起始迭代位置
		*/
		iterator	begin( )
		{
			return	iterator( m_String2Words.begin() );
		}

		iterator	end()
		{
			return	iterator( m_String2Words.end() );
		}
		
	protected :
		/**
		*	加入字符到字得映射
		*/
		void	AddMap( const TCHAR* szString, Word wd );

		/**
		*	获得映射表
		*/
		String2Words&	GetMapTable( );

	public :
		/**
		*	根据字符串查询	
		*/
		BOOL	QueryWords( const TCHAR* szSpell, WordEnumerator& enumer );

	protected :
		
		/**
		*	字符串到字得映射
		*/
		String2Words		m_String2Words;
	};

	/**
	*	拼音查询器
	*/
	class	SpellQuery  : public StringQueryBase//QueryBase
	{
		friend	class	ChineseDictionary;
	
	protected :
		/*
		*	初始化查询器
		*/
		BOOL	Initialize( WordTable* pWordTable);

		/**
		*	初始化字
		*/
		void	InitWord( Word wd, const WordComment& wc );
	};

	/**
	*	笔顺查询器
	*/
	class	StrokesOrderQuery	:	public SpellQuery
	{
		friend	class	ChineseDictionary;

⌨️ 快捷键说明

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