@@ -35,7 +35,11 @@ def sample_from_iterable(x):
3535
3636@attr .s
3737class CommunityWalker (Walker ):
38- """Defines the community walking strategy.
38+ """Community walking strategy which groups vertices with similar properties
39+ through probabilities and relations that are not explicitly modeled in a
40+ Knowledge Graph. Similar to the Random walking strategy, the Depth First
41+ Search (DFS) algorithm is used if a maximum number of walks is specified.
42+ Otherwise, the Breath First Search (BFS) algorithm is chosen.
3943
4044 Attributes:
4145 _is_support_remote: True if the walking strategy can be used with a
@@ -127,27 +131,23 @@ def _community_detection(self, kg: KG) -> None:
127131 self .labels_per_community [self .communities [node ]].append (node )
128132
129133 def _bfs (
130- self , kg : KG , root : Vertex , is_reverse : bool = False
134+ self , kg : KG , entity : Vertex , is_reverse : bool = False
131135 ) -> List [Walk ]:
132- """Extracts random walks of depth - 1 hops rooted in root with
133- Breadth-first search .
136+ """Extracts random walks for an entity based on Knowledge Graph using
137+ the Depth First Search (DFS) algorithm .
134138
135139 Args:
136140 kg: The Knowledge Graph.
137-
138- The graph from which the neighborhoods are extracted for the
139- provided entities.
140- root: The root node to extract walks.
141+ entity: The root node to extract walks.
141142 is_reverse: True to get the parent neighbors instead of the child
142143 neighbors, False otherwise.
143144 Defaults to False.
144145
145146 Returns:
146- The list of walks for the root node according to the depth and
147- max_walks.
147+ The list of unique walks for the provided entity.
148148
149149 """
150- walks : Set [Walk ] = {(root ,)}
150+ walks : Set [Walk ] = {(entity ,)}
151151 for i in range (self .max_depth ):
152152 for walk in walks .copy ():
153153 if is_reverse :
@@ -201,31 +201,27 @@ def _bfs(
201201 return list (walks )
202202
203203 def _dfs (
204- self , kg : KG , root : Vertex , is_reverse : bool = False
204+ self , kg : KG , entity : Vertex , is_reverse : bool = False
205205 ) -> List [Walk ]:
206- """Extracts a random limited number of walks of depth - 1 hops rooted
207- in root with Depth-first search .
206+ """Extracts random walks for an entity based on Knowledge Graph using
207+ the Depth First Search (DFS) algorithm .
208208
209209 Args:
210210 kg: The Knowledge Graph.
211-
212- The graph from which the neighborhoods are extracted for the
213- provided entities.
214- root: The root node to extract walks.
211+ entity: The root node to extract walks.
215212 is_reverse: True to get the parent neighbors instead of the child
216213 neighbors, False otherwise.
217- Defaults to False
214+ Defaults to False.
218215
219216 Returns:
220- The list of walks for the root node according to the depth and
221- max_walks.
217+ The list of unique walks for the provided entity.
222218
223219 """
224220 self .sampler .visited = set ()
225221 walks : List [Walk ] = []
226222 assert self .max_walks is not None
227223 while len (walks ) < self .max_walks :
228- sub_walk : Walk = (root ,)
224+ sub_walk : Walk = (entity ,)
229225 d = 1
230226 while d // 2 < self .max_depth :
231227 pred_obj = self .sampler .sample_hop (
@@ -301,18 +297,15 @@ def extract(
301297 self ._community_detection (kg )
302298 return super ().extract (kg , entities , verbose )
303299
304- def extract_walks (self , kg : KG , root : Vertex ) -> List [Walk ]:
300+ def extract_walks (self , kg : KG , entity : Vertex ) -> List [Walk ]:
305301 """Extracts random walks of depth - 1 hops rooted in root.
306302
307303 Args:
308304 kg: The Knowledge Graph.
309-
310- The graph from which the neighborhoods are extracted for the
311- provided entities.
312- root: The root node to extract walks.
305+ entity: The root node to extract walks.
313306
314307 Returns:
315- The list of walks.
308+ The list of unique walks for the provided entity .
316309
317310 """
318311 if self .max_walks is None :
@@ -322,33 +315,30 @@ def extract_walks(self, kg: KG, root: Vertex) -> List[Walk]:
322315 if self .with_reverse :
323316 return [
324317 r_walk [:- 1 ] + walk
325- for walk in fct_search (kg , root )
326- for r_walk in fct_search (kg , root , is_reverse = True )
318+ for walk in fct_search (kg , entity )
319+ for r_walk in fct_search (kg , entity , is_reverse = True )
327320 ]
328- return [walk for walk in fct_search (kg , root )]
321+ return [walk for walk in fct_search (kg , entity )]
329322
330- def _extract (self , kg : KG , instance : Vertex ) -> EntityWalks :
331- """Extracts walks rooted at the provided entities which are then each
332- transformed into a numerical representation.
323+ def _extract (self , kg : KG , entity : Vertex ) -> EntityWalks :
324+ """Extracts random walks for an entity based on a Knowledge Graph.
333325
334326 Args:
335327 kg: The Knowledge Graph.
336- instance : The instance to be extracted from the Knowledge Graph .
328+ entity : The root node to extract walks .
337329
338330 Returns:
339- The 2D matrix with its number of rows equal to the number of
340- provided entities; number of column equal to the embedding size .
331+ A dictionary having the entity as key and a list of tuples as value
332+ corresponding to the extracted walks .
341333
342334 """
343335 canonical_walks : Set [SWalk ] = set ()
344- for walk in self .extract_walks (kg , instance ):
345- canonical_walk : List [str ] = []
346- for i , hop in enumerate (walk ):
347- if i == 0 or i % 2 == 1 or self .md5_bytes is None :
348- canonical_walk .append (hop .name )
349- else :
350- canonical_walk .append (
351- str (md5 (hop .name .encode ()).digest ()[: self .md5_bytes ])
352- )
336+ for walk in self .extract_walks (kg , entity ):
337+ canonical_walk : List [str ] = [
338+ vertex .name
339+ if i == 0 or i % 2 == 1 or self .md5_bytes is None
340+ else str (md5 (vertex .name .encode ()).digest ()[: self .md5_bytes ])
341+ for i , vertex in enumerate (walk )
342+ ]
353343 canonical_walks .add (tuple (canonical_walk ))
354- return {instance .name : list (canonical_walks )}
344+ return {entity .name : list (canonical_walks )}
0 commit comments