-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDataModules.TextExtraction.pas
More file actions
107 lines (91 loc) · 2.88 KB
/
DataModules.TextExtraction.pas
File metadata and controls
107 lines (91 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
unit DataModules.TextExtraction;
interface
uses
System.SysUtils, System.Classes, System.Generics.Collections, System.Types,
AWS.Textract;
type
TTextExtractionDM = class(TDataModule)
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
FClient: ITextractClient;
FCurrentBlocks: TList<ITextractBlock>;
FInputFileName: string;
property Client: ITextractClient read FClient;
procedure SetCurrentBlocks(const ABlocks: TList<ITextractBlock>);
property CurrentBlocks: TList<ITextractBlock> read FCurrentBlocks write SetCurrentBlocks;
function SelectBlocksAt(const APoint: TPointF): TList<ITextractBlock>;
public
function ExtractText(const AFileName: string): Boolean;
function SelectTextAt(const APoint: TPointF): string;
property InputFileName: string read FInputFileName;
end;
var
TextExtractionDM: TTextExtractionDM;
implementation
{%CLASSGROUP 'FMX.Controls.TControl'}
{$R *.dfm}
{ TTextExtractionDM }
procedure TTextExtractionDM.DataModuleCreate(Sender: TObject);
begin
FClient := TTextractClient.Create;
FCurrentBlocks := TList<ITextractBlock>.Create;
end;
procedure TTextExtractionDM.DataModuleDestroy(Sender: TObject);
begin
FCurrentBlocks.Free;
FClient := nil;
end;
function TTextExtractionDM.ExtractText(const AFileName: string): Boolean;
var
LRequest: ITextractDetectDocumentTextRequest;
LResponse: ITextractDetectDocumentTextResponse;
begin
FInputFileName := AFileName;
LRequest := TTextractDetectDocumentTextRequest.Create;
LRequest.Document := TTextractDocument.FromFile(InputFileName);
LResponse := Client.DetectDocumentText(LRequest);
if LResponse.IsSuccessful then
begin
CurrentBlocks := LResponse.Blocks;
Result := True;
end
else
Result := False;
end;
function TTextExtractionDM.SelectBlocksAt(
const APoint: TPointF): TList<ITextractBlock>;
var
LBlock: ITextractBlock;
begin
Result := TList<ITextractBlock>.Create;
for LBlock in CurrentBlocks do
if not LBlock.BlockType.Equals('PAGE')
and (APoint.X >= LBlock.Geometry.BoundingBox.Left.Value)
and (APoint.X <= LBlock.Geometry.BoundingBox.Left.Value + LBlock.Geometry.BoundingBox.Width.Value)
and (APoint.Y >= LBlock.Geometry.BoundingBox.Top.Value)
and (APoint.Y <= LBlock.Geometry.BoundingBox.Top.Value + LBlock.Geometry.BoundingBox.Height.Value) then
Result.Add(LBlock);
end;
function TTextExtractionDM.SelectTextAt(const APoint: TPointF): string;
var
LBlocks: TList<ITextractBlock>;
begin
Result := '';
LBlocks := SelectBlocksAt(APoint);
try
if LBlocks.Count > 0 then
Result := LBlocks.First.Text;
finally
LBlocks.Free;
end;
end;
procedure TTextExtractionDM.SetCurrentBlocks(
const ABlocks: TList<ITextractBlock>);
begin
CurrentBlocks.Clear;
if Assigned(ABlocks) then
for var LBock in ABlocks do
CurrentBlocks.Add(LBock);
end;
end.