|
14 | 14 | import khiops |
15 | 15 | import os |
16 | 16 | from khiops import core as kh |
| 17 | +from khiops.core import KhiopsOutputWriter |
17 | 18 |
|
18 | 19 | # Disable open files without encoding because samples are simple code snippets |
19 | 20 | # pylint: disable=unspecified-encoding |
@@ -65,6 +66,85 @@ def build_dictionary_from_data_table(): |
65 | 66 | ) |
66 | 67 |
|
67 | 68 |
|
| 69 | +def create_dictionary_domain(): |
| 70 | + """Creates a dictionary domain (a set of dictionaries) from scratch |
| 71 | + with all the possible variable types |
| 72 | + """ |
| 73 | + # Imports |
| 74 | + import os |
| 75 | + from khiops import core as kh |
| 76 | + |
| 77 | + # Creates a Root dictionary |
| 78 | + root_dictionary = kh.Dictionary( |
| 79 | + json_data={"name": "dict_from_scratch", "root": True, "key": ["Id"]} |
| 80 | + ) |
| 81 | + |
| 82 | + # Starts with simple variables to declare |
| 83 | + simple_variables = [ |
| 84 | + {"name": "Id", "type": "Categorical"}, |
| 85 | + {"name": "Num", "type": "Numerical"}, |
| 86 | + {"name": "text", "type": "Text"}, |
| 87 | + {"name": "hour", "type": "Time"}, |
| 88 | + {"name": "date", "type": "Date"}, |
| 89 | + {"name": "ambiguous_ts", "type": "Timestamp"}, |
| 90 | + {"name": "ts", "type": "TimestampTZ"}, |
| 91 | + ] |
| 92 | + for var_spec in simple_variables: |
| 93 | + var = kh.Variable() |
| 94 | + var.name = var_spec["name"] |
| 95 | + var.type = var_spec["type"] |
| 96 | + root_dictionary.add_variable(var) |
| 97 | + |
| 98 | + # Creates a second dictionary (not linked yet) |
| 99 | + second_dictionary = kh.Dictionary( |
| 100 | + json_data={"name": "Service", "key": ["Id", "id_product"]} |
| 101 | + ) |
| 102 | + second_dictionary.add_variable( |
| 103 | + kh.Variable(json_data={"name": "Id", "type": "Categorical"}) |
| 104 | + ) |
| 105 | + second_dictionary.add_variable( |
| 106 | + kh.Variable(json_data={"name": "id_product", "type": "Categorical"}) |
| 107 | + ) |
| 108 | + # Creates a third dictionary (not linked yet) |
| 109 | + third_dictionary = kh.Dictionary(json_data={"name": "Address", "key": ["Id"]}) |
| 110 | + third_dictionary.add_variable( |
| 111 | + kh.Variable(json_data={"name": "StreetNumber", "type": "Numerical"}) |
| 112 | + ) |
| 113 | + third_dictionary.add_variable( |
| 114 | + kh.Variable(json_data={"name": "StreetName", "type": "Categorical"}) |
| 115 | + ) |
| 116 | + third_dictionary.add_variable( |
| 117 | + kh.Variable(json_data={"name": "id_city", "type": "Categorical"}) |
| 118 | + ) |
| 119 | + |
| 120 | + # Adds the variables used in a Multi-tables context in the first dictionary. |
| 121 | + # They link the root dictionary to the additional ones |
| 122 | + root_dictionary.add_variable( |
| 123 | + kh.Variable(json_data={"name": "Services", "type": "Table(Service)"}) |
| 124 | + ) |
| 125 | + root_dictionary.add_variable( |
| 126 | + kh.Variable(json_data={"name": "Address", "type": "Entity(Address)"}) |
| 127 | + ) |
| 128 | + |
| 129 | + # Creates a DictionaryDomain (set of dictionaries) |
| 130 | + dictionary_domain = kh.DictionaryDomain() |
| 131 | + dictionary_domain.add_dictionary(root_dictionary) |
| 132 | + dictionary_domain.add_dictionary(second_dictionary) |
| 133 | + dictionary_domain.add_dictionary(third_dictionary) |
| 134 | + |
| 135 | + output_dir = os.path.join("kh_samples", "create_dictionary_domain") |
| 136 | + dictionary_file_path = os.path.join(output_dir, "dict_from_scratch.kdic") |
| 137 | + |
| 138 | + # Create the output directory if needed |
| 139 | + if not os.path.isdir(output_dir): |
| 140 | + os.mkdir(output_dir) |
| 141 | + |
| 142 | + # Writes the dictionary domain to a file |
| 143 | + with open(dictionary_file_path, "w") as report_file: |
| 144 | + report_file_writer = KhiopsOutputWriter(report_file) |
| 145 | + dictionary_domain.write(report_file_writer) |
| 146 | + |
| 147 | + |
68 | 148 | def detect_data_table_format(): |
69 | 149 | """Detects the format of a data table with and without a dictionary file |
70 | 150 |
|
@@ -1987,6 +2067,7 @@ def build_deployed_dictionary(): |
1987 | 2067 | exported_samples = [ |
1988 | 2068 | get_khiops_version, |
1989 | 2069 | build_dictionary_from_data_table, |
| 2070 | + create_dictionary_domain, |
1990 | 2071 | detect_data_table_format, |
1991 | 2072 | check_database, |
1992 | 2073 | export_dictionary_files, |
|
0 commit comments