(input_table, LABEL_DICT, TYPE_DICT, CLASS_DICT)
| 14 | from Usem import fill_zero |
| 15 | |
| 16 | def computeColumnSemantics(input_table, LABEL_DICT, TYPE_DICT, CLASS_DICT): |
| 17 | col_id = 0 |
| 18 | not_found_in_yago = [] |
| 19 | column_dictionary = {} |
| 20 | for columnName in input_table.columns: |
| 21 | if genFunc.getColumnType(input_table[columnName].tolist()) == 1: #check column Type |
| 22 | input_table[columnName] = input_table[columnName].map(str) |
| 23 | #get unique values in the column and preprocess them. |
| 24 | value_list = genFunc.preprocessListValues(input_table[columnName].unique()) |
| 25 | #search values in KB |
| 26 | all_found_types = {} |
| 27 | total_kb_hits = 0 |
| 28 | for value in value_list: |
| 29 | current_entities = set() |
| 30 | current_types = set() |
| 31 | |
| 32 | current_entities = LABEL_DICT.get(value, "None") |
| 33 | if current_entities != "None": #found in KB |
| 34 | total_kb_hits += 1 |
| 35 | for entity in current_entities: |
| 36 | if entity in TYPE_DICT: |
| 37 | temp_type = TYPE_DICT[entity] |
| 38 | for entity_type in temp_type: |
| 39 | current_types.add(entity_type) |
| 40 | for each_type in current_types: |
| 41 | if each_type in all_found_types: |
| 42 | all_found_types[each_type] +=1 |
| 43 | else: |
| 44 | all_found_types[each_type] = 1 |
| 45 | else: |
| 46 | not_found_in_yago.append(value) |
| 47 | |
| 48 | #find the top-level type with highest count. |
| 49 | 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] |
| 50 | if all_top_types: |
| 51 | selected_top_type = all_top_types[0][0] |
| 52 | top_type_count = all_top_types[0][1] |
| 53 | children_of_top_types = CLASS_DICT[selected_top_type] |
| 54 | #add children of top types to the bag of word |
| 55 | for each in all_found_types: |
| 56 | if each in children_of_top_types and (all_found_types[each] / top_type_count) >= 0: |
| 57 | if columnName not in column_dictionary: |
| 58 | column_dictionary[columnName] = [each] |
| 59 | else: |
| 60 | column_dictionary[columnName].append(each) |
| 61 | col_id += 1 |
| 62 | |
| 63 | return column_dictionary#处理一下,改成一个DataFrame |
| 64 | |
| 65 | |
| 66 | # 定义处理单个表格的函数 |
no test coverage detected