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

📄 extractsentenceview.cpp

📁 本程序用vc编写
💻 CPP
字号:
// ExtractSentenceView.cpp : implementation of the CExtractSentenceView class
//

#include "stdafx.h"
#include "ExtractSentence.h"

#include "LWORDSet.h"
#include "SentenceContainTermSet.h"
#include "ExtractSentenceDoc.h"
#include "ExtractSentenceView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define MAXLENGTH 10000

/////////////////////////////////////////////////////////////////////////////
// CExtractSentenceView

IMPLEMENT_DYNCREATE(CExtractSentenceView, CRecordView)

BEGIN_MESSAGE_MAP(CExtractSentenceView, CRecordView)
	//{{AFX_MSG_MAP(CExtractSentenceView)
	ON_BN_CLICKED(IDC_SENTENCE_TO_DB, OnSentenceToDB)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExtractSentenceView construction/destruction

CExtractSentenceView::CExtractSentenceView()
	: CRecordView(CExtractSentenceView::IDD)
{
	//{{AFX_DATA_INIT(CExtractSentenceView)
		// NOTE: the ClassWizard will add member initialization here
	m_pSet = NULL;
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	m_pSentenceSet = NULL;

}

CExtractSentenceView::~CExtractSentenceView()
{
}

void CExtractSentenceView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExtractSentenceView)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BOOL CExtractSentenceView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CRecordView::PreCreateWindow(cs);
}

void CExtractSentenceView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_lWORDSet;
	m_pSentenceSet = &GetDocument()->m_SentenceSet;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	if (m_pSet->IsOpen())
		m_pSet->Close();
	m_pSet->Open();

	if (m_pSentenceSet->IsOpen())
		m_pSentenceSet->Close();
	m_pSentenceSet->Open();
}

/////////////////////////////////////////////////////////////////////////////
// CExtractSentenceView printing

BOOL CExtractSentenceView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CExtractSentenceView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CExtractSentenceView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CExtractSentenceView diagnostics

#ifdef _DEBUG
void CExtractSentenceView::AssertValid() const
{
	CRecordView::AssertValid();
}

void CExtractSentenceView::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

CExtractSentenceDoc* CExtractSentenceView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CExtractSentenceDoc)));
	return (CExtractSentenceDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CExtractSentenceView database support
CRecordset* CExtractSentenceView::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CExtractSentenceView message handlers

void CExtractSentenceView::OnSentenceToDB() 
{
	// TODO: Add your control notification handler code here
	CExtractSentenceDoc *m_pDoc = GetDocument();
	
	WIN32_FIND_DATA ffd;
	CString fileName;
	char buffer[MAXLENGTH];
	CString term1;
	CString test;//test

	HANDLE hFind = FindFirstFile("e:\\*.txt", &ffd);
	if (hFind != INVALID_HANDLE_VALUE)
	{
		//procedure files
		try
		{
			//get the first file name
			fileName = ffd.cFileName;
			AfxMessageBox(fileName);

			//create the CStdioFile object in order to read from 
			CStdioFile txtFile;
			txtFile.Open("e:\\" + fileName, CFile::modeRead | CFile::shareDenyWrite | CFile::typeText);
			
			//create the CFile object in order to write to 
			HANDLE hFile = CreateFile("d:\\" + fileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
			if (hFile == INVALID_HANDLE_VALUE) 
			{
				AfxMessageBox(_T("couldn't create the file!"));
				return;
			}
			CFile myFile((int)hFile);

			///*open the recordset
			if (!m_pSet->IsOpen())
				m_pSet->Open();
			if (!m_pSentenceSet->IsOpen())
				m_pSentenceSet->Open();
			//*/

			//procedure the first file
			while (txtFile.ReadString(buffer, MAXLENGTH))
			{
				//init *sentence[]
				for (int i = 0; i < MAXSENTENCE; i++)
					m_pDoc->sentence[i] = NULL;

				//get sentences by spliting paragraph 
				m_pDoc->splitParagraph(buffer, m_pDoc->sentence);

				/*
				i = 0;
				while (m_pDoc->sentence[i] != NULL)
				{
					AfxMessageBox(m_pDoc->sentence[i]);
					i++;
				}
				*/

				//procedure every sentence in paragraph
				i = 0;
				while (m_pDoc->sentence[i] != NULL)
				{					
					AfxMessageBox(m_pDoc->sentence[i]);

					//find term in every sentence
					m_pSet->MoveFirst();
					while (!m_pSet->IsEOF() && !m_pDoc->searchTermsInSentence(m_pDoc->sentence[i], m_pSet->m_LWORD))
					{
						m_pSet->MoveNext();
					}
					//haven't found the first term, then get out of loop to search next sentence
					if (m_pSet->IsEOF())
					{
						i++;
						test = "一个术语都没有找到!";
						AfxMessageBox(test);
						continue;
					}
					term1 = m_pSet->m_LWORD;

					//if there is a term in the sentence[i], find next term				
					m_pSet->MoveNext();
					while (!m_pSet->IsEOF() && !m_pDoc->searchTermsInSentence(m_pDoc->sentence[i], m_pSet->m_LWORD))
					{
						m_pSet->MoveNext();
					}
					//haven't found the second term, then get out of loop to search next sentence
					if (m_pSet->IsEOF())
					{
						i++;
						test = "只找到一个术语!";
						AfxMessageBox(test);
						continue;
					}

					///*there are two terms in the sentence, so write it to a file
					myFile.Write(m_pDoc->sentence[i], strlen(m_pDoc->sentence[i]));
					myFile.Write("+++", 3);
					myFile.Write(term1, strlen(term1));
					myFile.Write("===", 3);
					myFile.Write(m_pSet->m_LWORD, strlen(m_pSet->m_LWORD));
					myFile.Write("------", 6);
					/////end write

					///*write the sentence into DB
					if (m_pSentenceSet->CanUpdate() == 0)
					{
						AfxMessageBox("cannot add new record");
					}
					else
					{
						test = "准备添加到数据库!";						
						AfxMessageBox(test);
						m_pSentenceSet->AddNew();
					
						m_pSentenceSet->m_WORD1 = term1;
						m_pSentenceSet->m_WORD2 = m_pSet->m_LWORD;
						m_pSentenceSet->m_SENTENCE = m_pDoc->sentence[i];

						m_pSentenceSet->Update();
						test = "添加成功!";
						AfxMessageBox(test);
					}	
					//*///end write to DB

					i++;
				}
			}
			myFile.Close();
			txtFile.Close();
			

			//procedure the other files
		    while (FindNextFile(hFind, &ffd))
			{	
		     	fileName = ffd.cFileName;
				//AfxMessageBox(fileName);

				//create the CStdioFile object in order to read from 
				CStdioFile txtFile;
				txtFile.Open("e:\\" + fileName, CFile::modeRead | CFile::shareDenyWrite | CFile::typeText);
			
				//create the CFile object in order to write to 
				HANDLE hFile = CreateFile("d:\\" + fileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
				if (hFile == INVALID_HANDLE_VALUE) 
				{
					AfxMessageBox(_T("couldn't create the file!"));
					return;
				}
				CFile myFile((int)hFile);

				//procedure the first file
				while (txtFile.ReadString(buffer, MAXLENGTH))
				{
					//init *sentence[]
					for (int i = 0; i < MAXSENTENCE; i++)
						m_pDoc->sentence[i] = NULL;

					//get sentences by spliting paragraph 
					m_pDoc->splitParagraph(buffer, m_pDoc->sentence);
	
					//test
					i = 0;
					while (m_pDoc->sentence[i] != NULL)
					{					
						//AfxMessageBox(sentence[i]);
						i++;
					}
					//test

					///*open the recordset
					if (!m_pSet->IsOpen())
						m_pSet->Open();
					if (!m_pSentenceSet->IsOpen())
						m_pSentenceSet->Open();
					//*/
					
					//procedure every sentence in paragraph
					i = 0;
					while (m_pDoc->sentence[i] != NULL)
					{					
						//find term in every sentence
						m_pSet->MoveFirst();
						while (!m_pSet->IsEOF() && !m_pDoc->searchTermsInSentence(m_pDoc->sentence[i], m_pSet->m_LWORD))
						{
							m_pSet->MoveNext();
						}
						//haven't found the first term, then get out of loop to search next sentence
						if (m_pSet->IsEOF())
						{
							i++;
							continue;
						}
						term1 = m_pSet->m_LWORD;

						//if there is a term in the sentence[i], find next term				
						m_pSet->MoveNext();
						while (!m_pSet->IsEOF() && !m_pDoc->searchTermsInSentence(m_pDoc->sentence[i], m_pSet->m_LWORD))
						{
							m_pSet->MoveNext();
						}
						//haven't found the second term, then get out of loop to search next sentence
						if (m_pSet->IsEOF())
						{
							i++;
							continue;
						}
	
						//write the sentence into DB
						if (m_pSentenceSet->CanUpdate() == 0)
						{
							AfxMessageBox("cannot add new record");
						}
						else
						{
							m_pSentenceSet->AddNew();
					
							m_pSentenceSet->m_WORD1 = term1;
							m_pSentenceSet->m_WORD2 = m_pSet->m_LWORD;
							m_pSentenceSet->m_SENTENCE = m_pDoc->sentence[i];

							m_pSentenceSet->Update();
						}	
						//end write to DB

					
						///*there are two terms in the sentence, so write it to a file
						myFile.Write(m_pDoc->sentence[i], strlen(m_pDoc->sentence[i]));
						myFile.Write("+++", 3);
						myFile.Write(term1, strlen(term1));
						myFile.Write("===", 3);
						myFile.Write(m_pSet->m_LWORD, strlen(m_pSet->m_LWORD));
						myFile.Write("------", 6);
						//*///end write

						i++;
					}
				}
				myFile.Close();
				txtFile.Close();
			}
		}
		catch (CFileException *e)
		{
#ifdef _DEBUG
			afxDump << "file could not be opened."
				<< e->m_cause << "\n";
#endif
		}
	}

	FindClose(hFind);
}

⌨️ 快捷键说明

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