📄 mainfrm.cpp
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "PcfgParser.h"
#include "MainFrm.h"
#include "parsing.h"
#include "TestTreeDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CObArray rules, edges; // 规则、线图全局变量
extern int wordNum; // 分析句法结构的同时记录句中词数
CString ruleFileName; // 全局变量,规则库文件名
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_OpenRuleSet, OnOpenRuleSet)
ON_COMMAND(ID_ProbParsing, OnProbParsing)
ON_COMMAND(ID_ShowTree, OnShowTree)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CMDIFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CMDIFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CMDIFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::OnOpenRuleSet()
{
// TODO: Add your command handler code here
CFileDialog dlg(TRUE,"txt","*.txt",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"词性标注文件|*.pos|所有文件||");
if(dlg.DoModal()!=IDOK) {
AfxMessageBox("您没有打开规则库");
return;
}
FILE *rf;
ruleFileName = (const char *)dlg.GetPathName();
rf=fopen(ruleFileName,"rt"); //打开规则文件
if(!rf) {
AfxMessageBox("无法打开规则库文件");
return;
}
CStdioFile ruleFile(rf);
CString line;
CRule * r=NULL;
for(int i=0;i<rules.GetSize();i++)
if(rules[i]!=NULL)
delete rules[i];
rules.RemoveAll();
while(ruleFile.ReadString(line)) {
int i=line.Find(';');
if(i<0)
continue;
line=line.Left(i);
line.TrimLeft();
r=new CProbRule(line);
rules.Add(r);
}
ruleFile.Close();
}
void CMainFrame::OnProbParsing()
{
// TODO: Add your command handler code here
if (rules.GetSize()==0) {
AfxMessageBox("您没有打开规则库");
return;
}
CFileDialog dlg(TRUE,"","*.*",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"词性标注文件|*.pos|所有文件||");
if(dlg.DoModal()!=IDOK)
return;
FILE *in, *out;
in=fopen((const char *)dlg.GetPathName(),"rt");
if(!in) {
AfxMessageBox("无法打开输入文件");
return;
}
out=fopen((const char *)(dlg.GetPathName()+".pcfg"),"wt");
if(!out) {
AfxMessageBox("无法创建输出文件");
fclose(in);
return;
}
CStdioFile inFile(in),outFile(out);
CString line,curSent;
double sProb,sProbAddUp=0.0; // sProb是句子概率,sProbAddUp是全部句子概率之和
int sCnt=0; // 句子个数
while(inFile.ReadString(line)) {
line.TrimLeft();
line.TrimRight();
curSent = line;
if (line!="") {
line=ProbParsing(line,sProb);
if (sProb>0)
OnShowTree(); // 显示句法树
else
AfxMessageBox("当前句:\n"+curSent+"\n\n"+line);
sProbAddUp+=sProb;
sCnt++;
}
outFile.WriteString(line);
}
CString msg;
msg.Format("%d个句子,总概率%7.5f,平均每句概率%7.5f\n\n",sCnt,sProbAddUp,sProbAddUp/sCnt);
outFile.WriteString(msg);
inFile.Close();
outFile.Close();
GetRuleNewProb();
FILE *rf;
rf=fopen(ruleFileName+".new","wt");
if(!rf) {
AfxMessageBox("无法创建新的规则文件");
return;
}
CStdioFile newRule(rf);
for(int i=0;i<rules.GetSize();i++) {
CProbRule *r=(CProbRule *) rules[i];
msg.Format("%s->%s %s %8.6f;期望次数: %8.6f\n",r->Ls,r->Rs1,r->Rs2,r->Prob,r->DesireCount);
newRule.WriteString(msg);
}
newRule.Close();
AfxMessageBox("全部句子分析完毕,并生成新的规则文件rules.new");
}
void CMainFrame::OnShowTree()
{
// TODO: Add your command handler code here
CTestTreeDlg myDlg;
myDlg.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -