Skip to content

Commit 22e90f0

Browse files
Refactors import statements across multiple modules for improved organization and consistency. Updates argument and predicate filtering functions to follow naming conventions. Enhances test files by ensuring compatibility with the original PredPatt implementation and improving readability. Additionally, minor formatting adjustments and code cleanups are applied throughout the codebase.
1 parent 10b1348 commit 22e90f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1917
-1905
lines changed

decomp/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import os
21
import importlib.resources
2+
import os
3+
from logging import DEBUG, basicConfig
34

4-
from logging import basicConfig, DEBUG
55

66
# get the data directory using importlib.resources
77
DATA_DIR = str(importlib.resources.files('decomp') / 'data')
88
basicConfig(filename=os.path.join(DATA_DIR, 'build.log'),
99
filemode='w',
1010
level=DEBUG)
1111

12-
from .semantics.uds import UDSCorpus
13-
from .semantics.uds import NormalizedUDSAnnotation
14-
from .semantics.uds import RawUDSAnnotation
12+
from .semantics.uds import NormalizedUDSAnnotation, RawUDSAnnotation, UDSCorpus

decomp/corpus/corpus.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""Module for defining abstract graph corpus readers"""
22

33
from abc import ABCMeta, abstractmethod
4-
5-
from random import sample
4+
from collections.abc import Hashable, Iterator
65
from logging import warning
7-
from typing import Hashable, TypeVar, Iterator, Generic, TypeAlias
6+
from random import sample
7+
from typing import Generic, TypeAlias, TypeVar
8+
89

910
InGraph = TypeVar('InGraph') # the input graph type
1011
OutGraph = TypeVar('OutGraph') # the output graph type
@@ -60,19 +61,17 @@ def _graphbuilder(self,
6061

6162
@property
6263
def graphs(self) -> dict[Hashable, OutGraph]:
63-
"""the graphs in corpus"""
64+
"""The graphs in corpus"""
6465
return self._graphs
6566

6667
@property
6768
def graphids(self) -> list[Hashable]:
6869
"""The graph ids in corpus"""
69-
7070
return list(self._graphs)
7171

7272
@property
7373
def ngraphs(self) -> int:
7474
"""Number of graphs in corpus"""
75-
7675
return len(self._graphs)
7776

7877
def sample(self, k: int) -> dict[Hashable, OutGraph]:
@@ -83,6 +82,5 @@ def sample(self, k: int) -> dict[Hashable, OutGraph]:
8382
k
8483
the number of graphs to sample
8584
"""
86-
8785
sampled_keys = sample(list(self._graphs.keys()), k=k)
8886
return {tid: self._graphs[tid] for tid in sampled_keys}

decomp/graph/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Module for converting between NetworkX and RDFLib graphs"""
22

3-
from .rdf import RDFConverter
43
from .nx import NXConverter
4+
from .rdf import RDFConverter
5+
56

6-
__all__ = ['RDFConverter', 'NXConverter']
7+
__all__ = ['NXConverter', 'RDFConverter']

decomp/graph/nx.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Module for converting from networkx to RDF"""
22

3-
from networkx import DiGraph, to_dict_of_dicts
4-
from rdflib import Graph, URIRef, Literal
3+
from networkx import DiGraph
4+
from rdflib import Graph
55

66

77
class NXConverter:
@@ -26,7 +26,6 @@ def rdf_to_networkx(cls, rdfgraph: Graph) -> DiGraph:
2626
rdfgraph
2727
the RDFLib graph to convert
2828
"""
29-
3029
converter = cls(rdfgraph)
3130

3231
raise NotImplementedError

decomp/graph/rdf.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""Module for converting from networkx to RDF"""
22

33
from typing import Any
4+
45
from networkx import DiGraph, to_dict_of_dicts
5-
from rdflib import Graph, URIRef, Literal
6+
from rdflib import Graph, Literal, URIRef
67

78

89
class RDFConverter:
@@ -35,7 +36,6 @@ def networkx_to_rdf(cls, nxgraph: DiGraph) -> Graph:
3536
nxgraph
3637
the NetworkX graph to convert
3738
"""
38-
3939
converter = cls(nxgraph)
4040

4141
nxdict = to_dict_of_dicts(nxgraph)
@@ -50,44 +50,44 @@ def networkx_to_rdf(cls, nxgraph: DiGraph) -> Graph:
5050

5151
def _add_node_attributes(self, nodeid: str) -> None:
5252
self._construct_node(nodeid)
53-
53+
5454
self._add_attributes(nodeid,
5555
list(self.nxgraph.nodes[nodeid].items()))
5656

57-
57+
5858
def _add_edge_attributes(self, nodeid1: str, nodeid2: str) -> None:
5959
edgeid = self._construct_edge(nodeid1, nodeid2)
6060
edgetup = (nodeid1, nodeid2)
61-
61+
6262
self._add_attributes(edgeid,
6363
list(self.nxgraph.edges[edgetup].items()))
64-
64+
6565

6666
def _add_attributes(self, nid: str, attributes: list[tuple[str, Any]]) -> None:
6767
triples = []
68-
68+
6969
for attrid1, attrs1 in attributes:
7070
if not isinstance(attrs1, dict):
7171
if isinstance(attrs1, list) or isinstance(attrs1, tuple):
7272
errmsg = 'Cannot convert list- or tuple-valued' +\
7373
' attributes to RDF'
7474
raise ValueError(errmsg)
75-
75+
7676
triples += self._construct_property(nid,
7777
attrid1,
7878
attrs1)
7979

80-
else:
80+
else:
8181
for attrid2, attrs2 in attrs1.items():
8282
triples += self._construct_property(nid,
8383
attrid2,
8484
attrs2,
8585
attrid1)
8686

8787
for t in triples:
88-
self.rdfgraph.add(t)
89-
90-
def _construct_node(self, nodeid: str) -> None:
88+
self.rdfgraph.add(t)
89+
90+
def _construct_node(self, nodeid: str) -> None:
9191
if nodeid not in self.nodes:
9292
self.nodes[nodeid] = URIRef(nodeid)
9393

@@ -102,7 +102,7 @@ def _construct_edge(self, nodeid1: str, nodeid2: str) -> str:
102102
triple = (node1, self.nodes[edgeid], node2)
103103

104104
self.rdfgraph.add(triple)
105-
105+
106106
return edgeid
107107

108108
else:
@@ -113,15 +113,15 @@ def _construct_property(self, nodeid: str, propid: str, val: Any,
113113

114114
c = self.__class__
115115
triples: list[tuple[URIRef, URIRef, URIRef | Literal]]
116-
116+
117117
if isinstance(val, dict) and subspaceid is not None:
118118
# We currently do not support querying on raw UDS
119119
# annotations, all of which have dict-valued 'value'
120120
# and 'confidence' fields.
121121
if isinstance(val['value'], dict) or isinstance(val['confidence'], dict):
122122
raise TypeError('Attempted query of graph with raw properties. Querying '\
123123
'graphs with raw properties is prohibited.')
124-
triples = c._construct_subspace(subspaceid, propid)
124+
triples = c._construct_subspace(subspaceid, propid)
125125
triples += [(self.nodes[nodeid],
126126
c.PROPERTIES[propid],
127127
Literal(val['value'])),
@@ -132,26 +132,26 @@ def _construct_property(self, nodeid: str, propid: str, val: Any,
132132
elif propid in ['domain', 'type']:
133133
if val not in c.VALUES:
134134
c.VALUES[val] = URIRef(val)
135-
135+
136136
triples = [(self.nodes[nodeid],
137137
c.PROPERTIES[propid],
138138
c.VALUES[val])]
139139

140140
else:
141141
if propid not in c.PROPERTIES:
142142
c.PROPERTIES[propid] = URIRef(propid)
143-
143+
144144
triples = [(self.nodes[nodeid],
145145
c.PROPERTIES[propid],
146-
Literal(val))]
147-
146+
Literal(val))]
147+
148148
return triples
149149

150150
@classmethod
151151
def _construct_subspace(cls, subspaceid: str, propid: str) -> list[tuple[URIRef, URIRef, URIRef | Literal]]:
152152
if subspaceid not in cls.SUBSPACES:
153153
cls.SUBSPACES[subspaceid] = URIRef(subspaceid)
154-
154+
155155
if propid not in cls.PROPERTIES:
156156
cls.PROPERTIES[propid] = URIRef(propid)
157157
cls.PROPERTIES[propid+'-confidence'] = URIRef(propid+'-confidence')
@@ -161,7 +161,7 @@ def _construct_subspace(cls, subspaceid: str, propid: str) -> list[tuple[URIRef,
161161
cls.SUBSPACES[subspaceid]),
162162
(cls.PROPERTIES[propid+'-confidence'],
163163
cls.PROPERTIES['subspace'],
164-
cls.SUBSPACES[subspaceid]),
164+
cls.SUBSPACES[subspaceid]),
165165
(cls.PROPERTIES[propid],
166166
cls.PROPERTIES['confidence'],
167167
cls.PROPERTIES[propid+'-confidence'])]

decomp/semantics/predpatt/core/argument.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ def __init__(
5757
self,
5858
root: Token,
5959
ud: Any = dep_v1,
60-
rules: list[Any] = [], # NOTE: Mutable default to match original
61-
# WARNING: This mutable default is INTENTIONAL and REQUIRED
62-
# for exact compatibility with original PredPatt.
63-
# Instances share the same list when rules is not provided.
64-
# DO NOT CHANGE to None - this would break compatibility!
60+
rules: list[Any] | None = None,
6561
share: bool = False
6662
) -> None:
6763
"""Initialize an Argument.
@@ -80,7 +76,7 @@ def __init__(
8076
"""
8177
# maintain exact initialization order as original
8278
self.root = root
83-
self.rules = rules # intentionally using mutable default
79+
self.rules = rules if rules is not None else []
8480
self.position = root.position
8581
self.ud = ud
8682
self.tokens: list[Token] = []

decomp/semantics/predpatt/core/predicate.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,14 @@ def is_broken(self) -> bool | None:
260260
return True
261261
return None
262262

263-
def _format_predicate(self, name: dict[Any, str], C: Any = no_color) -> str:
263+
def _format_predicate(self, name: dict[Any, str], c: Any = no_color) -> str:
264264
"""Format predicate with argument placeholders.
265265
266266
Parameters
267267
----------
268268
name : dict[Any, str]
269269
Mapping from arguments to their names.
270-
C : callable, optional
270+
c : callable, optional
271271
Color function for formatting.
272272
273273
Returns
@@ -276,7 +276,7 @@ def _format_predicate(self, name: dict[Any, str], C: Any = no_color) -> str:
276276
Formatted predicate string.
277277
"""
278278
# collect tokens and arguments
279-
X = sort_by_position(self.tokens + self.arguments)
279+
x = sort_by_position(self.tokens + self.arguments)
280280

281281
if self.type == POSS:
282282
# possessive format: "?a 's ?b"
@@ -295,7 +295,7 @@ def _format_predicate(self, name: dict[Any, str], C: Any = no_color) -> str:
295295
if gov_arg:
296296
# format: gov_arg is/are other_tokens_and_args
297297
rest = []
298-
for item in X:
298+
for item in x:
299299
if item == gov_arg:
300300
continue
301301
if item in self.arguments:
@@ -306,7 +306,7 @@ def _format_predicate(self, name: dict[Any, str], C: Any = no_color) -> str:
306306
return f'{name[gov_arg]} is/are {rest_str}'
307307
else:
308308
# fallback if no governor argument found
309-
return ' '.join(name[item] if item in self.arguments else item.text for item in X)
309+
return ' '.join(name[item] if item in self.arguments else item.text for item in x)
310310

311311
else:
312312
# normal predicate or xcomp special case
@@ -317,7 +317,7 @@ def _format_predicate(self, name: dict[Any, str], C: Any = no_color) -> str:
317317
self.root.tag not in {postag.VERB, postag.ADJ}):
318318
# add is/are after first argument
319319
first_arg_added = False
320-
for item in X:
320+
for item in x:
321321
if item in self.arguments:
322322
result.append(name[item])
323323
if not first_arg_added:
@@ -327,7 +327,7 @@ def _format_predicate(self, name: dict[Any, str], C: Any = no_color) -> str:
327327
result.append(item.text)
328328
else:
329329
# normal formatting
330-
for item in X:
330+
for item in x:
331331
if item in self.arguments:
332332
result.append(name[item])
333333
else:
@@ -338,7 +338,7 @@ def _format_predicate(self, name: dict[Any, str], C: Any = no_color) -> str:
338338
def format(
339339
self,
340340
track_rule: bool = False,
341-
C: Any = no_color,
341+
c: Any = no_color,
342342
indent: str = '\t'
343343
) -> str:
344344
"""Format predicate with arguments for display.
@@ -347,7 +347,7 @@ def format(
347347
----------
348348
track_rule : bool, optional
349349
Whether to include rule tracking information.
350-
C : callable, optional
350+
c : callable, optional
351351
Color function for formatting.
352352
indent : str, optional
353353
Indentation string to use.
@@ -362,24 +362,24 @@ def format(
362362
verbose = ''
363363
if track_rule:
364364
rules_str = ','.join(sorted(map(str, self.rules)))
365-
verbose = ' ' + C(f'[{self.root.text}-{self.root.gov_rel},{rules_str}]', 'magenta')
365+
verbose = ' ' + c(f'[{self.root.text}-{self.root.gov_rel},{rules_str}]', 'magenta')
366366

367-
pred_str = self._format_predicate(argument_names(self.arguments), C)
367+
pred_str = self._format_predicate(argument_names(self.arguments), c)
368368
lines.append(f'{indent}{pred_str}{verbose}')
369369

370370
# format arguments
371371
name = argument_names(self.arguments)
372372
for arg in self.arguments:
373373
if (arg.isclausal() and arg.root.gov in self.tokens and
374374
self.type == NORMAL):
375-
s = C('SOMETHING', 'yellow') + ' := ' + arg.phrase()
375+
s = c('SOMETHING', 'yellow') + ' := ' + arg.phrase()
376376
else:
377-
s = C(arg.phrase(), 'green')
377+
s = c(arg.phrase(), 'green')
378378
rule = ''
379379
if track_rule:
380380
rules_str = ','.join(sorted(map(str, arg.rules)))
381381
rule = f',{rules_str}'
382-
verbose = C(f' [{arg.root.text}-{arg.root.gov_rel}{rule}]',
382+
verbose = c(f' [{arg.root.text}-{arg.root.gov_rel}{rule}]',
383383
'magenta')
384384
else:
385385
verbose = ''

0 commit comments

Comments
 (0)