| 1 | unit FormCheck;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, LazFileUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
|---|
| 7 | ExtCtrls, ComCtrls, Menus, ActnList, Acronym, RegistryEx, Registry, Common,
|
|---|
| 8 | Generics.Collections, FormEx;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 | TReportType = (rtNone, rtNote, rtWarning, rtError);
|
|---|
| 12 |
|
|---|
| 13 | TReportItem = class
|
|---|
| 14 | Message: string;
|
|---|
| 15 | Position: TPoint;
|
|---|
| 16 | Kind: TReportType;
|
|---|
| 17 | end;
|
|---|
| 18 |
|
|---|
| 19 | { TReportItems }
|
|---|
| 20 |
|
|---|
| 21 | TReportItems = class(TObjectList<TReportItem>)
|
|---|
| 22 | function AddNew(Message: string; Position: TPoint;
|
|---|
| 23 | Kind: TReportType = rtNone): TReportItem;
|
|---|
| 24 | procedure SaveToCsv(FileName: string);
|
|---|
| 25 | end;
|
|---|
| 26 |
|
|---|
| 27 | { TFormCheck }
|
|---|
| 28 |
|
|---|
| 29 | TFormCheck = class(TFormEx)
|
|---|
| 30 | AGoToLocation: TAction;
|
|---|
| 31 | ASaveToCsv: TAction;
|
|---|
| 32 | ActionList1: TActionList;
|
|---|
| 33 | ButtonLoadFromFile: TButton;
|
|---|
| 34 | ButtonAcronymsContent: TButton;
|
|---|
| 35 | ButtonAcronymsSummary: TButton;
|
|---|
| 36 | ButtonCheck: TButton;
|
|---|
| 37 | CheckBoxCaseSensitive: TCheckBox;
|
|---|
| 38 | EditSummaryStart: TEdit;
|
|---|
| 39 | EditSummaryEnd: TEdit;
|
|---|
| 40 | GroupBox1: TGroupBox;
|
|---|
| 41 | GroupBox2: TGroupBox;
|
|---|
| 42 | Label1: TLabel;
|
|---|
| 43 | Label2: TLabel;
|
|---|
| 44 | LabelAcronymCountContent: TLabel;
|
|---|
| 45 | LabelAcronymCountSummary: TLabel;
|
|---|
| 46 | ListViewReport: TListView;
|
|---|
| 47 | MemoDocument: TMemo;
|
|---|
| 48 | MenuItem1: TMenuItem;
|
|---|
| 49 | MenuItemGoTo: TMenuItem;
|
|---|
| 50 | OpenDialog1: TOpenDialog;
|
|---|
| 51 | PageControl1: TPageControl;
|
|---|
| 52 | Panel1: TPanel;
|
|---|
| 53 | Panel2: TPanel;
|
|---|
| 54 | PopupMenuReport: TPopupMenu;
|
|---|
| 55 | SaveDialog1: TSaveDialog;
|
|---|
| 56 | Splitter1: TSplitter;
|
|---|
| 57 | TabSheetSource: TTabSheet;
|
|---|
| 58 | TabSheetReport: TTabSheet;
|
|---|
| 59 | procedure AGoToLocationExecute(Sender: TObject);
|
|---|
| 60 | procedure ASaveToCsvExecute(Sender: TObject);
|
|---|
| 61 | procedure ButtonAcronymsSummaryClick(Sender: TObject);
|
|---|
| 62 | procedure ButtonAcronymsContentClick(Sender: TObject);
|
|---|
| 63 | procedure ButtonCheckClick(Sender: TObject);
|
|---|
| 64 | procedure ButtonLoadFromFileClick(Sender: TObject);
|
|---|
| 65 | procedure FormCreate(Sender: TObject);
|
|---|
| 66 | procedure FormDestroy(Sender: TObject);
|
|---|
| 67 | procedure FormShow(Sender: TObject);
|
|---|
| 68 | procedure ListViewReportData(Sender: TObject; Item: TListItem);
|
|---|
| 69 | private
|
|---|
| 70 | AcronymDbSummary: TAcronymDb;
|
|---|
| 71 | AcronymDbContent: TAcronymDb;
|
|---|
| 72 | LastDocumentFileName: string;
|
|---|
| 73 | function SearchLine(Lines: TStrings; Text: string; Start: Integer = 0): Integer;
|
|---|
| 74 | function SearchLineReverse(Lines: TStrings; Text: string; Start: Integer = -1): Integer;
|
|---|
| 75 | procedure FindInSummary;
|
|---|
| 76 | procedure FindInContent;
|
|---|
| 77 | function AllowedSideChar(Before, After: Char): Boolean;
|
|---|
| 78 | function ParseMeaning(Acronym, Text: string; StartIndex: Integer;
|
|---|
| 79 | out Meaning: string; DashSeparator: Boolean = False): Boolean;
|
|---|
| 80 | function IsUppercaseAlpha(Text: string): Boolean;
|
|---|
| 81 | function IsLowercaseAlpha(Text: string): Boolean;
|
|---|
| 82 | function IsAlpha(Text: string): Boolean;
|
|---|
| 83 | function IsAcronym(Text: string): Boolean;
|
|---|
| 84 | function IsDigit(Text: Char): Boolean;
|
|---|
| 85 | procedure ReportDifferencies;
|
|---|
| 86 | function WordContainsLetters(Text, Letters: string): Boolean;
|
|---|
| 87 | function StringEqual(Text1, Text2: string): Boolean;
|
|---|
| 88 | procedure ReloadReport;
|
|---|
| 89 | public
|
|---|
| 90 | AcronymDb: TAcronymDb;
|
|---|
| 91 | ReportItems: TReportItems;
|
|---|
| 92 | procedure UpdateInterface;
|
|---|
| 93 | procedure LoadConfig(RegistryContext: TRegistryContext);
|
|---|
| 94 | procedure SaveConfig(RegistryContext: TRegistryContext);
|
|---|
| 95 | end;
|
|---|
| 96 |
|
|---|
| 97 | const
|
|---|
| 98 | ReportTypeString: array[TReportType] of string = ('', 'Note', 'Warning', 'Error');
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 | implementation
|
|---|
| 102 |
|
|---|
| 103 | {$R *.lfm}
|
|---|
| 104 |
|
|---|
| 105 | uses
|
|---|
| 106 | FormAcronyms;
|
|---|
| 107 |
|
|---|
| 108 | resourcestring
|
|---|
| 109 | SAcronymCountContent = 'Content acronym count:';
|
|---|
| 110 | SAcronymCountSummary = 'Summary acronym count:';
|
|---|
| 111 | SDuplicateAcronymContent = 'Duplicate acronym %s with "%s" in document body.';
|
|---|
| 112 | SDuplicateAcronymSummary = 'Duplicate acronym %s with "%s" in acronym summary.';
|
|---|
| 113 | SMissingAcronymContent = 'Document body acronym %s with meaning "%s" missing from acronym summary.';
|
|---|
| 114 | SMissingAcronymSummary = 'Summary acronym %s with meaning "%s" missing from document body.';
|
|---|
| 115 | SAcronymContentMultipleMeanings = 'Content acronym %s has multiple meanings: %s.';
|
|---|
| 116 | SAcronymSummaryMultipleMeanings = 'Summary acronym %s has multiple meanings: %s.';
|
|---|
| 117 | SAcronymWithDifferentMeaning = 'Acronym %s has different meaning for content "%s" and for summary "%s".';
|
|---|
| 118 | SAcronymWithDifferentMeaningCount = 'Acronym %s has different meaning count for content (%d) and for summary (%d).';
|
|---|
| 119 | SPluralAcronym = 'Acronym %s is defined as plural in document body.';
|
|---|
| 120 | SPluralAcronymUsed = 'Acronym %s is used as plural in document body.';
|
|---|
| 121 | SSummaryAcronyms = 'Summary acronyms';
|
|---|
| 122 | SContentAcronyms = 'Content acronyms';
|
|---|
| 123 | SAcronymUsedBeforeDefined = 'Acronym %s used before it was defined.';
|
|---|
| 124 | SCSVFilter = 'CSV file (.csv)|*.csv|Any file|*.*';
|
|---|
| 125 |
|
|---|
| 126 | const
|
|---|
| 127 | MinAcronymLength = 2;
|
|---|
| 128 |
|
|---|
| 129 | { TReportItems }
|
|---|
| 130 |
|
|---|
| 131 | function TReportItems.AddNew(Message: string; Position: TPoint;
|
|---|
| 132 | Kind: TReportType = rtNone): TReportItem;
|
|---|
| 133 | begin
|
|---|
| 134 | Result := TReportItem.Create;
|
|---|
| 135 | Result.Message := Message;
|
|---|
| 136 | Result.Position := Position;
|
|---|
| 137 | Result.Kind := Kind;
|
|---|
| 138 | Add(Result);
|
|---|
| 139 | end;
|
|---|
| 140 |
|
|---|
| 141 | procedure TReportItems.SaveToCsv(FileName: string);
|
|---|
| 142 | var
|
|---|
| 143 | I: Integer;
|
|---|
| 144 | F: TStringList;
|
|---|
| 145 | Line: TStringList;
|
|---|
| 146 | begin
|
|---|
| 147 | F := TStringList.Create;
|
|---|
| 148 | Line := TStringList.Create;
|
|---|
| 149 | Line.StrictDelimiter := True;
|
|---|
| 150 | try
|
|---|
| 151 | Line.Clear;
|
|---|
| 152 | for I := 0 to Count - 1 do
|
|---|
| 153 | with Items[I] do begin
|
|---|
| 154 | Line.Clear;
|
|---|
| 155 | if Position <> Point(0, 0) then
|
|---|
| 156 | Line.Add(IntToStr(Position.X) + ', ' + IntToStr(Position.Y))
|
|---|
| 157 | else Line.Add('');
|
|---|
| 158 | Line.Add(ReportTypeString[Kind]);
|
|---|
| 159 | Line.Add(Message);
|
|---|
| 160 | F.Add(Line.CommaText);
|
|---|
| 161 | end;
|
|---|
| 162 | F.SaveToFile(FileName);
|
|---|
| 163 | finally
|
|---|
| 164 | F.Free;
|
|---|
| 165 | Line.Free;
|
|---|
| 166 | end;
|
|---|
| 167 | end;
|
|---|
| 168 |
|
|---|
| 169 | { TFormCheck }
|
|---|
| 170 |
|
|---|
| 171 | procedure TFormCheck.ButtonCheckClick(Sender: TObject);
|
|---|
| 172 | begin
|
|---|
| 173 | ReportItems.Clear;
|
|---|
| 174 | UpdateInterface;
|
|---|
| 175 | FindInSummary;
|
|---|
| 176 | FindInContent;
|
|---|
| 177 | ReportDifferencies;
|
|---|
| 178 | UpdateInterface;
|
|---|
| 179 | ReloadReport;
|
|---|
| 180 | TabSheetReport.Show;
|
|---|
| 181 | end;
|
|---|
| 182 |
|
|---|
| 183 | procedure TFormCheck.ButtonLoadFromFileClick(Sender: TObject);
|
|---|
| 184 | begin
|
|---|
| 185 | OpenDialog1.InitialDir := ExtractFileDir(LastDocumentFileName);
|
|---|
| 186 | OpenDialog1.FileName := ExtractFileName(LastDocumentFileName);
|
|---|
| 187 | if OpenDialog1.Execute then begin
|
|---|
| 188 | LastDocumentFileName := OpenDialog1.FileName;
|
|---|
| 189 | MemoDocument.Lines.LoadFromFile(OpenDialog1.FileName);
|
|---|
| 190 | TabSheetSource.Show;
|
|---|
| 191 | end;
|
|---|
| 192 | end;
|
|---|
| 193 |
|
|---|
| 194 | procedure TFormCheck.FormCreate(Sender: TObject);
|
|---|
| 195 | begin
|
|---|
| 196 | AcronymDbSummary := TAcronymDb.Create;
|
|---|
| 197 | AcronymDbContent := TAcronymDb.Create;
|
|---|
| 198 | ReportItems := TReportItems.Create;
|
|---|
| 199 | end;
|
|---|
| 200 |
|
|---|
| 201 | procedure TFormCheck.FormDestroy(Sender: TObject);
|
|---|
| 202 | begin
|
|---|
| 203 | FreeAndNil(ReportItems);
|
|---|
| 204 | FreeAndNil(AcronymDbSummary);
|
|---|
| 205 | FreeAndNil(AcronymDbContent);
|
|---|
| 206 | end;
|
|---|
| 207 |
|
|---|
| 208 | procedure TFormCheck.FormShow(Sender: TObject);
|
|---|
| 209 | begin
|
|---|
| 210 | PageControl1.TabIndex := 0;
|
|---|
| 211 | if FileExists(LastDocumentFileName) then
|
|---|
| 212 | MemoDocument.Lines.LoadFromFile(LastDocumentFileName);
|
|---|
| 213 | UpdateInterface;
|
|---|
| 214 | end;
|
|---|
| 215 |
|
|---|
| 216 | procedure TFormCheck.ListViewReportData(Sender: TObject; Item: TListItem);
|
|---|
| 217 | begin
|
|---|
| 218 | if Item.Index < ReportItems.Count then
|
|---|
| 219 | with ReportItems[Item.Index] do begin
|
|---|
| 220 | if Position <> Point(0, 0) then
|
|---|
| 221 | Item.Caption := IntToStr(Position.X) + ', ' + IntToStr(Position.Y)
|
|---|
| 222 | else Item.Caption := '';
|
|---|
| 223 | Item.Data := ReportItems[Item.Index];
|
|---|
| 224 | Item.SubItems.Add(ReportTypeString[Kind]);
|
|---|
| 225 | Item.SubItems.Add(Message);
|
|---|
| 226 | end;
|
|---|
| 227 | end;
|
|---|
| 228 |
|
|---|
| 229 | procedure TFormCheck.ButtonAcronymsContentClick(Sender: TObject);
|
|---|
| 230 | var
|
|---|
| 231 | FormAcronyms: TFormAcronyms;
|
|---|
| 232 | begin
|
|---|
| 233 | FormAcronyms := TFormAcronyms.Create(Self);
|
|---|
| 234 | try
|
|---|
| 235 | FormAcronyms.AcronymDb := AcronymDb;
|
|---|
| 236 | FormAcronyms.Acronyms := AcronymDbContent.Acronyms;
|
|---|
| 237 | FormAcronyms.Caption := SContentAcronyms;
|
|---|
| 238 | FormAcronyms.ShowModal;
|
|---|
| 239 | finally
|
|---|
| 240 | FreeAndNil(FormAcronyms);
|
|---|
| 241 | end;
|
|---|
| 242 | end;
|
|---|
| 243 |
|
|---|
| 244 | procedure TFormCheck.ButtonAcronymsSummaryClick(Sender: TObject);
|
|---|
| 245 | var
|
|---|
| 246 | FormAcronyms: TFormAcronyms;
|
|---|
| 247 | begin
|
|---|
| 248 | FormAcronyms := TFormAcronyms.Create(Self);
|
|---|
| 249 | try
|
|---|
| 250 | FormAcronyms.AcronymDb := AcronymDb;
|
|---|
| 251 | FormAcronyms.Acronyms := AcronymDbSummary.Acronyms;
|
|---|
| 252 | FormAcronyms.Caption := SSummaryAcronyms;
|
|---|
| 253 | FormAcronyms.ShowModal;
|
|---|
| 254 | finally
|
|---|
| 255 | FreeAndNil(FormAcronyms);
|
|---|
| 256 | end;
|
|---|
| 257 | end;
|
|---|
| 258 |
|
|---|
| 259 | procedure TFormCheck.ASaveToCsvExecute(Sender: TObject);
|
|---|
| 260 | begin
|
|---|
| 261 | SaveDialog1.InitialDir := ExtractFileDir(LastDocumentFileName);
|
|---|
| 262 | SaveDialog1.DefaultExt := '.csv';
|
|---|
| 263 | SaveDialog1.FileName := ExtractFileNameWithoutExt(ExtractFileName(LastDocumentFileName)) + SaveDialog1.DefaultExt;
|
|---|
| 264 | SaveDialog1.Filter := SCSVFilter;
|
|---|
| 265 | if SaveDialog1.Execute then begin
|
|---|
| 266 | ReportItems.SaveToCsv(SaveDialog1.FileName);
|
|---|
| 267 | end;
|
|---|
| 268 | end;
|
|---|
| 269 |
|
|---|
| 270 | procedure TFormCheck.AGoToLocationExecute(Sender: TObject);
|
|---|
| 271 | begin
|
|---|
| 272 | if Assigned(ListViewReport.Selected) then
|
|---|
| 273 | with TReportItem(ListViewReport.Selected.Data) do
|
|---|
| 274 | if Position <> Point(0, 0) then begin
|
|---|
| 275 | MemoDocument.CaretPos := Position;
|
|---|
| 276 | TabSheetSource.Show;
|
|---|
| 277 | end;
|
|---|
| 278 | end;
|
|---|
| 279 |
|
|---|
| 280 | function TFormCheck.SearchLine(Lines: TStrings; Text: string; Start: Integer = 0): Integer;
|
|---|
| 281 | begin
|
|---|
| 282 | Result := Start;
|
|---|
| 283 | while (Result < Lines.Count) and (Pos(Text, Lines[Result]) = 0) do Inc(Result);
|
|---|
| 284 | if Result >= Lines.Count then Result := -1;
|
|---|
| 285 | end;
|
|---|
| 286 |
|
|---|
| 287 | function TFormCheck.SearchLineReverse(Lines: TStrings; Text: string;
|
|---|
| 288 | Start: Integer = -1): Integer;
|
|---|
| 289 | begin
|
|---|
| 290 | if Start = -1 then Result := Lines.Count - 1
|
|---|
| 291 | else Result := Start;
|
|---|
| 292 | while (Result >= 0) and (Pos(Text, Lines[Result]) = 0) do Dec(Result);
|
|---|
| 293 | end;
|
|---|
| 294 |
|
|---|
| 295 | procedure TFormCheck.FindInSummary;
|
|---|
| 296 | var
|
|---|
| 297 | AcronymSectionStart: Integer;
|
|---|
| 298 | AcronymSectionEnd: Integer;
|
|---|
| 299 | I: Integer;
|
|---|
| 300 | Line: string;
|
|---|
| 301 | Acronym: string;
|
|---|
| 302 | Meaning: string;
|
|---|
| 303 | Index: Integer;
|
|---|
| 304 | begin
|
|---|
| 305 | AcronymDbSummary.Acronyms.Clear;
|
|---|
| 306 |
|
|---|
| 307 | AcronymSectionStart := SearchLineReverse(MemoDocument.Lines, EditSummaryStart.Text);
|
|---|
| 308 | if AcronymSectionStart <> -1 then begin
|
|---|
| 309 | AcronymSectionEnd := SearchLine(MemoDocument.Lines, EditSummaryEnd.Text, AcronymSectionStart + 1);
|
|---|
| 310 | if AcronymSectionEnd <> -1 then begin
|
|---|
| 311 | Acronym := '';
|
|---|
| 312 | for I := AcronymSectionStart + 1 to AcronymSectionEnd - 1 do begin
|
|---|
| 313 | Line := Trim(MemoDocument.Lines[I]);
|
|---|
| 314 | Line := StringReplace(Line, #9, ' ', [rfReplaceAll]);
|
|---|
| 315 | if Line <> '' then begin
|
|---|
| 316 | if (Acronym <> '') and IsUppercaseAlpha(Acronym) then begin
|
|---|
| 317 | Meaning := Line;
|
|---|
| 318 | end else begin
|
|---|
| 319 | Index := Pos(' ', Line);
|
|---|
| 320 | if Index > 0 then begin
|
|---|
| 321 | Acronym := Copy(Line, 1, Index - 1);
|
|---|
| 322 | Meaning := Trim(Copy(Line, Index + 1, Length(Line)));
|
|---|
| 323 | end else begin
|
|---|
| 324 | if Acronym = '' then Acronym := Line
|
|---|
| 325 | else Meaning := Line;
|
|---|
| 326 | end;
|
|---|
| 327 | end;
|
|---|
| 328 | if (Acronym <> '') and IsUppercaseAlpha(Acronym) and (Meaning <> '') then begin
|
|---|
| 329 | if Assigned(AcronymDbSummary.SearchAcronym(Acronym, Meaning)) then
|
|---|
| 330 | ReportItems.AddNew(Format(SDuplicateAcronymSummary, [Acronym, Meaning]), Point(0, I), rtWarning)
|
|---|
| 331 | else AcronymDbSummary.AddAcronym(Acronym, Meaning);
|
|---|
| 332 | Acronym := '';
|
|---|
| 333 | Meaning := '';
|
|---|
| 334 | end;
|
|---|
| 335 | end;
|
|---|
| 336 | end;
|
|---|
| 337 | end;
|
|---|
| 338 | end;
|
|---|
| 339 | end;
|
|---|
| 340 |
|
|---|
| 341 | procedure TFormCheck.FindInContent;
|
|---|
| 342 | var
|
|---|
| 343 | Text: string;
|
|---|
| 344 | Index: Integer;
|
|---|
| 345 | State: (stNone, stAcronymUsage, stAcronymDefinition);
|
|---|
| 346 | Acronym: string;
|
|---|
| 347 | AcronymCharBefore: Char;
|
|---|
| 348 | AcronymCharAfter: Char;
|
|---|
| 349 | Lines: TStringList;
|
|---|
| 350 | HasUpperCase: Boolean;
|
|---|
| 351 | HasLowerCase: Boolean;
|
|---|
| 352 | I: Integer;
|
|---|
| 353 | J: Integer;
|
|---|
| 354 | Line: string;
|
|---|
| 355 | Meaning: string;
|
|---|
| 356 | Meaning1: string;
|
|---|
| 357 | Meaning2: string;
|
|---|
| 358 | HasMeaning1: Boolean;
|
|---|
| 359 | HasMeaning2: Boolean;
|
|---|
| 360 | Plural: Boolean;
|
|---|
| 361 | StartIndex: Integer;
|
|---|
| 362 | Acro: TAcronym;
|
|---|
| 363 | begin
|
|---|
| 364 | AcronymDbContent.Acronyms.Clear;
|
|---|
| 365 |
|
|---|
| 366 | // Make lowercase lines where all alpha characters are in uppercase
|
|---|
| 367 | // These are usually first level chapter titles or first page text
|
|---|
| 368 | Lines := TStringList.Create;
|
|---|
| 369 | try
|
|---|
| 370 | Lines.Assign(MemoDocument.Lines);
|
|---|
| 371 | for I := 0 to Lines.Count - 1 do begin
|
|---|
| 372 | HasLowerCase := False;
|
|---|
| 373 | HasUpperCase := False;
|
|---|
| 374 | Line := Lines[I];
|
|---|
| 375 | for J := 1 to Length(Line) do begin
|
|---|
| 376 | if IsUppercaseAlpha(Line[J]) then HasUpperCase := True;
|
|---|
| 377 | if IsLowercaseAlpha(Line[J]) then HasLowerCase := True;
|
|---|
| 378 | end;
|
|---|
| 379 | if HasUpperCase and not HasLowerCase then
|
|---|
| 380 | Lines[I] := LowerCase(Lines[I]);
|
|---|
| 381 | end;
|
|---|
| 382 |
|
|---|
| 383 | // Find acronyms usage in text
|
|---|
| 384 | Text := Lines.Text;
|
|---|
| 385 | Text := StringReplace(Text, #9, ' ', [rfReplaceAll]);
|
|---|
| 386 | Text := StringReplace(Text, LineEnding, ' ', [rfReplaceAll]);
|
|---|
| 387 | Index := 1;
|
|---|
| 388 | AcronymCharBefore := ' ';
|
|---|
| 389 | AcronymCharAfter := ' ';
|
|---|
| 390 | State := stNone;
|
|---|
| 391 | StartIndex := 0;
|
|---|
| 392 | Acronym := '';
|
|---|
| 393 | if Length(Text) = 0 then Exit;
|
|---|
| 394 | repeat
|
|---|
| 395 | if State = stAcronymUsage then begin
|
|---|
| 396 | if not IsUppercaseAlpha(Text[Index]) then begin
|
|---|
| 397 | if Text[Index] = 's' then begin
|
|---|
| 398 | Acronym := Acronym + Text[Index];
|
|---|
| 399 | Inc(Index);
|
|---|
| 400 | end;
|
|---|
| 401 | if (Index) < Length(Text) then AcronymCharAfter := Text[Index]
|
|---|
| 402 | else AcronymCharAfter := ' ';
|
|---|
| 403 | State := stNone;
|
|---|
| 404 |
|
|---|
| 405 | // Allow plural acronyms with ending 's' character
|
|---|
| 406 | if (Length(Acronym) >= 1) and (Acronym[Length(Acronym)] = 's') then begin
|
|---|
| 407 | Acronym := Copy(Acronym, 1, Length(Acronym) - 1);
|
|---|
| 408 | Plural := True;
|
|---|
| 409 | end else Plural := False;
|
|---|
| 410 |
|
|---|
| 411 | // Acronyms should not contain numbers
|
|---|
| 412 | if (Length(Acronym) >= MinAcronymLength) and
|
|---|
| 413 | AllowedSideChar(AcronymCharBefore, AcronymCharAfter) then begin
|
|---|
| 414 | // If plural acronym then try to remove ending 's' character from the meaning
|
|---|
| 415 | if Plural then ReportItems.AddNew(Format(SPluralAcronymUsed, [Acronym]), Point(0, 0), rtNote);
|
|---|
| 416 |
|
|---|
| 417 | Acro := AcronymDbContent.Acronyms.SearchByName(Acronym);
|
|---|
| 418 | if not Assigned(Acro) then begin
|
|---|
| 419 | ReportItems.AddNew(Format(SAcronymUsedBeforeDefined, [Acronym]), Point(0, 0), rtNote);
|
|---|
| 420 | AcronymDbContent.AddAcronym(Acronym, '');
|
|---|
| 421 | end;
|
|---|
| 422 | end;
|
|---|
| 423 | end else Acronym := Acronym + Text[Index];
|
|---|
| 424 | end else
|
|---|
| 425 | if State = stAcronymDefinition then begin
|
|---|
| 426 | if Text[Index] = ')' then begin
|
|---|
| 427 | // Allow plural acronyms with ending 's' character
|
|---|
| 428 | if (Length(Acronym) >= 1) and (Acronym[Length(Acronym)] = 's') then begin
|
|---|
| 429 | Acronym := Copy(Acronym, 1, Length(Acronym) - 1);
|
|---|
| 430 | Plural := True;
|
|---|
| 431 | end else Plural := False;
|
|---|
| 432 | if IsAcronym(Acronym) then begin
|
|---|
| 433 | HasMeaning1 := ParseMeaning(Acronym, Text, StartIndex - 1, Meaning1);
|
|---|
| 434 | if HasMeaning1 then Meaning := Meaning1;
|
|---|
| 435 | HasMeaning2 := ParseMeaning(Acronym, Text, StartIndex - 1, Meaning2, True);
|
|---|
| 436 | if HasMeaning2 then Meaning := Meaning2;
|
|---|
| 437 | if HasMeaning1 and HasMeaning2 then begin
|
|---|
| 438 | if Length(Meaning1) > Length(Meaning2) then Meaning := Meaning1
|
|---|
| 439 | else Meaning := Meaning2;
|
|---|
| 440 | end;
|
|---|
| 441 | if HasMeaning1 or HasMeaning2 then begin
|
|---|
| 442 | // If plural acronym then try to remove ending 's' character from the meaning
|
|---|
| 443 | if Plural then ReportItems.AddNew(Format(SPluralAcronym, [Acronym]), Point(0, 0), rtNote);
|
|---|
| 444 | if Plural and (Length(Meaning) >= 1) and (Meaning[Length(Meaning)] = 's') then begin
|
|---|
| 445 | Meaning := Copy(Meaning, 1, Length(Meaning) - 1);
|
|---|
| 446 | end;
|
|---|
| 447 | if Assigned(AcronymDbContent.SearchAcronym(Acronym, Meaning)) then
|
|---|
| 448 | ReportItems.AddNew(Format(SDuplicateAcronymContent, [Acronym, Meaning]), Point(0, 0), rtWarning)
|
|---|
| 449 | else AcronymDbContent.AddAcronym(Acronym, Meaning);
|
|---|
| 450 | end;
|
|---|
| 451 | end else
|
|---|
| 452 | // No acronym inside parenthesis, continue with parsing inside
|
|---|
| 453 | Index := StartIndex + 1;
|
|---|
| 454 | State := stNone;
|
|---|
| 455 | end else begin
|
|---|
| 456 | Acronym := Acronym + Text[Index];
|
|---|
| 457 | end;
|
|---|
| 458 | end else begin
|
|---|
| 459 | if Text[Index] = '(' then begin
|
|---|
| 460 | State := stAcronymDefinition;
|
|---|
| 461 | Acronym := '';
|
|---|
| 462 | StartIndex := Index;
|
|---|
| 463 | end else
|
|---|
| 464 | if IsUppercaseAlpha(Text[Index]) then begin
|
|---|
| 465 | State := stAcronymUsage;
|
|---|
| 466 | Acronym := Text[Index];
|
|---|
| 467 | if (Index - 1) >= 1 then AcronymCharBefore := Text[Index - 1]
|
|---|
| 468 | else AcronymCharBefore := ' ';
|
|---|
| 469 | end;
|
|---|
| 470 | end;
|
|---|
| 471 | Inc(Index);
|
|---|
| 472 | until Index > Length(Text);
|
|---|
| 473 | finally
|
|---|
| 474 | Lines.Free;
|
|---|
| 475 | end;
|
|---|
| 476 | end;
|
|---|
| 477 |
|
|---|
| 478 | function TFormCheck.AllowedSideChar(Before, After: Char): Boolean;
|
|---|
| 479 | begin
|
|---|
| 480 | Result := ((Before = ' ') or (Before = #10) or (Before = #13) or (Before = ',') or
|
|---|
| 481 | (Before = ';') or (Before = '(') or (Before = ')'))
|
|---|
| 482 | and ((After = ' ') or (After = #10) or (After = #13) or (After = ',') or
|
|---|
| 483 | (After = '.') or (After = ';') or (After = '(') or (After = ')'));
|
|---|
| 484 | end;
|
|---|
| 485 |
|
|---|
| 486 | function TFormCheck.ParseMeaning(Acronym, Text: string; StartIndex: Integer;
|
|---|
| 487 | out Meaning: string; DashSeparator: Boolean): Boolean;
|
|---|
| 488 | var
|
|---|
| 489 | StartIndex2: Integer;
|
|---|
| 490 | StartIndex3: Integer;
|
|---|
| 491 | StartIndex4: Integer;
|
|---|
| 492 | LetterIndex: Integer;
|
|---|
| 493 | OneWord: string;
|
|---|
| 494 | WordLetterIndex: Integer;
|
|---|
| 495 | WordCount: Integer;
|
|---|
| 496 | WordCountWrong: Integer;
|
|---|
| 497 | begin
|
|---|
| 498 | Result := True;
|
|---|
| 499 | Meaning := '';
|
|---|
| 500 | StartIndex2 := StartIndex;
|
|---|
| 501 | LetterIndex := Length(Acronym);
|
|---|
| 502 | WordCount := 0;
|
|---|
| 503 | WordCountWrong := 0;
|
|---|
| 504 | while Length(Acronym) > 0 do begin
|
|---|
| 505 | StartIndex3 := PosFromIndexReverse(' ', Text, StartIndex2);
|
|---|
| 506 | if DashSeparator then begin
|
|---|
| 507 | StartIndex4 := PosFromIndexReverse('-', Text, StartIndex2);
|
|---|
| 508 | if StartIndex4 > StartIndex3 then StartIndex3 := StartIndex4;
|
|---|
| 509 | end;
|
|---|
| 510 |
|
|---|
| 511 | if StartIndex3 = 0 then Break;
|
|---|
| 512 | OneWord := Copy(Text, StartIndex3 + 1, StartIndex2 - StartIndex3);
|
|---|
| 513 | if OneWord = '$' then begin
|
|---|
| 514 | // Avoid parsing Bash variables
|
|---|
| 515 | Result := False;
|
|---|
| 516 | Exit;
|
|---|
| 517 | end;
|
|---|
| 518 | if Trim(OneWord) = '' then begin
|
|---|
| 519 | StartIndex2 := StartIndex3 - 1;
|
|---|
| 520 | Continue;
|
|---|
| 521 | end;
|
|---|
| 522 | // Is first letter capital?
|
|---|
| 523 | if (Length(OneWord) > 0) and IsAlpha(OneWord[1]) then begin
|
|---|
| 524 | WordLetterIndex := PosFromIndexReverse(LowerCase(OneWord[1]), LowerCase(Copy(Acronym, 1, LetterIndex)), LetterIndex);
|
|---|
| 525 | if WordLetterIndex > 0 then begin
|
|---|
| 526 | // First letter was found in acronym
|
|---|
| 527 | if WordLetterIndex <= LetterIndex then begin
|
|---|
| 528 | if not WordContainsLetters(LowerCase(OneWord), LowerCase(Copy(Acronym, WordLetterIndex, LetterIndex - WordLetterIndex + 1))) then begin
|
|---|
| 529 | Result := False;
|
|---|
| 530 | Exit;
|
|---|
| 531 | end;
|
|---|
| 532 | LetterIndex := WordLetterIndex - 1;
|
|---|
| 533 | end else begin
|
|---|
| 534 | Dec(LetterIndex);
|
|---|
| 535 | end;
|
|---|
| 536 | WordCountWrong := 0;
|
|---|
| 537 | end else begin
|
|---|
| 538 | Inc(WordCountWrong);
|
|---|
| 539 | if WordCountWrong > 1 then begin
|
|---|
| 540 | Result := False;
|
|---|
| 541 | Exit;
|
|---|
| 542 | end;
|
|---|
| 543 | end;
|
|---|
| 544 | end else begin
|
|---|
| 545 | Inc(WordCountWrong);
|
|---|
| 546 | if WordCountWrong > 1 then begin
|
|---|
| 547 | Result := False;
|
|---|
| 548 | Exit;
|
|---|
| 549 | end;
|
|---|
| 550 | end;
|
|---|
| 551 | StartIndex2 := StartIndex3 - 1;
|
|---|
| 552 | if LetterIndex < 1 then Break;
|
|---|
| 553 | Inc(WordCount);
|
|---|
| 554 | if WordCount > 2 * Length(Acronym) then begin
|
|---|
| 555 | // False acronym in braces with too much words
|
|---|
| 556 | Result := False;
|
|---|
| 557 | Exit;
|
|---|
| 558 | end;
|
|---|
| 559 | end;
|
|---|
| 560 | Meaning := Trim(Copy(Text, StartIndex2 + 1, StartIndex - StartIndex2));
|
|---|
| 561 | end;
|
|---|
| 562 |
|
|---|
| 563 | function TFormCheck.IsUppercaseAlpha(Text: string): Boolean;
|
|---|
| 564 | var
|
|---|
| 565 | I: Integer;
|
|---|
| 566 | begin
|
|---|
| 567 | I := 1;
|
|---|
| 568 | Result := True;
|
|---|
| 569 | while (I <= Length(Text)) do begin
|
|---|
| 570 | if not (Text[I] in ['A'..'Z']) then begin
|
|---|
| 571 | Result := False;
|
|---|
| 572 | Break;
|
|---|
| 573 | end;
|
|---|
| 574 | Inc(I);
|
|---|
| 575 | end;
|
|---|
| 576 | end;
|
|---|
| 577 |
|
|---|
| 578 | function TFormCheck.IsLowercaseAlpha(Text: string): Boolean;
|
|---|
| 579 | var
|
|---|
| 580 | I: Integer;
|
|---|
| 581 | begin
|
|---|
| 582 | I := 1;
|
|---|
| 583 | Result := True;
|
|---|
| 584 | while (I <= Length(Text)) do begin
|
|---|
| 585 | if not (Text[I] in ['a'..'z']) then begin
|
|---|
| 586 | Result := False;
|
|---|
| 587 | Break;
|
|---|
| 588 | end;
|
|---|
| 589 | Inc(I);
|
|---|
| 590 | end;
|
|---|
| 591 | end;
|
|---|
| 592 |
|
|---|
| 593 | function TFormCheck.IsAlpha(Text: string): Boolean;
|
|---|
| 594 | var
|
|---|
| 595 | I: Integer;
|
|---|
| 596 | begin
|
|---|
| 597 | I := 1;
|
|---|
| 598 | Result := True;
|
|---|
| 599 | while (I <= Length(Text)) do begin
|
|---|
| 600 | if not (Text[I] in ['A'..'Z']) and not (Text[I] in ['a'..'z']) then begin
|
|---|
| 601 | Result := False;
|
|---|
| 602 | Break;
|
|---|
| 603 | end;
|
|---|
| 604 | Inc(I);
|
|---|
| 605 | end;
|
|---|
| 606 | end;
|
|---|
| 607 |
|
|---|
| 608 | function TFormCheck.IsAcronym(Text: string): Boolean;
|
|---|
| 609 | const
|
|---|
| 610 | MinAcronymLength = 2;
|
|---|
| 611 | begin
|
|---|
| 612 | Result := (Length(Text) >= MinAcronymLength) and IsUppercaseAlpha(Text);
|
|---|
| 613 | end;
|
|---|
| 614 |
|
|---|
| 615 | function TFormCheck.IsDigit(Text: Char): Boolean;
|
|---|
| 616 | begin
|
|---|
| 617 | Result := Text in ['0'..'9'];
|
|---|
| 618 | end;
|
|---|
| 619 |
|
|---|
| 620 | procedure TFormCheck.ReportDifferencies;
|
|---|
| 621 | var
|
|---|
| 622 | I: Integer;
|
|---|
| 623 | J: Integer;
|
|---|
| 624 | Acronym: TAcronym;
|
|---|
| 625 | Acronym2: TAcronym;
|
|---|
| 626 | Meaning: TAcronymMeaning;
|
|---|
| 627 | Meaning2: TAcronymMeaning;
|
|---|
| 628 | begin
|
|---|
| 629 | // In content but not in summary
|
|---|
| 630 | for I := 0 to AcronymDbContent.Acronyms.Count - 1 do begin
|
|---|
| 631 | Acronym := AcronymDbContent.Acronyms[I];
|
|---|
| 632 | if Acronym.Meanings.Count > 1 then
|
|---|
| 633 | ReportItems.AddNew(Format(SAcronymContentMultipleMeanings, [Acronym.Name,
|
|---|
| 634 | Acronym.Meanings.GetNames]), Point(0, 0), rtWarning);
|
|---|
| 635 | Acronym2 := AcronymDbSummary.Acronyms.SearchByName(Acronym.Name);
|
|---|
| 636 | if not Assigned(Acronym2) then
|
|---|
| 637 | for J := 0 to Acronym.Meanings.Count - 1 do begin
|
|---|
| 638 | Meaning := Acronym.Meanings[J];
|
|---|
| 639 | if not Assigned(AcronymDbSummary.SearchAcronym(Acronym.Name, Meaning.Name, [sfCaseInsensitive])) then
|
|---|
| 640 | ReportItems.AddNew(Format(SMissingAcronymContent, [Acronym.Name, Meaning.Name]), Point(0, 0), rtWarning);
|
|---|
| 641 | end;
|
|---|
| 642 | end;
|
|---|
| 643 |
|
|---|
| 644 | // In summary but not in content
|
|---|
| 645 | for I := 0 to AcronymDbSummary.Acronyms.Count - 1 do begin
|
|---|
| 646 | Acronym := AcronymDbSummary.Acronyms[I];
|
|---|
| 647 | if Acronym.Meanings.Count > 1 then
|
|---|
| 648 | ReportItems.AddNew(Format(SAcronymSummaryMultipleMeanings, [Acronym.Name, Acronym.Meanings.GetNames]), Point(0, 0), rtWarning);
|
|---|
| 649 | Acronym2 := AcronymDbContent.Acronyms.SearchByName(Acronym.Name);
|
|---|
| 650 | if not Assigned(Acronym2) then
|
|---|
| 651 | for J := 0 to Acronym.Meanings.Count - 1 do begin
|
|---|
| 652 | Meaning := Acronym.Meanings[J];
|
|---|
| 653 | if not Assigned(AcronymDbContent.SearchAcronym(Acronym.Name, Meaning.Name, [sfCaseInsensitive])) then
|
|---|
| 654 | ReportItems.AddNew(Format(SMissingAcronymSummary, [Acronym.Name, Meaning.Name]), Point(0, 0), rtWarning);
|
|---|
| 655 | end;
|
|---|
| 656 | end;
|
|---|
| 657 |
|
|---|
| 658 | // With different meaning
|
|---|
| 659 | for I := 0 to AcronymDbSummary.Acronyms.Count - 1 do begin
|
|---|
| 660 | Acronym := AcronymDbSummary.Acronyms[I];
|
|---|
| 661 | Acronym2 := AcronymDbContent.Acronyms.SearchByName(Acronym.Name);
|
|---|
| 662 | if Assigned(Acronym2) then begin
|
|---|
| 663 | if (Acronym.Meanings.Count = 1) and (Acronym2.Meanings.Count = 1) then begin
|
|---|
| 664 | Meaning := Acronym.Meanings[0];
|
|---|
| 665 | Meaning2 := Acronym2.Meanings[0];
|
|---|
| 666 | if not StringEqual(Meaning.Name, Meaning2.Name) then
|
|---|
| 667 | ReportItems.AddNew(Format(SAcronymWithDifferentMeaning, [Acronym.Name, Meaning2.Name, Meaning.Name]), Point(0, 0), rtWarning);
|
|---|
| 668 | end else
|
|---|
| 669 | ReportItems.AddNew(Format(SAcronymWithDifferentMeaningCount, [Acronym.Name, Acronym2.Meanings.Count, Acronym.Meanings.Count]), Point(0, 0), rtWarning);
|
|---|
| 670 | end;
|
|---|
| 671 | end;
|
|---|
| 672 | end;
|
|---|
| 673 |
|
|---|
| 674 | function TFormCheck.WordContainsLetters(Text, Letters: string): Boolean;
|
|---|
| 675 | var
|
|---|
| 676 | I: Integer;
|
|---|
| 677 | LetterIndex: Integer;
|
|---|
| 678 | begin
|
|---|
| 679 | Result := True;
|
|---|
| 680 | for I := 1 to Length(Letters) do begin
|
|---|
| 681 | LetterIndex := Pos(Letters[I], Text);
|
|---|
| 682 | if LetterIndex > 0 then begin
|
|---|
| 683 | Text := Copy(Text, LetterIndex + 1, Length(Text));
|
|---|
| 684 | end else begin
|
|---|
| 685 | Result := False;
|
|---|
| 686 | Break;
|
|---|
| 687 | end;
|
|---|
| 688 | end;
|
|---|
| 689 | end;
|
|---|
| 690 |
|
|---|
| 691 | function TFormCheck.StringEqual(Text1, Text2: string): Boolean;
|
|---|
| 692 | begin
|
|---|
| 693 | if CheckBoxCaseSensitive.Checked then Result := Text1 = Text2
|
|---|
| 694 | else Result := LowerCase(Text1) = LowerCase(Text2);
|
|---|
| 695 | end;
|
|---|
| 696 |
|
|---|
| 697 | procedure TFormCheck.ReloadReport;
|
|---|
| 698 | begin
|
|---|
| 699 | ListViewReport.Items.Count := ReportItems.Count;
|
|---|
| 700 | ListViewReport.Refresh;
|
|---|
| 701 | end;
|
|---|
| 702 |
|
|---|
| 703 | procedure TFormCheck.UpdateInterface;
|
|---|
| 704 | begin
|
|---|
| 705 | LabelAcronymCountContent.Caption := SAcronymCountContent + ' ' + IntToStr(AcronymDbContent.GetMeaningsCount);
|
|---|
| 706 | LabelAcronymCountSummary.Caption := SAcronymCountSummary + ' ' + IntToStr(AcronymDbSummary.GetMeaningsCount);
|
|---|
| 707 | end;
|
|---|
| 708 |
|
|---|
| 709 | procedure TFormCheck.LoadConfig(RegistryContext: TRegistryContext);
|
|---|
| 710 | begin
|
|---|
| 711 | with TRegistryEx.Create do
|
|---|
| 712 | try
|
|---|
| 713 | RootKey := RegistryContext.RootKey;
|
|---|
| 714 | OpenKey(RegistryContext.Key, True);
|
|---|
| 715 | EditSummaryStart.Text := ReadStringWithDefault('SummaryStart', 'ACRONYMS AND ABBREVIATIONS');
|
|---|
| 716 | EditSummaryEnd.Text := ReadStringWithDefault('SummaryEnd', 'Appendix');
|
|---|
| 717 | LastDocumentFileName := ReadStringWithDefault('LastDocumentFileName', '');
|
|---|
| 718 | CheckBoxCaseSensitive.Checked := ReadBoolWithDefault('CaseSensitiveComparison', False);
|
|---|
| 719 | finally
|
|---|
| 720 | Free;
|
|---|
| 721 | end;
|
|---|
| 722 | end;
|
|---|
| 723 |
|
|---|
| 724 | procedure TFormCheck.SaveConfig(RegistryContext: TRegistryContext);
|
|---|
| 725 | begin
|
|---|
| 726 | with TRegistryEx.Create do
|
|---|
| 727 | try
|
|---|
| 728 | RootKey := RegistryContext.RootKey;
|
|---|
| 729 | OpenKey(RegistryContext.Key, True);
|
|---|
| 730 | WriteString('SummaryStart', EditSummaryStart.Text);
|
|---|
| 731 | WriteString('SummaryEnd', EditSummaryEnd.Text);
|
|---|
| 732 | WriteString('LastDocumentFileName', LastDocumentFileName);
|
|---|
| 733 | WriteBool('CaseSensitiveComparison', CheckBoxCaseSensitive.Checked);
|
|---|
| 734 | finally
|
|---|
| 735 | Free;
|
|---|
| 736 | end;
|
|---|
| 737 | end;
|
|---|
| 738 |
|
|---|
| 739 | end.
|
|---|
| 740 |
|
|---|