-
Notifications
You must be signed in to change notification settings - Fork 1
/
FeatureHelper.cs
138 lines (123 loc) · 5.75 KB
/
FeatureHelper.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.Linq;
using UglyToad.PdfPig.Content;
using UglyToad.PdfPig.Core;
using static UglyToad.PdfPig.Core.PdfPath;
namespace PdfPigSvmRegionClassifier
{
public static class FeatureHelper
{
public static readonly Dictionary<int, string> Categories = new Dictionary<int, string>()
{
{ 0, "title" }, // 0 title
{ 1, "text" }, // 1 text
{ 2, "list" }, // 2 list
{ 3, "table" }, // 3 table
{ 4, "image" }, // 4 image
};
private static readonly char[] Bullets = new char[]
{
'•', 'o', '▪', '❖', '➢', '►', '✓', '➔', '⇨', '➪',
'➨', '➫', '➬', '➭', '➮', '➯', '➱', '➲', '\u2023',
'\u2043', '\u204C', '\u204D'
};
public static double[] GetFeatures(Page page, PdfRectangle bbox, IEnumerable<Letter> letters, IEnumerable<PdfPath> paths, IEnumerable<IPdfImage> images)
{
// Letters features
double charsCount = 0;
double pctNumericChars = 0;
double pctAlphabeticalChars = 0;
double pctSymbolicChars = 0;
double pctBulletChars = 0;
double deltaToHeight = -1; // might be problematic
if (letters != null && letters.Count() > 0)
{
var avgHeight = page.Letters.Select(l => l.GlyphRectangle.Height).Average();
char[] chars = letters.SelectMany(l => l.Value).ToArray();
charsCount = chars.Length;
pctNumericChars = Math.Round(chars.Count(c => char.IsNumber(c)) / charsCount, 5);
pctAlphabeticalChars = Math.Round(chars.Count(c => char.IsLetter(c)) / charsCount, 5);
pctSymbolicChars = Math.Round(chars.Count(c => !char.IsLetterOrDigit(c)) / charsCount, 5);
pctBulletChars = Math.Round(chars.Count(c => Bullets.Any(bullet => bullet == c)) / charsCount, 5);
deltaToHeight = avgHeight != 0 ? Math.Round((double)(letters.Select(l => l.GlyphRectangle.Height).Average() / avgHeight), 5) : -1;
}
// Paths features
double pathsCount = 0;
double pctBezierPaths = 0;
double pctHorPaths = 0;
double pctVertPaths = 0;
double pctOblPaths = 0;
if (paths != null && paths.Count() > 0)
{
foreach (var path in paths)
{
foreach (var command in path.Commands)
{
if (command is BezierCurve bezierCurve)
{
pathsCount++;
pctBezierPaths++;
}
else if (command is Line line)
{
pathsCount++;
if (line.From.X == line.To.X)
{
pctVertPaths++;
}
else if (line.From.Y == line.To.Y)
{
pctHorPaths++;
}
else
{
pctOblPaths++;
}
}
}
}
pctBezierPaths = Math.Round(pctBezierPaths / pathsCount, 5);
pctHorPaths = Math.Round(pctHorPaths / pathsCount, 5);
pctVertPaths = Math.Round(pctVertPaths / pathsCount, 5);
pctOblPaths = Math.Round(pctOblPaths / pathsCount, 5);
}
// Images features
double imagesCount = 0;
double imageAvgProportion = 0;
if (images != null && images.Count() > 0)
{
imagesCount = images.Count();
imageAvgProportion = (double)(images.Average(i => i.Bounds.Area) / bbox.Area);
}
return new double[]
{
charsCount, pctNumericChars, pctAlphabeticalChars, pctSymbolicChars, pctBulletChars, deltaToHeight,
pathsCount, pctBezierPaths, pctHorPaths, pctVertPaths, pctOblPaths,
imagesCount, imageAvgProportion
};
}
public static IEnumerable<Letter> GetLettersInside(PdfRectangle bound, IEnumerable<Letter> letters)
{
return letters.Where(l => l.GlyphRectangle.Left >= bound.Left &&
l.GlyphRectangle.Right <= bound.Right &&
l.GlyphRectangle.Bottom >= bound.Bottom &&
l.GlyphRectangle.Top <= bound.Top);
}
public static IEnumerable<IPdfImage> GetImagesInside(PdfRectangle bound, IEnumerable<IPdfImage> images)
{
return images.Where(b => b.Bounds.Left >= bound.Left &&
b.Bounds.Right <= bound.Right &&
b.Bounds.Bottom >= bound.Bottom &&
b.Bounds.Top <= bound.Top);
}
public static IEnumerable<PdfPath> GetPathsInside(PdfRectangle bound, IEnumerable<PdfPath> paths)
{
return paths.Where(b => b.GetBoundingRectangle().HasValue)
.Where(b => b.GetBoundingRectangle().Value.Left >= bound.Left &&
b.GetBoundingRectangle().Value.Right <= bound.Right &&
b.GetBoundingRectangle().Value.Bottom >= bound.Bottom &&
b.GetBoundingRectangle().Value.Top <= bound.Top);
}
}
}