(input_table, tab_id, LABEL_DICT, FACT_DICT)
| 146 | |
| 147 | #compute KB RS for the query table |
| 148 | def computeRelationSemantics(input_table, tab_id, LABEL_DICT, FACT_DICT): |
| 149 | relation_bag_of_words = [] |
| 150 | total_cols = input_table.shape[1] |
| 151 | #total_rows = input_table.shape[0] |
| 152 | relation_dependencies = [] |
| 153 | entities_finding_relation = {} |
| 154 | relation_dictionary = {} |
| 155 | total_hits = {} |
| 156 | #compute relation semantics |
| 157 | for i in range(0, total_cols-1): |
| 158 | #print("i=",i) |
| 159 | if genFunc.getColumnType(input_table.iloc[:, i].tolist()) == 1: |
| 160 | #the subject in rdf triple should be a text column |
| 161 | for j in range(i+1, total_cols): |
| 162 | semantic_dict_forward = {} |
| 163 | semantic_dict_backward = {} |
| 164 | #print("j=",j) |
| 165 | column_pairs = input_table.iloc[:, [i, j]] |
| 166 | column_pairs = (column_pairs.drop_duplicates()).dropna() |
| 167 | unique_rows_in_pair = column_pairs.shape[0] |
| 168 | total_kb_forward_hits = 0 |
| 169 | total_kb_backward_hits = 0 |
| 170 | #print(column_pairs) |
| 171 | #assign relation semantic to each value pair of i and j |
| 172 | for k in range(0, unique_rows_in_pair): |
| 173 | #print(k) |
| 174 | #extract subject and object |
| 175 | found_relation = 0 |
| 176 | subject_value = genFunc.preprocessString(str(column_pairs.iloc[k][0]).lower()) |
| 177 | object_value = genFunc.preprocessString(str(column_pairs.iloc[k][1]).lower()) |
| 178 | is_sub_null = genFunc.checkIfNullString(subject_value) |
| 179 | is_obj_null = genFunc.checkIfNullString(object_value) |
| 180 | if is_sub_null != 0: |
| 181 | sub_entities = LABEL_DICT.get(subject_value, "None") |
| 182 | if sub_entities != "None": |
| 183 | if is_obj_null != 0: |
| 184 | obj_entities = LABEL_DICT.get(object_value, "None") |
| 185 | if obj_entities != "None": |
| 186 | #As both are not null, search for relation semantics |
| 187 | for sub_entity in sub_entities: |
| 188 | for obj_entity in obj_entities: |
| 189 | #preparing key to search in the fact file |
| 190 | entity_forward = sub_entity + "__" + obj_entity |
| 191 | entity_backward = obj_entity + "__" + sub_entity |
| 192 | relation_forward = FACT_DICT.get(entity_forward, "None") |
| 193 | relation_backward = FACT_DICT.get(entity_backward, "None") |
| 194 | if relation_forward != "None": |
| 195 | found_relation = 1 |
| 196 | total_kb_forward_hits += 1 |
| 197 | #keep track of the entity finding relation. We will use this to speed up the column semantics search |
| 198 | key = str(i)+"_"+subject_value |
| 199 | if key not in entities_finding_relation: |
| 200 | entities_finding_relation[key] = {sub_entity} |
| 201 | else: |
| 202 | entities_finding_relation[key].add(sub_entity) |
| 203 | key = str(j) + "_" + object_value |
| 204 | if key not in entities_finding_relation: |
| 205 | entities_finding_relation[key] = {obj_entity} |
no test coverage detected