📄 qim.cpp
字号:
#include "Qim.h"
#include <fstream>
using namespace std ;
const int min_dynamic_word_id = 20769;
bool Qim::InsertDynamicPhrase(const string& py, const string& strPrase, double frequency)
{
int id ;
if (m_BiHashTable.GetIDAndInsertByWord(strPrase, id))
{
return m_DynamicPraseTable.InsertPhrase(py, id, frequency) ;
}
return false;
}
bool Qim::InsertDynamicPhrase1(string py, string strSlimcutpy, string strPhrase,double frequency)
{
int id;
if (m_BiHashTable.GetIDAndInsertByWord(strPhrase, id))
{
if( id >= min_dynamic_word_id)
{
m_DynamicPraseTable.InsertPhrase(py, id, frequency) ;
m_DynamicPraseTable.InsertPhrase(strSlimcutpy,id,frequency);
}
return true;
}
return false;
}
bool Qim::InsertStaticPhrase(const string& py, const string& strPrase, double frequency)
{
int id ;
if (m_BiHashTable.GetIDByWord(strPrase, id))
{
m_StaticPhraseTable.InsertSingleWord(py, frequency, id) ;
return true ;
}
else if( m_BiHashTable.GetIDAndInsertByWord(strPrase, id) )
{
m_StaticPhraseTable.InsertSingleWord(py, frequency, id) ;
return true ;
}
return false ;
}
//Get the word by py, using longest-matching strategy, return the longest matching count
int Qim::GetStaticWordByLongestMatchedPy(const string& py, vector<string>& vecPhraseTable)
{
vector<int> idTable ;
vecPhraseTable.clear() ;
int maxMatchedLength = m_StaticPhraseTable.GetWordByLongestMatchedPy(py, idTable) ;
if(idTable.size()>0)
{
for (size_t i = 0; i<idTable.size(); i++)
{
int id = idTable[i] ;
string word ;
if( m_BiHashTable.GetWordByID(id, word) )
{
vecPhraseTable.push_back(word) ;
}
}
}
return maxMatchedLength ;
}
int Qim::GetDynamicPraseByLongestMatchedPy(const string& py, vector<string>& vecPhraseTable)
{
vecPhraseTable.clear() ;
vector<ID> vecResultID ;
int longestMatchLength = m_DynamicPraseTable.GetPhraseByLongestMatchPy(py, vecResultID) ;
for (size_t i = 0; i<vecResultID.size(); i++)
{
int id = vecResultID[i].getid() ;
string phrase ;
if ( m_BiHashTable.GetWordByID(id, phrase))
{
vecPhraseTable.push_back(phrase) ;
}
}
return longestMatchLength ;
}
bool Qim::IncreaseStaticFrequency(const string& py, const string& strPhrase)
{
int id;
if ( m_BiHashTable.GetIDByWord(strPhrase, id) )
{
m_StaticPhraseTable.IncreaseFrequency(py, id) ;
return true ;
}
return false ;
}
bool Qim::IncreaseDynamicFrequency(const string& py, const string& strPhrase)
{
int id ;
if (m_BiHashTable.GetIDByWord(strPhrase, id))
{
return m_DynamicPraseTable.IncreaseFrequency(py, id) ;
}
return false ;
}
bool Qim::Save(const string& strStaticTableFilename, const string& strDynamicTableFilename, const string& strHashtableFilename)
{
bool bRet[3] ;
bRet[0] = m_StaticPhraseTable.Save(strStaticTableFilename) ;
bRet[1] = m_DynamicPraseTable.Save(strDynamicTableFilename) ;
bRet[2] = m_BiHashTable.Save(strHashtableFilename) ;
return ( bRet[0] && bRet[1] && bRet[2] ) ;
}
bool Qim::Load(const string& strStaticTableFilename, const string& strDynamicTableFilename, const string& strHashtableFilename)
{
bool bRet[3] ;
bRet[0] = m_StaticPhraseTable.Load(strStaticTableFilename) ;
bRet[1] = m_DynamicPraseTable.Load(strDynamicTableFilename) ;
bRet[2] = m_BiHashTable.Load(strHashtableFilename) ;
return ( bRet[0] && bRet[1] && bRet[2] ) ;
}
bool Qim::LoadFromRawFile (const string& strFileName)
{
ifstream input(strFileName.c_str()) ;
if (!input.is_open())
{
return false ;
}
string py ;
string word ;
double frequency ;
while (input >> py)
{
input >> word >> frequency ;
InsertStaticPhrase(py, word, frequency) ;
}
return true ;
}
#include <iostream>
#include <sstream>
bool Qim::LoadDynamicFromRawFile(const string& strFilename)
{
ifstream in(strFilename.c_str()) ;
if ( !in.is_open() )
{
return false ;
}
string strPhrase ;
string strLine ;
while ( in >> strPhrase )
{
string strPy ;
string strShortPy ;
string temp ;
getline( in, strLine) ;
stringstream s(strLine.c_str()) ;
while (s>>temp)
{
strPy += temp ;
strShortPy += temp[0] ;
}
InsertDynamicPhrase1(strPy, strShortPy, strPhrase, 1) ;
}
in.close() ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -