-
Notifications
You must be signed in to change notification settings - Fork 722
/
Copy pathSparkNLPReader.scala
370 lines (346 loc) · 37.5 KB
/
SparkNLPReader.scala
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright 2017-2024 John Snow Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.johnsnowlabs.reader
import org.apache.spark.sql.DataFrame
import scala.collection.JavaConverters._
class SparkNLPReader(params: java.util.Map[String, String] = new java.util.HashMap()) {
/** Instantiates class to read HTML files.
*
* Two types of input paths are supported,
*
* htmlPath: this is a path to a directory of HTML files or a path to an HTML file E.g.
* "path/html/files"
*
* url: this is the URL or set of URLs of a website . E.g., "https://www.wikipedia.org"
*
* ==Example==
* {{{
* val url = "https://www.wikipedia.org"
* val sparkNLPReader = new SparkNLPReader()
* val htmlDf = sparkNLPReader.html(url)
* }}}
*
* ==Example 2==
* You can use SparkNLP for one line of code
* {{{
* val htmlDf = SparkNLP.read.html(url)
* }}}
* {{{
* htmlDf.show(false)
*
* +--------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
* |url |html |
* +--------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
* |https://example.com/|[{Title, Example Domain, {pageNumber -> 1}}, {NarrativeText, 0, This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission., {pageNumber -> 1}}, {NarrativeText, 0, More information... More information..., {pageNumber -> 1}}] |
* +--------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
*
* htmlDf.printSchema()
* root
* |-- url: string (nullable = true)
* |-- html: array (nullable = true)
* | |-- element: struct (containsNull = true)
* | | |-- elementType: string (nullable = true)
* | | |-- content: string (nullable = true)
* | | |-- metadata: map (nullable = true)
* | | | |-- key: string
* | | | |-- value: string (valueContainsNull = true)
* }}}
*
* @param params
* Parameter with custom configuration
*/
def html(htmlPath: String): DataFrame = {
val htmlReader = new HTMLReader(getTitleFontSize, getStoreContent)
htmlReader.read(htmlPath)
}
def html(urls: Array[String]): DataFrame = {
val htmlReader = new HTMLReader(getTitleFontSize, getStoreContent)
htmlReader.read(urls)
}
def html(urls: java.util.List[String]): DataFrame = {
val htmlReader = new HTMLReader(getTitleFontSize, getStoreContent)
htmlReader.read(urls.asScala.toArray)
}
private def getTitleFontSize: Int = {
val titleFontSize =
try {
params.asScala.getOrElse("titleFontSize", "16").toInt
} catch {
case _: IllegalArgumentException => 16
}
titleFontSize
}
private def getStoreContent: Boolean = {
val storeContent =
try {
params.asScala.getOrElse("storeContent", "false").toBoolean
} catch {
case _: IllegalArgumentException => false
}
storeContent
}
/** Instantiates class to read email files.
*
* emailPath: this is a path to a directory of HTML files or a path to an HTML file E.g.
* "path/email/files"
*
* ==Example==
* {{{
* val emailsPath = "home/user/emails-directory"
* val sparkNLPReader = new SparkNLPReader()
* val emailDf = sparkNLPReader.email(emailsPath)
* }}}
*
* ==Example 2==
* You can use SparkNLP for one line of code
* {{{
* val emailDf = SparkNLP.read.email(emailsPath)
* }}}
*
* {{{
* emailDf.select("email").show(false)
* +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
* |email |
* +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
* |[{Title, Email Text Attachments, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>}}, {NarrativeText, Email test with two text attachments\r\n\r\nCheers,\r\n\r\n, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>, mimeType -> text/plain}}, {NarrativeText, <html>\r\n<head>\r\n<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\r\n<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>\r\n</head>\r\n<body dir="ltr">\r\n<span style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">Email test with two text attachments</span>\r\n<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">\r\n<br>\r\n</div>\r\n<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">\r\nCheers,</div>\r\n<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">\r\n<br>\r\n</div>\r\n</body>\r\n</html>\r\n, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>, mimeType -> text/html}}, {Attachment, filename.txt, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>, contentType -> text/plain; name="filename.txt"}}, {NarrativeText, This is the content of the file.\n, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>, mimeType -> text/plain}}, {Attachment, filename2.txt, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>, contentType -> text/plain; name="filename2.txt"}}, {NarrativeText, This is an additional content file.\n, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>, mimeType -> text/plain}}]|
* +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
*
* emailDf.printSchema()
* root
* |-- path: string (nullable = true)
* |-- content: binary (nullable = true)
* |-- email: array (nullable = true)
* | |-- element: struct (containsNull = true)
* | | |-- elementType: string (nullable = true)
* | | |-- content: string (nullable = true)
* | | |-- metadata: map (nullable = true)
* | | | |-- key: string
* | | | |-- value: string (valueContainsNull = true)
* }}}
*
* @param params
* Parameter with custom configuration
*/
def email(emailPath: String): DataFrame = {
val emailReader = new EmailReader(getAddAttachmentContent, getStoreContent)
emailReader.read(emailPath)
}
private def getAddAttachmentContent: Boolean = {
val addAttachmentContent =
try {
params.asScala.getOrElse("addAttachmentContent", "false").toBoolean
} catch {
case _: IllegalArgumentException => false
}
addAttachmentContent
}
/** Instantiates class to read Word files.
*
* docPath: this is a path to a directory of Word files or a path to an HTML file E.g.
* "path/word/files"
*
* ==Example==
* {{{
* val docsPath = "home/user/word-directory"
* val sparkNLPReader = new SparkNLPReader()
* val docsDf = sparkNLPReader.email(docsPath)
* }}}
*
* ==Example 2==
* You can use SparkNLP for one line of code
* {{{
* val docsDf = SparkNLP.read.doc(docsPath)
* }}}
*
* {{{
* docsDf.select("doc").show(false)
* +----------------------------------------------------------------------------------------------------------------------------------------------------+
* |doc | |
* +----------------------------------------------------------------------------------------------------------------------------------------------------+
* |[{Table, Header Col 1, {}}, {Table, Header Col 2, {}}, {Table, Lorem ipsum, {}}, {Table, A Link example, {}}, {NarrativeText, Dolor sit amet, {}}] |
* +----------------------------------------------------------------------------------------------------------------------------------------------------+
*
* docsDf.printSchema()
* root
* |-- path: string (nullable = true)
* |-- content: binary (nullable = true)
* |-- doc: array (nullable = true)
* | |-- element: struct (containsNull = true)
* | | |-- elementType: string (nullable = true)
* | | |-- content: string (nullable = true)
* | | |-- metadata: map (nullable = true)
* | | | |-- key: string
* | | | |-- value: string (valueContainsNull = true)
* }}}
*
* @param params
* Parameter with custom configuration
*/
def doc(docPath: String): DataFrame = {
val wordReader = new WordReader(getStoreContent)
wordReader.doc(docPath)
}
/** Instantiates class to read Excel files.
*
* docPath: this is a path to a directory of Excel files or a path to an HTML file E.g.
* "path/excel/files"
*
* ==Example==
* {{{
* val docsPath = "home/user/excel-directory"
* val sparkNLPReader = new SparkNLPReader()
* val xlsDf = sparkNLPReader.xls(docsPath)
* }}}
*
* ==Example 2==
* You can use SparkNLP for one line of code
* {{{
* val xlsDf = SparkNLP.read.xls(docsPath)
* }}}
*
* {{{
* xlsDf.select("xls").show(false)
* +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
* |xls |
* +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
* |[{Title, Financial performance, {SheetName -> Index}}, {Title, Topic\tPeriod\t\t\tPage, {SheetName -> Index}}, {NarrativeText, Quarterly revenue\tNine quarters to 30 June 2023\t\t\t1.0, {SheetName -> Index}}, {NarrativeText, Group financial performance\tFY 22\tFY 23\t\t2.0, {SheetName -> Index}}, {NarrativeText, Segmental results\tFY 22\tFY 23\t\t3.0, {SheetName -> Index}}, {NarrativeText, Segmental analysis\tFY 22\tFY 23\t\t4.0, {SheetName -> Index}}, {NarrativeText, Cash flow\tFY 22\tFY 23\t\t5.0, {SheetName -> Index}}, {Title, Operational metrics, {SheetName -> Index}}, {Title, Topic\tPeriod\t\t\tPage, {SheetName -> Index}}, {NarrativeText, Mobile customers\tNine quarters to 30 June 2023\t\t\t6.0, {SheetName -> Index}}, {NarrativeText, Fixed broadband customers\tNine quarters to 30 June 2023\t\t\t7.0, {SheetName -> Index}}, {NarrativeText, Marketable homes passed\tNine quarters to 30 June 2023\t\t\t8.0, {SheetName -> Index}}, {NarrativeText, TV customers\tNine quarters to 30 June 2023\t\t\t9.0, {SheetName -> Index}}, {NarrativeText, Converged customers\tNine quarters to 30 June 2023\t\t\t10.0, {SheetName -> Index}}, {NarrativeText, Mobile churn\tNine quarters to 30 June 2023\t\t\t11.0, {SheetName -> Index}}, {NarrativeText, Mobile data usage\tNine quarters to 30 June 2023\t\t\t12.0, {SheetName -> Index}}, {NarrativeText, Mobile ARPU\tNine quarters to 30 June 2023\t\t\t13.0, {SheetName -> Index}}, {Title, Other, {SheetName -> Index}}, {Title, Topic\tPeriod\t\t\tPage, {SheetName -> Index}}, {NarrativeText, Average foreign exchange rates\tNine quarters to 30 June 2023\t\t\t14.0, {SheetName -> Index}}, {NarrativeText, Guidance rates\tFY 23/24\t\t\t14.0, {SheetName -> Index}}]|
* +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
*
* xlsDf.printSchema()
* root
* |-- path: string (nullable = true)
* |-- content: binary (nullable = true)
* |-- xls: array (nullable = true)
* | |-- element: struct (containsNull = true)
* | | |-- elementType: string (nullable = true)
* | | |-- content: string (nullable = true)
* | | |-- metadata: map (nullable = true)
* | | | |-- key: string
* | | | |-- value: string (valueContainsNull = true)
* }}}
*
* @param params
* Parameter with custom configuration
*/
def xls(docPath: String): DataFrame = {
val excelReader = new ExcelReader(getTitleFontSize, getCellSeparator, getStoreContent)
excelReader.xls(docPath)
}
private def getCellSeparator: String = {
params.asScala.getOrElse("cellSeparator", "\t")
}
/** Instantiates class to read PowerPoint files.
*
* docPath: this is a path to a directory of Excel files or a path to an HTML file E.g.
* "path/power-point/files"
*
* ==Example==
* {{{
* val docsPath = "home/user/power-point-directory"
* val sparkNLPReader = new SparkNLPReader()
* val pptDf = sparkNLPReader.ppt(docsPath)
* }}}
*
* ==Example 2==
* You can use SparkNLP for one line of code
* {{{
* val pptDf = SparkNLP.read.ppt(docsPath)
* }}}
*
* {{{
* xlsDf.select("ppt").show(false)
* +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
* |ppt |
* +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
* |[{Title, Adding a Bullet Slide, {}}, {ListItem, • Find the bullet slide layout, {}}, {ListItem, – Use _TextFrame.text for first bullet, {}}, {ListItem, • Use _TextFrame.add_paragraph() for subsequent bullets, {}}, {NarrativeText, Here is a lot of text!, {}}, {NarrativeText, Here is some text in a text box!, {}}]|
* +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
*
* pptDf.printSchema()
* root
* |-- path: string (nullable = true)
* |-- content: binary (nullable = true)
* |-- ppt: array (nullable = true)
* | |-- element: struct (containsNull = true)
* | | |-- elementType: string (nullable = true)
* | | |-- content: string (nullable = true)
* | | |-- metadata: map (nullable = true)
* | | | |-- key: string
* | | | |-- value: string (valueContainsNull = true)
* }}}
*
* @param params
* Parameter with custom configuration
*/
def ppt(docPath: String): DataFrame = {
val powerPointReader = new PowerPointReader(getStoreContent)
powerPointReader.ppt(docPath)
}
/** Instantiates class to read txt files.
*
* filePath: this is a path to a directory of TXT files or a path to an TXT file E.g.
* "path/txt/files"
*
* ==Example==
* {{{
* val filePath = "home/user/txt/files"
* val sparkNLPReader = new SparkNLPReader()
* val txtDf = sparkNLPReader.txt(filePath)
* }}}
*
* ==Example 2==
* You can use SparkNLP for one line of code
* {{{
* val txtDf = SparkNLP.read.txt(filePath)
* }}}
*
* {{{
* txtDf.select("txt").show(false)
* +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
* |txt |
* +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
* |[{Title, BIG DATA ANALYTICS, {paragraph -> 0}}, {NarrativeText, Apache Spark is a fast and general-purpose cluster computing system.\nIt provides high-level APIs in Java, Scala, Python, and R., {paragraph -> 0}}, {Title, MACHINE LEARNING, {paragraph -> 1}}, {NarrativeText, Spark's MLlib provides scalable machine learning algorithms.\nIt includes tools for classification, regression, clustering, and more., {paragraph -> 1}}]|
* +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
*
* emailDf.printSchema()
* root
* |-- path: string (nullable = true)
* |-- content: binary (nullable = true)
* |-- txt: array (nullable = true)
* | |-- element: struct (containsNull = true)
* | | |-- elementType: string (nullable = true)
* | | |-- content: string (nullable = true)
* | | |-- metadata: map (nullable = true)
* | | | |-- key: string
* | | | |-- value: string (valueContainsNull = true)
* }}}
*
* @param params
* Parameter with custom configuration
*/
def txt(filePath: String): DataFrame = {
val textReader = new TextReader(getTitleLengthSize, getStoreContent)
textReader.txt(filePath)
}
private def getTitleLengthSize: Int = {
val titleLengthSize =
try {
params.asScala.getOrElse("titleLengthSize", "50").toInt
} catch {
case _: IllegalArgumentException => 50
}
titleLengthSize
}
}