-
Notifications
You must be signed in to change notification settings - Fork 1
/
align.py
167 lines (148 loc) · 5 KB
/
align.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
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
import pyspark.sql.functions as F
import pyspark.sql.types as T
from pyspark.sql import SparkSession
from pyspark import SparkConf
from subprocess import Popen, PIPE, STDOUT
import json
def wrapper(n: int = 8, overlap: int = 7, theta: int = 300, text: bool = False, cmd_path="./alignment"):
return_text = {
True: "--text=true",
False: "--text=false"
}[text]
return_context = {
True: "--context=true",
False: "--context=false"
}[text]
def map_func(res, source, target, return_text=True):
if return_text:
return list(map(lambda x: (
int(x["source"].get("begin", 0)),
int(x["source"].get("end", 0)),
x["source"].get("text", ""),
x["source"].get("before", ""),
x["source"].get("after", ""),
len(source),
int(x["target"].get("begin", 0)),
int(x["target"].get("end", 0)),
x["target"].get("text", ""),
x["target"].get("before", ""),
x["target"].get("after", ""),
len(target)
), res))
else:
return list(map(lambda x: (
int(x["source"].get("begin", 0)),
int(x["source"].get("end", 0)),
int(x["target"].get("begin", 0)),
int(x["target"].get("end", 0))
), res))
def inner(source: str, target: str):
msg = json.dumps({
"source": source,
"target": target,
"config": json.dumps({
"seeder": {"hash": {"n": n, "overlap": overlap}},
"extender": {"range": {"theta": theta}}
})
})
stdout = ""
try:
p = Popen([cmd_path, "read", return_text, return_context], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
stdout, _ = p.communicate(input=msg.encode("utf-8"))
p.kill()
stdout = json.loads(stdout.decode("utf-8"))
return list(map_func(stdout, source, target))
except Exception as e:
return [(0, 0, stdout, "", "", 0, 0, 0, stdout, "", "", 0)]
return inner
def align(source, target, wrapper):
return wrapper(source, target)
def process(sc, path, align_cmd):
# create candidate pair dataframe
df = (
# load parquet
sc.read.parquet(path)
.select(
F.col("doi_a"),
F.col("doi_b"),
F.col("text_a"),
F.col("text_b")
)
)
df = (
df
.withColumn(
"seeds",
F.udf(
lambda source, target: align(source, target, align_cmd),
T.ArrayType(T.StructType([
T.StructField("begin_a", T.IntegerType(), False),
T.StructField("end_a", T.IntegerType(), False),
T.StructField("text_a", T.StringType(), True),
T.StructField("before_a", T.StringType(), True),
T.StructField("after_a", T.StringType(), True),
T.StructField("doc_length_a", T.IntegerType(), True),
T.StructField("begin_b", T.IntegerType(), False),
T.StructField("end_b", T.IntegerType(), False),
T.StructField("text_b", T.StringType(), True),
T.StructField("before_b", T.StringType(), True),
T.StructField("after_b", T.StringType(), True),
T.StructField("doc_length_b", T.IntegerType(), True),
]))
)("text_a", "text_b")
)
.drop("text_a", "text_b")
)
df = (
df
.filter(F.size("seeds") > 0)
.withColumn(
"seeds",
F.explode("seeds")
)
.select(
F.col("doi_a"),
F.col("doi_b"),
F.col("seeds.*")
)
)
return df
def run(sc, args):
# args
input_path = args[0]
output_path = args[1]
ngram_length = int(args[2])
overlap = int(args[3])
theta = int(args[4])
# process
cmd = wrapper(n=ngram_length, overlap=overlap, theta=theta, text=True, cmd_path="./jobs/alignment")
# apply
df = process(sc, input_path, cmd)
df.show(vertical=True, truncate=True)
df.write.mode("overwrite").parquet(output_path)
if __name__ == '__main__':
# args
INPUT = "stereo-joined.parquet/"
OUTPUT = "stereo-aligned.parquet"
BATCH = "00"
SUBBATCH = ""
NGRAM_LENGTH = 8
NGRAM_OVERLAP = 7
THETA = 250
CMD = wrapper(n=NGRAM_LENGTH, overlap=NGRAM_OVERLAP, theta=THETA, text=True, cmd_path="./alignment")
# spark session
spark = (
SparkSession
.builder
.config(conf=SparkConf())
.getOrCreate()
)
# process
input_batched = INPUT + BATCH + "/part-*"+SUBBATCH+"-*.parquet"
output_batched = OUTPUT + "/" + BATCH
(
process(spark, input_batched, CMD)
.write
.mode('overwrite')
.parquet(output_batched)
)