-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.py
69 lines (63 loc) · 1.47 KB
/
filter.py
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
import pyspark.sql.functions as F
from pyspark.sql import SparkSession
from pyspark import SparkConf
def process(sc, input_path, doi_path, max_words):
dois = (
sc.read.text(doi_path)
.select(
F.col("value").alias("doi")
)
)
return (
dois
.join(
(
# load parquet
sc.read.parquet(input_path)
# select columns
.select(
F.col("doi"),
F.col("content")
)
),
on="doi", how="inner"
)
.filter(F.size(F.split(F.col("content"), " ")) < max_words)
.select(
F.col("doi"),
F.col("content")
)
)
def run(sc, args):
# args
input_path = args[0]
output_path = args[1]
doi_path = args[2]
max_words = args[3]
# process
(
process(sc, input_path, doi_path, max_words)
.write
.mode('overwrite')
.parquet(output_path)
)
if __name__ == '__main__':
# args
INPUT = "stereo-grobid-preprocessed.parquet/*"
OUTPUT = "grobid-filtered.parquet"
DOI_PATH = "grobid_dois.txt"
MAX_WORDS = 65_000
# spark session
spark = (
SparkSession
.builder
.config(conf=SparkConf())
.getOrCreate()
)
# process
(
process(spark, INPUT, DOI_PATH, MAX_WORDS)
.write
.mode('overwrite')
.parquet(OUTPUT)
)