📄 orgtagdlg.cpp
字号:
// OrgTagDlg.cpp : implementation file
//
#include "stdafx.h"
#include "OrgTag.h"
#include "OrgTagDlg.h"
#include "ICTCLAS.h"
#include "PreProcess.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COrgTagDlg dialog
COrgTagDlg::COrgTagDlg(CWnd* pParent /*=NULL*/)
: CDialog(COrgTagDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(COrgTagDlg)
m_label = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void COrgTagDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(COrgTagDlg)
DDX_Control(pDX, IDC_RICHEDIT_TEXT, m_edit);
DDX_Control(pDX, IDC_PROGRESS_TAG, m_progress);
DDX_Text(pDX, IDC_STATIC1, m_label);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(COrgTagDlg, CDialog)
//{{AFX_MSG_MAP(COrgTagDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_IMPORT, OnButtonImport)
ON_BN_CLICKED(IDC_BUTTON_TAG, OnButtonTag)
ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
ON_COMMAND(ID_MENUITEM_ABOUT, OnMenuitemAbout)
ON_COMMAND(ID_MENUITEM_ANNOTATOR, OnMenuitemAnnotator)
ON_COMMAND(ID_MENUITEM_TRAIN, OnMenuitemTrain)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COrgTagDlg message handlers
BOOL COrgTagDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_operation = ANNOTATOR;
m_nList = 0;
m_senList = new CSentenceList[10];
m_annotator = new CAnnotator(&m_progress);
return TRUE; // return TRUE unless you set the focus to a control
}
void COrgTagDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void COrgTagDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR COrgTagDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void COrgTagDlg::OnButtonImport()
{
// TODO: Add your control notification handler code here
m_edit.SetSel(0, -1);
m_edit.ReplaceSel("");
m_strInput = "";
CFileDialog fileDialog(TRUE, "*.txt", "",
OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT,
"Text Files(*.txt)|*.txt|", NULL);
// if(m_operation == TRAIN)
// fileDialog.m_ofn.Flags |= OFN_EXPLORER|OFN_ALLOWMULTISELECT;
m_nList = 0;
POSITION pos;
if(fileDialog.DoModal() == IDOK )
{
pos = fileDialog.GetStartPosition();
while(pos)
{
m_fileName = fileDialog.GetNextPathName(pos);
if(m_operation == TRAIN)
{
FileToSenList(m_senList[m_nList], m_fileName);
m_strInput += SenListToString(m_senList[m_nList++]) + "\r\n\r\n";
}
else
{
/* CFile file;
if(!file.Open(m_fileName,CFile::modeRead))
{
AfxMessageBox("Fail to open the file!");
return;
}
BYTE buffer[4096];
DWORD dwRead;
while(dwRead = file.Read(buffer, 4096))
for(unsigned int i=0; i<dwRead; i++)
m_strInput += buffer[i];
file.Close();*/
char* oldFile = new char[strlen(m_fileName)];
char* newFile = new char[strlen(m_fileName) + 4];
sprintf(oldFile, "%s", m_fileName);
CString m_newFileName = m_fileName;
m_newFileName.Replace(".txt", "_org.txt");
sprintf(newFile, "%s", m_newFileName);
CFileFind find;
if (!find.FindFile(newFile))
{
ICTCLAS_Init();
ICTCLAS_FileProcess(oldFile, newFile);
ICTCLAS_Exit();
}
FileToSenList(m_senList[0], m_newFileName);
m_strInput += SenListToString(m_senList[0]);
m_nList ++ ;
}
m_edit.SetWindowText(m_strInput);
}
if(m_operation == TRAIN)
this->DisplayResult(m_senList);
}
}
void COrgTagDlg::OnButtonTag()
{
// TODO: Add your control notification handler code here
CSentenceList senList;
m_progress.SetPos(0);
if(m_operation == ANNOTATOR)
{
// m_label = "分词中……";
// UpdateData(false);
m_label = "标注中……";
UpdateData(false);
// annotate
// waiting to be modified by Xu-YuShi
// m_senList[0] := CSentenceList
// &m_progress := pointer to CProgressCtrl
//CAnnotator anno(&m_progress);
m_senList[0] = m_annotator->Annotate(m_senList[0]);
m_label = "更新显示中……";
UpdateData(false);
this->DisplayResult(m_senList);
m_label = "完成";
UpdateData(false);
}
else // TRAIN
{
// waiting to be modifid by Xu-YuShi
// m_senList[cnt] := CSentenceList
// &m_progress := pointer to CProgressCtrl
CAnnotator anno(&m_progress);
for(int cnt = 0; cnt < m_nList; cnt ++ )
{
anno.Train(m_senList[cnt]);
}
}
}
void COrgTagDlg::OnButtonClose()
{
// TODO: Add your control notification handler code here
CDialog::OnCancel();
}
void COrgTagDlg::OnMenuitemAbout()
{
// TODO: Add your command handler code here
CAboutDlg* about = new CAboutDlg();
about->DoModal();
}
void COrgTagDlg::OnMenuitemAnnotator()
{
// TODO: Add your command handler code here
m_operation = ANNOTATOR;
CheckMenuItem(this->GetMenu()->m_hMenu, ID_MENUITEM_TRAIN, MF_UNCHECKED);
CheckMenuItem(this->GetMenu()->m_hMenu, ID_MENUITEM_ANNOTATOR, MF_CHECKED);
((CButton*)GetDlgItem(IDC_BUTTON_TAG))->SetWindowText("Tag");
}
void COrgTagDlg::OnMenuitemTrain()
{
// TODO: Add your command handler code here
m_operation = TRAIN;
CheckMenuItem(this->GetMenu()->m_hMenu, ID_MENUITEM_ANNOTATOR, MF_UNCHECKED);
CheckMenuItem(this->GetMenu()->m_hMenu, ID_MENUITEM_TRAIN, MF_CHECKED);
((CButton*)GetDlgItem(IDC_BUTTON_TAG))->SetWindowText("Train");
}
void COrgTagDlg::DisplayResult(CSentenceList* pSenList)
{
CHARFORMAT2 cf;
m_edit.GetSelectionCharFormat(cf);
cf.cbSize=sizeof(cf);
cf.dwMask=CFM_BACKCOLOR;
cf.crBackColor = 255;
cf.dwEffects = 0;
POSITION p, p2;
CSentence sentence;
CWord word;
int cnt = 0;
for(int i = 0; i < m_nList; i++)
{
p = pSenList[i].GetHeadPosition();
for(int k = 0; k < pSenList[i].GetCount(); k++)
{
sentence.RemoveAll();
sentence = pSenList[i].GetNext(p);
p2 = sentence.GetHeadPosition();
for(int k2 = 0; k2 < sentence.GetCount(); k2++)
{
word = sentence.GetNext(p2);
int temp = strlen(word.m_word);
cnt += temp;
if(!word.m_ne)
{
m_edit.SetSel(cnt - temp, cnt);
m_edit.SendMessage(EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&cf);
}
}
}
cnt += 4;
}
}
//**************************************************************
CODE PosTranlate(CString pos)
{
if(pos == "tt") return tt;
else if(pos == "w") return w;
else if(pos == "Ag") return Ag;
else if(pos == "a") return a;
else if(pos == "ad") return ad;
else if(pos == "an") return an;
else if(pos == "b") return b;
else if(pos == "c") return c;
else if(pos == "Dg") return Dg;
else if(pos == "d") return d;
else if(pos == "e") return e;
else if(pos == "f") return f;
else if(pos == "g") return g;
else if(pos == "h") return h;
else if(pos == "i") return i;
else if(pos == "j") return j;
else if(pos == "k") return k;
else if(pos == "l") return l;
else if(pos == "m") return m;
else if(pos == "Ng") return Ng;
else if(pos == "n") return n;
else if(pos == "nr") return nr;
else if(pos == "ns") return ns;
else if(pos == "nt") return nt;
else if(pos == "nz") return nz;
else if(pos == "o") return o;
else if(pos == "p") return p;
else if(pos == "q") return q;
else if(pos == "r") return r;
else if(pos == "s") return s;
else if(pos == "Tg") return Tg;
else if(pos == "t") return t;
else if(pos == "u") return u;
else if(pos == "Vg") return Vg;
else if(pos == "v") return v;
else if(pos == "vd") return vd;
else if(pos == "vn") return vn;
else if(pos == "x") return x;
else if(pos == "y") return y;
else return z;
}
//**************************************************************
void FileToSenList(CSentenceList& senList, CString fileName)
{
CFile file;
file.Open(fileName, CFile::modeRead);
char pbuf[1024];
UINT nBytesRead;
CString str = "";
while(nBytesRead = file.Read( pbuf, 1024 ))
{
for(unsigned int i=0; i<nBytesRead; i++)
str += pbuf[i];
}
file.Close();
int length = strlen(str);
char* input = new char[length];
sprintf(input, "%s", str);
senList.RemoveAll();
CWord word;
CSentence sentence;
char* seps = "/";
int cnt = 0;
bool flag = false;
char* w_word;
char* w_pos;
char* temp = strtok(input, seps);
while(temp!=NULL)
{
if(cnt == 0)
{
cnt = strlen(temp);
w_word = new char[cnt+1];
w_word = temp;
w_word[cnt] = '\0';
}
else
{
cnt = strlen(temp);
flag = false;
int j = 0;
while((temp[j] >= 'A' && temp[j] <= 'Z')
|| (temp[j] >= 'a' && temp[j] <= 'z'))
j++;
w_pos = new char[j];
w_pos = temp;
w_pos[j] = '\0';
word.m_ne = NONNE;
if(j == 3)
{
if(w_pos[1] == 'y') w_pos[1] = '\0';
else if(w_pos[2] == 'y') w_pos[2] = '\0';
word.m_ne = ORG;
}
word.m_word.Format("%s", w_word);
word.m_pos = PosTranlate(w_pos);
sentence.AddTail(word);
if(word.m_pos == w)
{
senList.AddTail(sentence);
sentence.RemoveAll();
}
j+=2;
int t1 = 0;
int t2 = 0;
if(j<cnt) w_word = new char[cnt-j+1];
while(j<cnt)
{
while(temp[j]==13 || temp[j]==10)
{
w_word[t1++] = temp[j++];
flag = true;
}
if(flag)
{
if(temp[j] == ' ')
while(temp[j] == ' ')
w_word[t1++] = temp[j++];
else if(temp[j]!=-95)
flag = false;
}
if(flag)
{
w_word[t1] = '\0';
w_pos = "tt";
word.m_word.Format("%s", w_word);
word.m_pos = PosTranlate(w_pos);
word.m_ne = NONNE;
sentence.AddTail(word);
flag = false;
}
w_word[t2++] = temp[j++];
}
w_word[t2] = '\0';
}
temp = strtok(NULL, seps);
}
}
//**************************************************************
CString SenListToString(CSentenceList senList)
{
CString output;
POSITION p, p2;
p = senList.GetHeadPosition();
CSentence sentence;
CWord word;
for(int k = 0; k < senList.GetCount(); k++)
{
sentence.RemoveAll();
sentence = senList.GetNext(p);
p2 = sentence.GetHeadPosition();
for(int k2 = 0; k2 < sentence.GetCount(); k2++)
{
word = sentence.GetNext(p2);
int temp = strlen(word.m_word);
for(int k3 = 0; k3 < temp; k3++)
output += word.m_word[k3];
}
}
return output;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -