MCPcopy Create free account
hub / github.com/google-research/language / load_kb

Method load_kb

language/emql/data_loader.py:118–212  ·  view source on GitHub ↗

r"""Load KB from file. We store KB as a dictionary of dictionaries, where keys are subjects and values are dictionaries of facts. Each dictionary of facts is constructed with relations as keys and objects as values. Input formats are different in different tasks. In the "webqsp" ta

(
      self, name, kb_file
  )

Source from the content-addressed store, hash-verified

116 self.construct_dataset(name, train_split=TRAIN_SPLIT)
117
118 def load_kb(
119 self, name, kb_file
120 ):
121 r"""Load KB from file.
122
123 We store KB as a dictionary of dictionaries, where keys are subjects and
124 values are dictionaries of facts. Each dictionary of facts is constructed
125 with relations as keys and objects as values.
126
127 Input formats are different in different tasks. In the "webqsp" task,
128 subject, relation, and object entities are split by "\t".
129
130 Args:
131 name: task name
132 kb_file: kb file
133
134 Returns:
135 a dictionary of dictionaries, and mappings from fact/relation/entity to id
136 """
137
138 def get_delimiter():
139 if name == 'webqsp' or name.startswith('query2box'):
140 return '\t'
141 else:
142 return '|'
143
144 def set_has_inverse_fact():
145 if name == 'webqsp':
146 return False
147 else:
148 return True
149
150 def set_reorder():
151 if name == 'webqsp':
152 return lambda ps: (ps[1], ps[0], ps[2])
153 else:
154 return lambda ps: ps
155
156 def set_inv_suffix():
157 if name.startswith('query2box'):
158 suffix = '_reverse'
159 else:
160 suffix = '-inv'
161 return suffix
162
163 # Prepare for loading the KB.
164 self._inv_suffix = set_inv_suffix()
165 delimiter = get_delimiter()
166 has_inverse_fact = set_has_inverse_fact()
167 reorder = set_reorder()
168
169 kb = dict()
170 fact2id = dict()
171 entity2id = dict()
172 relation2id = dict()
173 with gfile.GFile(kb_file, 'r') as f_in:
174 for line in tqdm(f_in):
175 subj, rel, obj = reorder(tuple(line.strip().split(delimiter)))

Callers 1

__init__Method · 0.95

Calls 3

_inverse_relMethod · 0.95
splitMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected