Приложение 2. Текст программы
unit uMain;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus, OleServer, WordXP;
type TfrmMain = class(TForm) mnMain: TMainMenu; N1: TMenuItem; N2: TMenuItem; N3: TMenuItem; N4: TMenuItem; N5: TMenuItem; N6: TMenuItem; N7: TMenuItem; N8: TMenuItem; N9: TMenuItem; N10: TMenuItem; N11: TMenuItem; wa: TWordApplication; wd: TWordDocument; wf: TWordFont; N12: TMenuItem; procedure N2Click(Sender: TObject); procedure N3Click(Sender: TObject); procedure N5Click(Sender: TObject); procedure N7Click(Sender: TObject); procedure N9Click(Sender: TObject); procedure N11Click(Sender: TObject); procedure N12Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var frmMain: TfrmMain;
implementation
uses uInputModule, uClassify, uEditDictModule, uClassifySettings, uDataModule, uGetFileName, uEditPubl;
{$R *.dfm}
procedure TfrmMain.N2Click(Sender: TObject); begin frmInputForm.ShowModal; end;
procedure TfrmMain.N3Click(Sender: TObject); begin frmClassify.ShowModal; end; procedure TfrmMain.N5Click(Sender: TObject); begin
frmEditDict.ShowModal; end;
procedure TfrmMain.N7Click(Sender: TObject); begin frmClassifySettings.ShowModal; end;
procedure TfrmMain.N9Click(Sender: TObject); begin Close; end;
procedure TfrmMain.N11Click(Sender: TObject); var tmpl, Template, NewTemplate, ItemIndex: olevariant; ARange: Range; pars: Paragraphs; par: Paragraph; st: string; vcol: OleVariant; iStat: integer; begin dmIAS.aqFiles.Close; dmIAS.aqFiles.Open; frmGetFileName.cbFiles.Items.Clear; frmGetFileName.cbFiles.Text:= dmIAS.aqFiles.FieldByName('file').AsString; while not dmIAS.aqFiles.Eof do begin frmGetFileName.cbFiles.Items.Add(dmIAS.aqFiles.FieldByName('file').AsString); dmIAS.aqFiles.Next; end;
frmGetFileName.iMD:= 0; frmGetFileName.ShowModal; if (frmGetFileName.iMD = 0) or (frmGetFileName.cbFiles.Text = '') then exit; dmIAS.aqExe.Close; dmIAS.aqExe.SQL.Text:= 'Select * from qObrPubl where file =:file'; dmIAS.aqExe.Parameters.ParamByName('file').Value:= frmGetFileName.cbFiles.Text; dmIAS.aqExe.Open;
try wa.Connect; wa.Visible:= True; except MessageDlg('А у Вас Word не установлен:(', mtError, [mbOk], 0); Abort; end; ItemIndex:= 1; Template:= EmptyParam; NewTemplate:= False; // Создание документа wa.Documents.Add(Template, NewTemplate, EmptyParam, EmptyParam); wd.ConnectTo(wa.Documents.Item(ItemIndex)); wd.PageSetup.Set_Orientation(wdOrientLandscape); wa.Options.CheckSpellingAsYouType:= False;
wa.Options.CheckGrammarAsYouType:= False; ARange:= wd.Range(EmptyParam, EmptyParam); pars:= wd.Paragraphs; tmpl:= ARange; par:= pars.Add(tmpl); wf.ConnectTo(wd.Sentences.Get_Last.Font);
wd.Range.Paragraphs.Set_Alignment(wdAlignParagraphLeft); wa.Selection.Font.Bold:= 1; wa.Selection.Font.Size:= 16; st:= 'Отчет по обработанным статьям файла ' + frmGetFileName.cbFiles.Text; wa.Selection.InsertAfter(st+#13); wa.Selection.InsertAfter(' '+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
iStat:= -1; while not dmIAS.aqExe.Eof do begin if iStat <> dmIAS.aqExe.FieldByName('id_publ').AsInteger then begin vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); wa.Selection.Font.Bold:= 0; wa.Selection.Font.Size:= 14; wa.Selection.InsertAfter(' '+#13); wa.Selection.InsertAfter(' '+#13); st:= 'Дата публикации: '; wa.Selection.InsertAfter(st); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); if (dmIAS.aqExe.FieldByName('data').AsString = '30.12.1899') then st:= ' ' else st:= dmIAS.aqExe.FieldByName('data').AsString; wa.Selection.InsertAfter(st+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
st:= 'Название публикации: '; wa.Selection.InsertAfter(st); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); st:= dmIAS.aqExe.FieldByName('name_publ').AsString; wa.Selection.InsertAfter(st+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
st:= 'Ключевые слова: '; wa.Selection.InsertAfter(st); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); st:= dmIAS.aqExe.FieldByName('keywords').AsString; wa.Selection.InsertAfter(st+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
st:= 'Автор публикации: '; wa.Selection.InsertAfter(st); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); st:= dmIAS.aqExe.FieldByName('author').AsString; wa.Selection.InsertAfter(st+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
st:= 'Текст публикации: '; wa.Selection.InsertAfter(st); vcol:= wdCollapseEnd;
wa.Selection.Collapse(vcol); st:= dmIAS.aqExe.FieldByName('text_publ').AsString; wa.Selection.InsertAfter(st+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
st:= 'Сопоставленные рубрики:'; wa.Selection.InsertAfter(st+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
st:= dmIAS.aqExe.FieldByName('indx').AsString + ' ' + dmIAS.aqExe.FieldByName('name_r').AsString; wa.Selection.InsertAfter(st); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); iStat:= dmIAS.aqExe.FieldByName('id_publ').AsInteger; end else begin wa.Selection.InsertAfter(' '+#13); st:= dmIAS.aqExe.FieldByName('indx').AsString + ' ' + dmIAS.aqExe.FieldByName('name_r').AsString; wa.Selection.InsertAfter(st); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); end; dmIAS.aqExe.Next; end; wa.Disconnect; end;
procedure TfrmMain.N12Click(Sender: TObject);
begin frmEditPubl.ShowModal; end; end.
unit uFuzzySearch;
interface
function IndistinctMatching(MaxMatching:Integer; strInputMatching:WideString; strInputStandart:WideString):Integer;
implementation
uses SysUtils;
type TRetCount = packed record lngSubRows: Word; lngCountLike: Word; end;
function Matching(StrInputA:WideString; StrInputB:WideString; lngLen:Integer):TRetCount; Var TempRet: TRetCount; PosStrB: Integer; PosStrA: Integer;
StrA: WideString; StrB: WideString; StrTempA: WideString; StrTempB: WideString; begin StrA:= String(StrInputA); StrB:= String(StrInputB);
For PosStrA:= 1 To Length(strA) - lngLen + 1 do begin StrTempA:= System.Copy(strA, PosStrA, lngLen);
For PosStrB:= 1 To Length(strB) - lngLen + 1 do begin StrTempB:= System.Copy(strB, PosStrB, lngLen); If SysUtils.AnsiCompareText(StrTempA,StrTempB) = 0 Then begin Inc(TempRet.lngCountLike); break; end; end;
Inc(TempRet.lngSubRows); end; // PosStrA
Matching.lngCountLike:= TempRet.lngCountLike; Matching.lngSubRows:= TempRet.lngSubRows; end; { function }
//------------------------------------------------------------------------------ function IndistinctMatching(MaxMatching:Integer; strInputMatching:WideString; strInputStandart:WideString):Integer; Var gret: TRetCount; tret: TRetCount; lngCurLen: Integer; //текущая длина подстроки begin //если не передан какой-либо параметр, то выход If (MaxMatching = 0) Or (Length(strInputMatching) = 0) Or (Length(strInputStandart) = 0) Then begin IndistinctMatching:= 0; exit; end;
gret.lngCountLike:= 0; gret.lngSubRows:= 0; // Цикл прохода по длине сравниваемой фразы For lngCurLen:= 1 To MaxMatching do begin //Сравниваем строку A со строкой B tret:= Matching(strInputMatching, strInputStandart, lngCurLen); gret.lngCountLike:= gret.lngCountLike + tret.lngCountLike; gret.lngSubRows:= gret.lngSubRows + tret.lngSubRows; //Сравниваем строку B со строкой A //tret:= Matching(strInputStandart, strInputMatching, lngCurLen); //gret.lngCountLike:= gret.lngCountLike + tret.lngCountLike; //gret.lngSubRows:= gret.lngSubRows + tret.lngSubRows; end;
If gret.lngSubRows = 0 Then begin
IndistinctMatching:= 0; exit; end;
IndistinctMatching:= Trunc((gret.lngCountLike / gret.lngSubRows) * 100); end; end.
unit uClassify;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Mask, DBCtrls, Grids, DBGridEh, ComCtrls, WordXP, OleServer;
type TfrmClassify = class(TForm) Panel1: TPanel; Panel2: TPanel; Label1: TLabel; DBNavigator1: TDBNavigator; dbmText: TDBMemo; Panel3: TPanel; Label7: TLabel; Button2: TButton; Panel4: TPanel; Label6: TLabel; Label3: TLabel; dbAuthor: TDBEdit; Label2: TLabel; dbDate: TDBEdit; Label4: TLabel; dbName: TDBEdit; Label5: TLabel; dbKeywords: TDBEdit; pbClassify: TProgressBar; dbgClassify: TDBGridEh; Button1: TButton; Label8: TLabel; dbFile: TDBEdit; lblCountArticles: TLabel; Button3: TButton; wd: TWordDocument; wa: TWordApplication; wf: TWordFont; procedure SetDBElemColor(flColor: boolean); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormShow(Sender: TObject); procedure Button3Click(Sender: TObject);
private { Private declarations } public { Public declarations } end;
var frmClassify: TfrmClassify;
implementation
uses uDataModule, uFuzzySearch, uClassifySettings, uStructs;
{$R *.dfm}
procedure TfrmClassify.SetDBElemColor(flColor: boolean); begin if frmClassify = nil then exit; if flColor then begin dbName.Color:= clMoneyGreen; dbKeywords.Color:= clMoneyGreen; dbmText.Color:= clMoneyGreen; end else begin dbName.Color:= clWindow; dbKeywords.Color:= clWindow; dbmText.Color:= clWindow; end; end;
procedure TfrmClassify.Button1Click(Sender: TObject); var id_publ, id_rubr, level, Name_cnt, Text_cnt, Key_cnt, im, iNamePorog12, iTextPorog12, iKeyPorog12, iNamePorog23, iTextPorog23, iKeyPorog23, iFuzzyPorog: integer; stWord, res: string;
function GetRes(NameCnt, TextCnt, KeyCnt: integer): String; begin if (NameCnt < iNamePorog12) and (TextCnt < iTextPorog12) and (KeyCnt < iKeyPorog12) then result:= '-' else if (NameCnt >= iNamePorog23) or (TextCnt >= iTextPorog23) or (KeyCnt >= iKeyPorog23) then result:= '+' else result:= '?'; end;
begin // Устанавливаем пороги срабатывания iNamePorog12:= frmClassifySettings.GetNamePorog(12); iTextPorog12:= frmClassifySettings.GetTextPorog(12); iKeyPorog12:= frmClassifySettings.GetKeyPorog(12); iNamePorog23:= frmClassifySettings.GetNamePorog(23); iTextPorog23:= frmClassifySettings.GetTextPorog(23); iKeyPorog23:= frmClassifySettings.GetKeyPorog(23); iFuzzyPorog:= frmClassifySettings.GetFuzzyPorog; // Начинаем классификацию dmIAS.aqDict.Close; dmIAS.aqDict.Open; dmIAS.TruncClassify; pbClassify.Position:= 0; pbClassify.Max:= dmIAS.atPublikatsii.RecordCount * (dmIAS.aqDict.RecordCount+1);
with dmIAS.atPublikatsii do begin First; while not Eof do begin id_publ:= FieldByName('id_publ').AsInteger; Name_cnt:= 0; Text_cnt:= 0; Key_cnt:= 0; dmIAS.aqDict.First; id_rubr:= dmIAS.aqDict.FieldByName('id').AsInteger; level:= dmIAS.aqDict.FieldByName('level').AsInteger; while not dmIAS.aqDict.Eof do begin // Классифицируем по словарю для каждой рубрики if id_rubr <> dmIAS.aqDict.FieldByName('id').AsInteger then begin res:= GetRes(Name_cnt, Text_cnt, Key_cnt); dmIAS.InsertClassify(id_publ, id_rubr, level, Name_cnt, Text_cnt, Key_cnt, res); id_rubr:= dmIAS.aqDict.FieldByName('id').AsInteger; level:= dmIAS.aqDict.FieldByName('level').AsInteger; Name_cnt:= 0; Text_cnt:= 0; Key_cnt:= 0; end; stWord:= AnsiUpperCase(dmIAS.aqDict.FieldByName('Word').AsString); // Классификация по наименованию im:= IndistinctMatching(length(stWord), stWord, AnsiUpperCase(FieldByName('name_publ').AsString)); if im > iFuzzyPorog then Inc(Name_cnt); // Классификация по тексту im:= IndistinctMatching(length(stWord), stWord, AnsiUpperCase(FieldByName('text_publ').AsString)); if im > iFuzzyPorog then Inc(Text_cnt); // Классификация по ключевым словам im:= IndistinctMatching(length(stWord), stWord, AnsiUpperCase(FieldByName('keywords').AsString)); if im > iFuzzyPorog then Inc(Key_cnt); dmIAS.aqDict.Next; pbClassify.StepIt; Application.ProcessMessages;
end; res:= GetRes(Name_cnt, Text_cnt, Key_cnt); dmIAS.InsertClassify(id_publ, id_rubr, level, Name_cnt, Text_cnt, Key_cnt, res); Next; pbClassify.StepIt; Application.ProcessMessages; end; end; dmIAS.aqClassify.Close; dmIAS.atPublikatsii.First; dmIAS.aqClassify.Open; Application.ProcessMessages;
ShowMessage('Классификация успешно завершена.'); end;
procedure TfrmClassify.Button2Click(Sender: TObject); var i, i_rubr, level: integer; begin if dbgClassify.SelectedRows.Count = 0 then begin ShowMessage('Не выбрано ни одной записи!'); exit; end; // Удалить все записи из Publ_Rubr для данной статьи dmIAS.DeleteFromPublRubr(dmIAS.atPublikatsii.FieldByName('id_publ').AsInteger); // Записать в Publ_Rubr все выбранные рубрики для данной статьи for i:= 0 to dbgClassify.SelectedRows.Count-1 do begin dbgClassify.DataSource.DataSet.GotoBookmark(Pointer(dbgClassify.SelectedRows.Items[i])); i_rubr:= dbgClassify.DataSource.DataSet.FieldByName('id').AsInteger; level:= dbgClassify.DataSource.DataSet.FieldByName('level_r').AsInteger; dmIAS.InsertIntoPublRubr(dmIAS.atPublikatsii.FieldByName('id_publ').AsInteger, i_rubr, level); end; dbgClassify.DataSource.DataSet.GotoBookmark(Pointer(dbgClassify.SelectedRows.Items[0])); ShowMessage('Соответствующие статье рубрики сохранены.');
dmIAS.atObrPublikatsii.Close; dmIAS.atObrPublikatsii.Open; dmIAS.atCountObrPublikatsii.Close; dmIAS.atCountObrPublikatsii.Open;
lblCountArticles.Caption:= 'Всего в базе: ' + IntToStr(dmIAS.atPublikatsii.RecordCount) + ' статей. ' + ' Классифицировано ' + IntToStr(dmIAS.atCountObrPublikatsii.RecordCount) + ' статей.'; end;
procedure TfrmClassify.FormCreate(Sender: TObject); begin frmClassifySettings.SetNamePorog(2, 12); frmClassifySettings.SetTextPorog(3, 12); frmClassifySettings.SetKeyPorog(2, 12); frmClassifySettings.SetNamePorog(4, 23); frmClassifySettings.SetTextPorog(6, 23); frmClassifySettings.SetKeyPorog(4, 23); frmClassifySettings.SetFuzzyPorog(50); frmClassifySettings.SetflShowAll(true); end;
procedure TfrmClassify.FormShow(Sender: TObject); begin { if frmClassifySettings.GetflShowAll then // Показывать все статьи with dmIAS.atPublikatsii do begin Close; SQL.Text:= stSelectAllPubl; Open; end
else // Показывать необработанные статьи with dmIAS.atPublikatsii do begin Close; SQL.Text:= stSelectNeobrPubl; Open; end;} lblCountArticles.Caption:= 'Всего в базе: ' + IntToStr(dmIAS.atPublikatsii.RecordCount) + ' статей. ' + ' Классифицировано ' + IntToStr(dmIAS.atCountObrPublikatsii.RecordCount) + ' статей.'; end;
procedure TfrmClassify.Button3Click(Sender: TObject); var tmpl, Template, NewTemplate, ItemIndex: olevariant; ARange: Range; pars: Paragraphs; par: Paragraph; tbls: Tables; tbl1: Table; st: string; vcol: OleVariant; i: integer; begin try wa.Connect; wa.Visible:= True; except MessageDlg('А у Вас Word не установлен:(', mtError, [mbOk], 0); Abort; end; ItemIndex:= 1; Template:= EmptyParam; NewTemplate:= False; // Создание документа wa.Documents.Add(Template, NewTemplate, EmptyParam, EmptyParam); wd.ConnectTo(wa.Documents.Item(ItemIndex)); wd.PageSetup.Set_Orientation(wdOrientLandscape); wa.Options.CheckSpellingAsYouType:= False; wa.Options.CheckGrammarAsYouType:= False; ARange:= wd.Range(EmptyParam, EmptyParam); pars:= wd.Paragraphs; tmpl:= ARange; par:= pars.Add(tmpl); wf.ConnectTo(wd.Sentences.Get_Last.Font); wd.Range.Paragraphs.Set_Alignment(wdAlignParagraphLeft); wa.Selection.Font.Bold:= 1; wa.Selection.Font.Size:= 16; st:= 'Отчет по результатам классификации статьи '; wa.Selection.InsertAfter(st+#13); wa.Selection.InsertAfter(' '+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
wa.Selection.Font.Bold:= 0; wa.Selection.Font.Size:= 14; st:= 'Дата публикации: '; wa.Selection.InsertAfter(st);
vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
if (dmIAS.atPublikatsii.FieldByName('data').AsString = '30.12.1899') then st:= ' ' else st:= dmIAS.atPublikatsii.FieldByName('data').AsString; wa.Selection.InsertAfter(st+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
st:= 'Файл публикации: '; wa.Selection.InsertAfter(st); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); st:= dmIAS.atPublikatsii.FieldByName('file').AsString; wa.Selection.InsertAfter(st+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
st:= 'Название публикации: '; wa.Selection.InsertAfter(st); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); st:= dmIAS.atPublikatsii.FieldByName('name_publ').AsString; wa.Selection.InsertAfter(st+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
st:= 'Ключевые слова: '; wa.Selection.InsertAfter(st); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); st:= dmIAS.atPublikatsii.FieldByName('keywords').AsString; wa.Selection.InsertAfter(st+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
st:= 'Автор публикации: '; wa.Selection.InsertAfter(st); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); st:= dmIAS.atPublikatsii.FieldByName('author').AsString; wa.Selection.InsertAfter(st+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
st:= 'Текст публикации: '; wa.Selection.InsertAfter(st); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); st:= dmIAS.atPublikatsii.FieldByName('text_publ').AsString; wa.Selection.InsertAfter(st+#13); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); wa.Selection.InsertAfter(' '+#13); st:= 'Результаты классификации:'; wa.Selection.InsertAfter(st); vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol);
ARange:= wd.Sentences.Last; tbls:= ARange.Tables; tbl1:= tbls.Add(ARange, dmIAS.aqClassify.RecordCount+1, // число строк 6, // число столбцов
EmptyParam, EmptyParam); tbl1.Cell(1, 1).Range.Text:= 'Код'; tbl1.Cell(1, 2).Range.Text:= 'Наименование'; tbl1.Cell(1, 3).Range.Text:= 'По назв.'; tbl1.Cell(1, 4).Range.Text:= 'По тексту'; tbl1.Cell(1, 5).Range.Text:= 'По кл.сл.'; tbl1.Cell(1, 6).Range.Text:= 'Результат';
i:= 2; dmIAS.aqClassify.First; while not dmIAS.aqClassify.Eof do begin tbl1.Cell(i, 1).Range.Text:= dmIAS.aqClassify.FieldByName('indx').AsString; tbl1.Cell(i, 2).Range.Text:= dmIAS.aqClassify.FieldByName('name_r').AsString; tbl1.Cell(i, 3).Range.Text:= dmIAS.aqClassify.FieldByName('name_cnt').AsString; tbl1.Cell(i, 4).Range.Text:= dmIAS.aqClassify.FieldByName('text_cnt').AsString; tbl1.Cell(i, 5).Range.Text:= dmIAS.aqClassify.FieldByName('key_cnt').AsString; tbl1.Cell(i, 6).Range.Text:= dmIAS.aqClassify.FieldByName('res').AsString; dmIAS.aqClassify.Next; Inc(i); end;
vcol:= wdCollapseEnd; wa.Selection.Collapse(vcol); wa.Disconnect; dmIAS.aqClassify.First; end;
end.
Воспользуйтесь поиском по сайту: ©2015 - 2024 megalektsii.ru Все авторские права принадлежат авторам лекционных материалов. Обратная связь с нами...
|