@@ -74,6 +74,7 @@ def candidate_subclass(
74
74
table_name : Optional [str ] = None ,
75
75
cardinality : Optional [int ] = None ,
76
76
values : Optional [List [Any ]] = None ,
77
+ nullables : Optional [List [bool ]] = None ,
77
78
) -> Type [Candidate ]:
78
79
"""Create new relation.
79
80
@@ -95,6 +96,10 @@ def candidate_subclass(
95
96
:param cardinality: The cardinality of the variable corresponding to the
96
97
Candidate. By default is 2 i.e. is a binary value, e.g. is or is not
97
98
a true mention.
99
+ :param values: A list of values a candidate can take as their label.
100
+ :param nullables: The number of nullables must match that of args.
101
+ If nullables[i]==True, a mention for ith mention subclass can be NULL.
102
+ If nullables=``None`` (by default), no mention can be NULL.
98
103
"""
99
104
if table_name is None :
100
105
table_name = camel_to_under (class_name )
@@ -124,6 +129,12 @@ def candidate_subclass(
124
129
elif cardinality is not None :
125
130
values = list (range (cardinality ))
126
131
132
+ if nullables :
133
+ if len (nullables ) != len (args ):
134
+ raise ValueError ("The number of nullables must match that of args." )
135
+ else :
136
+ nullables = [False ] * len (args )
137
+
127
138
class_spec = (args , table_name , cardinality , values )
128
139
if class_name in candidate_subclasses :
129
140
if class_spec == candidate_subclasses [class_name ][1 ]:
@@ -153,6 +164,7 @@ def candidate_subclass(
153
164
# Helper method to get argument names
154
165
"__argnames__" : [_ .__tablename__ for _ in args ],
155
166
"mentions" : args ,
167
+ "nullables" : nullables ,
156
168
}
157
169
class_attribs ["document_id" ] = Column (
158
170
Integer , ForeignKey ("document.id" , ondelete = "CASCADE" )
@@ -166,10 +178,12 @@ def candidate_subclass(
166
178
# Create named arguments, i.e. the entity mentions comprising the
167
179
# relation mention.
168
180
unique_args = []
169
- for arg in args :
181
+ for arg , nullable in zip ( args , nullables ) :
170
182
# Primary arguments are constituent Contexts, and their ids
171
183
class_attribs [arg .__tablename__ + "_id" ] = Column (
172
- Integer , ForeignKey (arg .__tablename__ + ".id" , ondelete = "CASCADE" )
184
+ Integer ,
185
+ ForeignKey (arg .__tablename__ + ".id" , ondelete = "CASCADE" ),
186
+ nullable = nullable ,
173
187
)
174
188
class_attribs [arg .__tablename__ ] = relationship (
175
189
arg .__name__ ,
0 commit comments