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 | class TokenBlocker(SchemaAgnosticBlocker):
"""Concatenates and tokenizes entity attribute values and blocks on tokens.
Examples
--------
>>> # doctest: +SKIP
>>> from sylloge import MovieGraphBenchmark
>>> from klinker.data import KlinkerDataset
>>> ds = KlinkerDataset.from_sylloge(MovieGraphBenchmark(),clean=True)
>>> from klinker.blockers import TokenBlocker
>>> blocker = TokenBlocker()
>>> blocks = blocker.assign(left=ds.left, right=ds.right)
"""
def __init__(
self,
tokenize_fn: Callable[[str], List[str]] = word_tokenize,
stop_words: Optional[List[str]] = None,
min_token_length: int = 3,
):
self.tokenizer = FilteredTokenizer(
tokenize_fn=tokenize_fn,
min_token_length=min_token_length,
stop_words=stop_words,
)
def _create_exploded_token_frame(self, tab):
tok_kwargs: Dict[str, Any] = {
"return_set": True,
}
if isinstance(tab, dd.Series):
tok_kwargs["meta"] = (tab.name, "O")
return (
tab.apply(self.tokenizer.tokenize, **tok_kwargs)
.explode()
.dropna()
.to_frame()
.reset_index()
)
def _tok_block(self, tab: SeriesType) -> Frame:
"""Perform token blocking on this series.
Args:
----
tab: SeriesType: series on which token blocking should be done.
Returns:
-------
token blocked series.
"""
name = tab.name
id_col_name = tab.index.name
# TODO figure out why this hack is needed
# i.e. why does dask assume later for the join, that this is named 0
# no matter what it is actually named
tok_name = "tok"
collect_ids_kwargs = {"id_col": id_col_name}
if isinstance(tab, dd.Series):
collect_ids_kwargs["meta"] = pd.Series(
[],
name=tab.name,
dtype="O",
index=pd.Series([], dtype="O", name=tok_name),
)
return (
self._create_exploded_token_frame(tab)
.rename(columns={name: tok_name}) # avoid same name for col and index
.groupby(tok_name)
.apply(lambda x, id_col: list(set(x[id_col])), **collect_ids_kwargs)
.to_frame(name=name)
)
def _assign(
self,
left: SeriesType,
right: SeriesType,
left_rel: Optional[KlinkerFrame] = None,
right_rel: Optional[KlinkerFrame] = None,
) -> KlinkerBlockManager:
"""Assign entity ids to blocks.
Args:
----
left: KlinkerFrame: Contains entity attribute information of left dataset.
right: KlinkerFrame: Contains entity attribute information of right dataset.
left_rel: Optional[KlinkerFrame]: (Default value = None) Contains relational information of left dataset.
right_rel: Optional[KlinkerFrame]: (Default value = None) Contains relational information of left dataset.
Returns:
-------
KlinkerBlockManager: instance holding the resulting blocks.
"""
left_tok = self._tok_block(left)
right_tok = self._tok_block(right)
left_tok.to_parquet("/tmp/tb_left_blocks.parquet")
right_tok.to_parquet("/tmp/tb_right_blocks.parquet")
pd_blocks = left_tok.join(right_tok, how="inner")
if isinstance(pd_blocks, dd.DataFrame):
return KlinkerBlockManager(pd_blocks)
return KlinkerBlockManager.from_pandas(pd_blocks)
|