Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit MainForm;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, System.Actions,
- Vcl.ActnList, Vcl.WinXCalendars, Vcl.WinXCtrls, Vcl.ExtCtrls, Vcl.Grids,
- Vcl.CheckLst, UnitTranslateFromRussian, UnitTranslateFromEnglish,
- UnitEditDictionary, Vcl.ValEdit, UnitFavorites, UnitFlashCards,
- Vcl.Imaging.pngimage;
- const
- FILE_NAME = 'Dictionary.rcd';
- FILE_FAVORITES = 'Fav.rcd';
- TEMP_FILE_NAME = 'Temp.rcd';
- FILE_FLASHCARDS = 'FlashCards.rcd';
- type
- TPartsOfSpeech = (TNoun, TVerb, TAdjective);
- EnglStr = String[91];
- RussStr = String[51];
- TWord = record
- Russian: RussStr;
- English: EnglStr;
- PartOfSpeech: TPartsOfSpeech;
- end;
- TPElem = ^TElem;
- TElem = record
- StartPage: EnglStr;
- FlipPage: EnglStr;
- Next: TPElem;
- Prev: TPElem;
- end;
- TFormMenu = class(TForm)
- PanelTabs: TPanel;
- PanelWelcome: TPanel;
- LabelWelcome: TLabel;
- PanelFrame: TPanel;
- PanelDictionaries: TPanel;
- LabelDictionary: TLabel;
- ButtonTranslateFromRussian: TButton;
- ButtonTranslateFromEnglish: TButton;
- Panel1: TPanel;
- PanelTools: TPanel;
- LabelTools: TLabel;
- ButtonFlashCards: TButton;
- ButtonChangeDictionary: TButton;
- PanelTopBorderOfFrame: TPanel;
- PanelBottomBorderOfFrame: TPanel;
- PanelLeftBorderOfFrame: TPanel;
- PanelRightBorderOfFrame: TPanel;
- ButtonFavorites: TButton;
- FrameTranslateFromRussian: TFrameTranslateFromRussian;
- FrameTranslateFromEnglish: TFrameTranslateFromEnglish;
- FrameEditDictionary: TFrameEditDictionary;
- FrameFavorites: TFrameFavorites;
- FrameFlashCards: TFrameFlashCards;
- Image1: TImage;
- procedure ButtonTranslateFromRussianClick(Sender: TObject);
- procedure ButtonTranslateFromEnglishClick(Sender: TObject);
- procedure ButtonFlashCardsClick(Sender: TObject);
- procedure ButtonChangeDictionaryClick(Sender: TObject);
- procedure ButtonFavoritesClick(Sender: TObject);
- private
- { Private declarations }
- public
- procedure WriteToFavorites(NumbRow: Integer; Dictionary: TValueListEditor; Path: String);
- procedure ClearDictionary(var Dict: TValueListEditor; Count: Integer);
- function ChooseArticle(Word: String): String;
- function GetFinalAppearance(Part: TPartsOfSpeech; Word: String): String;
- function IsRowEmpty(Dictionary: TValueListEditor; NumbRow: Integer): Boolean;
- procedure DeleteWord(var Dictionary: TValueListEditor);
- procedure SaveDictionaryToFile(Dictionary: TValueListEditor; Path: String);
- function RecognizePartOfSpeech(var ResearchWord: EnglStr): TPartsOfSpeech;
- function Search(Dictionary: TValueListEditor; var NewDictionary: TValueListEditor; SoughtFor: String): Boolean;
- function IsWordInDictionary(Dictionary: TValueListEditor; SearchStr: String): Boolean;
- function GetPartOfSpeech(var EnglishWord: EnglStr; var RadGroup: TRadioGroup; IsForAdding: Boolean): TPartsOfSpeech;
- end;
- var
- FormMenu: TFormMenu;
- implementation
- {$R *.dfm}
- uses UnitError;
- function TFormMenu.GetPartOfSpeech(var EnglishWord: EnglStr; var RadGroup: TRadioGroup; IsForAdding: Boolean): TPartsOfSpeech;
- var
- Part: TPartsOfSpeech;
- begin
- if (IsForAdding) then
- begin
- case RadGroup.ItemIndex of
- 0:
- Part := TVerb;
- 1:
- Part := TNoun;
- 2:
- Part := TAdjective;
- end;
- end
- else
- begin
- Part := MainForm.FormMenu.RecognizePartOfSpeech(EnglishWord);
- case (Part) of
- TNoun:
- RadGroup.ItemIndex := 1;
- TVerb:
- RadGroup.ItemIndex := 0;
- TAdjective:
- RadGroup.ItemIndex := 2;
- end;
- end;
- GetPartOfSpeech := Part;
- end;
- procedure TFormMenu.DeleteWord(var Dictionary: TValueListEditor);
- var
- CurrRow: Integer;
- begin
- CurrRow := Dictionary.Row;
- Dictionary.Row := 1;
- Dictionary.DeleteRow(CurrRow);
- end;
- function TFormMenu.IsRowEmpty(Dictionary: TValueListEditor; NumbRow: Integer): Boolean;
- begin
- if (Dictionary.Keys[NumbRow] <> '') and (Dictionary.Cells[1, NumbRow] <> '') then
- begin
- IsRowEmpty := false;
- end
- else
- begin
- IsRowEmpty := true;
- end;
- end;
- function TFormMenu.ChooseArticle(Word: String): String;
- begin
- if Word[1] in ['a', 'e', 'u', 'i', 'o'] then
- begin
- ChooseArticle := 'an';
- end
- else
- begin
- ChooseArticle := 'a';
- end;
- end;
- function TFormMenu.GetFinalAppearance(Part: TPartsOfSpeech; Word: String): String;
- var
- Answer: String;
- begin
- case Part of
- TNoun:
- Answer := ChooseArticle(Word) + ' ' + Word;
- TVerb:
- Answer := 'to' + ' ' + Word;
- TAdjective:
- Answer := Word;
- end;
- GetFinalAppearance := Answer;
- end;
- procedure TFormMenu.ClearDictionary(var Dict: TValueListEditor; Count: Integer);
- var
- I: Integer;
- begin
- if (Dict.RowCount > 1) then
- begin
- if not(IsRowEmpty(Dict, (Dict.RowCount - 1))) then
- begin
- Dict.Row := 1;
- for I := Count downto 1 do
- begin
- Dict.DeleteRow(I);
- end;
- end;
- end;
- end;
- procedure TFormMenu.WriteToFavorites(NumbRow: Integer; Dictionary: TValueListEditor; Path: String);
- var
- F: File of TWord;
- AddedWord: TWord;
- EnglishWord: EnglStr;
- begin
- AddedWord.Russian := Dictionary.Keys[NumbRow];
- EnglishWord := Dictionary.Cells[1, NumbRow];
- AddedWord.PartOfSpeech := MainForm.FormMenu.RecognizePartOfSpeech(EnglishWord);
- AddedWord.English := EnglishWord;
- AssignFile(F, Path);
- try
- try
- Reset(F);
- Seek(F, FileSize(F));
- Write(F, AddedWord);
- finally
- CloseFile(F);
- end;
- except
- UnitError.FormError.LabelError.Caption := 'Возникла ошибка при записи нового слова!';
- UnitError.FormError.ShowModal;
- UnitError.FormError.LabelError.Caption := '';
- end;
- end;
- function TFormMenu.IsWordInDictionary(Dictionary: TValueListEditor; SearchStr: String): Boolean;
- var
- I: Integer;
- DictSize: Integer;
- IsFound: Boolean;
- begin
- IsFound := false;
- DictSize := Dictionary.RowCount;
- I := 1;
- while (I < DictSize) and not(IsFound) do
- begin
- if (Dictionary.Cells[0, I] = SearchStr) or (Dictionary.Cells[1, I] = SearchStr) then
- begin
- IsFound := true;
- end;
- Inc(I);
- end;
- IsWordInDictionary := IsFound;
- end;
- function TFormMenu.Search(Dictionary: TValueListEditor; var NewDictionary: TValueListEditor; SoughtFor: String): Boolean;
- var
- DictSize: Integer;
- I: Integer;
- CurrWord: String;
- IsFound: Boolean;
- begin
- IsFound := false;
- DictSize := Dictionary.RowCount;
- I := 1;
- while (I < DictSize) do
- begin
- CurrWord := Dictionary.Cells[0, I];
- if (Length(CurrWord) >= Length(SoughtFor)) then
- begin
- if (Pos(SoughtFor, CurrWord) <> 0) then
- begin
- IsFound := true;
- if not(IsWordInDictionary(NewDictionary, CurrWord)) then
- begin
- NewDictionary.InsertRow(Dictionary.Cells[0, I], Dictionary.Cells[0, I], true);
- end;
- end;
- end;
- Inc(I);
- end;
- Search := IsFound;
- end;
- function TFormMenu.RecognizePartOfSpeech(var ResearchWord: EnglStr): TPartsOfSpeech;
- var
- WordStart: String;
- begin
- RecognizePartOfSpeech := TAdjective;
- if (length(ResearchWord) > 3) then
- begin
- WordStart := Copy(ResearchWord, 1, 2);
- if (WordStart = 'a ') then
- begin
- Delete(ResearchWord, 1, 2);
- RecognizePartOfSpeech := TNoun;
- end;
- if (WordStart = 'an') and (Copy(ResearchWord, 3, 1) = ' ') then
- begin
- Delete(ResearchWord, 1, 3);
- RecognizePartOfSpeech := TNoun;
- end;
- if (WordStart = 'to') and (Copy(ResearchWord, 3, 1) = ' ') then
- begin
- Delete(ResearchWord, 1, 3);
- RecognizePartOfSpeech := TVerb;
- end;
- end
- else
- begin
- RecognizePartOfSpeech := TAdjective;
- end;
- end;
- function LoadDictionaryFromFile(var Dictionary: TValueListEditor; IsFromEnglish: Boolean; Path: String): Boolean;
- var
- NewWord: TWord;
- F: File of TWord;
- Size, I: Integer;
- Translation, ForeignWord: String;
- begin
- AssignFile(F, Path);
- try
- Reset(F);
- Seek(F, 0);
- Size := FileSize(F);
- I := 0;
- while (I < Size) do
- begin
- Read(F, NewWord);
- if (IsFromEnglish) then
- begin
- ForeignWord := FormMenu.GetFinalAppearance(NewWord.PartOfSpeech, NewWord.English);
- Translation := NewWord.Russian;
- end
- else
- begin
- Translation := FormMenu.GetFinalAppearance(NewWord.PartOfSpeech, NewWord.English);
- ForeignWord := NewWord.Russian;
- end;
- Dictionary.InsertRow(ForeignWord, Translation, True);
- Inc(I);
- end;
- CloseFile(F);
- LoadDictionaryFromFile := true;
- except
- UnitError.FormError.LabelError.Caption := 'Возникла ошибка при загрузке словаря из файла! Проверьте наличие файла: ' + path;
- UnitError.FormError.ShowModal;
- UnitError.FormError.LabelError.Caption := '';
- FormMenu.ClearDictionary(Dictionary, (I + 1));
- LoadDictionaryFromFile := false;
- end;
- end;
- procedure TFormMenu.SaveDictionaryToFile(Dictionary: TValueListEditor; Path: String);
- var
- AddedWord: TWord;
- TempF: File of TWord;
- RecordsCount, I: Integer;
- EnglishWord: EnglStr;
- begin
- AssignFile(TempF, TEMP_FILE_NAME);
- try
- I := 1;
- try
- Rewrite(TempF);
- while (I < Dictionary.RowCount) do
- begin
- if not(MainForm.FormMenu.IsRowEmpty(Dictionary, Dictionary.Row)) then
- begin
- AddedWord.Russian := Dictionary.Keys[I];
- EnglishWord := Dictionary.Cells[1, I];
- AddedWord.PartOfSpeech := RecognizePartOfSpeech(EnglishWord);
- AddedWord.English := EnglishWord;
- Write(TempF, AddedWord);
- end;
- Inc(I);
- end;
- finally
- CloseFile(TempF);
- end;
- DeleteFile(Path);
- RenameFile(TEMP_FILE_NAME, Path);
- except
- UnitError.FormError.LabelError.Caption := 'Возникла ошибка при записи словаря в файл! Проверьте наличие файла: ' + path;
- UnitError.FormError.ShowModal;
- UnitError.FormError.LabelError.Caption := '';
- end;
- end;
- function CheckFileExistence(Path: String): Boolean;
- var
- F: File of TWord;
- IsCorrect: Boolean;
- begin
- IsCorrect := true;
- try
- AssignFile(F, Path);
- try
- Reset(F);
- finally
- CloseFile(F);
- end;
- except
- IsCorrect := false;
- UnitError.FormError.LabelError.Caption := 'Возникла ошибка при записи словаря в файл! Проверьте наличие файла: ' + path;
- UnitError.FormError.ShowModal;
- UnitError.FormError.LabelError.Caption := '';
- end;
- CheckFileExistence := IsCorrect;
- end;
- procedure TFormMenu.ButtonChangeDictionaryClick(Sender: TObject);
- begin
- FrameFlashCards.Enabled := false;
- FrameFlashCards.Visible := false;
- FrameTranslateFromRussian.Enabled := false;
- FrameTranslateFromRussian.Visible := false;
- FrameTranslateFromEnglish.Enabled := false;
- FrameTranslateFromEnglish.Visible := false;
- FrameFavorites.Visible := false;
- FrameFavorites.Enabled := false;
- ClearDictionary(FrameEditDictionary.Dictionary, (FrameEditDictionary.Dictionary.RowCount - 1));
- if (CheckFileExistence(FILE_NAME)) and (LoadDictionaryFromFile(FrameEditDictionary.Dictionary, false, FILE_NAME)) then
- begin
- FrameEditDictionary.Visible := true;
- FrameEditDictionary.Enabled := true;
- end;
- end;
- procedure TFormMenu.ButtonFavoritesClick(Sender: TObject);
- begin
- FrameFlashCards.Enabled := false;
- FrameFlashCards.Visible := false;
- FrameTranslateFromRussian.Enabled := false;
- FrameTranslateFromRussian.Visible := false;
- FrameTranslateFromEnglish.Enabled := false;
- FrameTranslateFromEnglish.Visible := false;
- FrameEditDictionary.Visible := false;
- FrameEditDictionary.Enabled := false;
- ClearDictionary(FrameFavorites.Dictionary, (FrameFavorites.Dictionary.RowCount - 1));
- if (CheckFileExistence(FILE_FAVORITES)) and (LoadDictionaryFromFile(FrameFavorites.Dictionary, false, FILE_FAVORITES)) then
- begin
- FrameFavorites.Visible := true;
- FrameFavorites.Enabled := true;
- end;
- end;
- procedure TFormMenu.ButtonFlashCardsClick(Sender: TObject);
- begin
- FrameTranslateFromRussian.Enabled := false;
- FrameTranslateFromRussian.Visible := false;
- FrameTranslateFromEnglish.Enabled := false;
- FrameTranslateFromEnglish.Visible := false;
- FrameEditDictionary.Visible := false;
- FrameEditDictionary.Enabled := false;
- FrameFavorites.Visible := false;
- FrameFavorites.Enabled := false;
- ClearDictionary(FrameFlashCards.DictionaryChangeWordSet, (FrameFlashCards.DictionaryChangeWordSet.RowCount - 1));
- if (CheckFileExistence(FILE_FLASHCARDS)) and (LoadDictionaryFromFile(FrameFlashCards.DictionaryChangeWordSet, false, FILE_FLASHCARDS)) then
- begin
- FrameFlashCards.Enabled := true;
- FrameFlashCards.Visible := true;
- end;
- end;
- procedure TFormMenu.ButtonTranslateFromEnglishClick(Sender: TObject);
- var
- Count: Integer;
- begin
- FrameFlashCards.Enabled := false;
- FrameFlashCards.Visible := false;
- FrameTranslateFromRussian.Enabled := false;
- FrameTranslateFromRussian.Visible := false;
- FrameEditDictionary.Visible := false;
- FrameEditDictionary.Enabled := false;
- FrameFavorites.Visible := false;
- FrameFavorites.Enabled := false;
- Count := FrameTranslateFromEnglish.Dictionary.RowCount;
- Dec(Count);
- ClearDictionary(FrameTranslateFromEnglish.Dictionary, Count);
- if (CheckFileExistence(FILE_NAME)) and (LoadDictionaryFromFile(FrameTranslateFromEnglish.Dictionary, true, FILE_NAME)) then
- begin
- FrameTranslateFromEnglish.Enabled := true;
- FrameTranslateFromEnglish.Visible := true;
- end;
- end;
- procedure TFormMenu.ButtonTranslateFromRussianClick(Sender: TObject);
- begin
- FrameFlashCards.Enabled := false;
- FrameFlashCards.Visible := false;
- FrameEditDictionary.Visible := false;
- FrameEditDictionary.Enabled := false;
- FrameTranslateFromEnglish.Enabled := false;
- FrameTranslateFromEnglish.Visible := false;
- FrameFavorites.Visible := false;
- FrameFavorites.Enabled := false;
- ClearDictionary(FrameTranslateFromRussian.Dictionary, (FrameTranslateFromRussian.Dictionary.RowCount - 1));
- if (CheckFileExistence(FILE_NAME)) and (LoadDictionaryFromFile(FrameTranslateFromRussian.Dictionary, false, FILE_NAME)) then
- begin
- FrameTranslateFromRussian.Enabled := true;
- FrameTranslateFromRussian.Visible := true;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement