| 245 | |
| 246 | #yago column semantics for query table |
| 247 | def computeColumnSemantics(input_table, subject_index, LABEL_DICT, TYPE_DICT, CLASS_DICT, RELATION_DICT, scoring_function): |
| 248 | col_id = 0 |
| 249 | not_found_in_yago = [] |
| 250 | column_bag_of_words = [] |
| 251 | column_dictionary = {} |
| 252 | subject_semantics = "" |
| 253 | for (columnName, columnData) in input_table.iteritems(): |
| 254 | if genFunc.getColumnType(input_table[columnName].tolist()) == 1: #check column Type |
| 255 | input_table[columnName] = input_table[columnName].map(str) |
| 256 | #get unique values in the column and preprocess them. |
| 257 | value_list = genFunc.preprocessListValues(input_table[columnName].unique()) |
| 258 | #search values in KB |
| 259 | all_found_types = {} |
| 260 | total_kb_hits = 0 |
| 261 | if str(subject_index) == str(col_id): |
| 262 | label = "sc" |
| 263 | else: |
| 264 | label = "c" |
| 265 | for value in value_list: |
| 266 | current_entities = set() |
| 267 | current_types = set() |
| 268 | current_entities = RELATION_DICT.get(str(col_id) + "_"+ value, "None") |
| 269 | #print(current_entities) |
| 270 | if current_entities != "None": |
| 271 | total_kb_hits += 1 |
| 272 | for entity in current_entities: |
| 273 | if entity in TYPE_DICT: |
| 274 | temp_type = TYPE_DICT[entity] |
| 275 | for entity_type in temp_type: |
| 276 | current_types.add(entity_type) |
| 277 | for each_type in current_types: |
| 278 | if each_type in all_found_types: |
| 279 | all_found_types[each_type] +=1 |
| 280 | else: |
| 281 | all_found_types[each_type] = 1 |
| 282 | |
| 283 | else: |
| 284 | current_entities = LABEL_DICT.get(value, "None") |
| 285 | if current_entities != "None": #found in KB |
| 286 | total_kb_hits += 1 |
| 287 | for entity in current_entities: |
| 288 | if entity in TYPE_DICT: |
| 289 | temp_type = TYPE_DICT[entity] |
| 290 | for entity_type in temp_type: |
| 291 | current_types.add(entity_type) |
| 292 | for each_type in current_types: |
| 293 | if each_type in all_found_types: |
| 294 | all_found_types[each_type] +=1 |
| 295 | else: |
| 296 | all_found_types[each_type] = 1 |
| 297 | else: |
| 298 | not_found_in_yago.append(value) |
| 299 | |
| 300 | #find the top-level type with highest count. |
| 301 | all_top_types = [v for v in sorted(all_found_types.items(), key=lambda kv: (-kv[1], kv[0])) if v[0] in CLASS_DICT] |
| 302 | if all_top_types: |
| 303 | selected_top_type = all_top_types[0][0] |
| 304 | top_type_count = all_top_types[0][1] |