5757 " )"
5858 ]
5959 },
60+ {
61+ "cell_type" : " markdown" ,
62+ "metadata" : {},
63+ "source" : [
64+ " ### `create_dictionary_domain()`\n\n " ,
65+ " Creates a dictionary domain (a set of dictionaries) from scratch\n with all the possible variable types\n \n "
66+ ]
67+ },
68+ {
69+ "cell_type" : " code" ,
70+ "execution_count" : null ,
71+ "metadata" : {},
72+ "outputs" : [],
73+ "source" : [
74+ " # Imports\n " ,
75+ " import os\n " ,
76+ " from khiops import core as kh\n " ,
77+ " \n " ,
78+ " # Creates a Root dictionary\n " ,
79+ " root_dictionary = kh.Dictionary(\n " ,
80+ " json_data={\" name\" : \" dict_from_scratch\" , \" root\" : True, \" key\" : [\" Id\" ]}\n " ,
81+ " )\n " ,
82+ " \n " ,
83+ " # Starts with simple variables to declare\n " ,
84+ " simple_variables = [\n " ,
85+ " {\" name\" : \" Id\" , \" type\" : \" Categorical\" },\n " ,
86+ " {\" name\" : \" Num\" , \" type\" : \" Numerical\" },\n " ,
87+ " {\" name\" : \" text\" , \" type\" : \" Text\" },\n " ,
88+ " {\" name\" : \" hour\" , \" type\" : \" Time\" },\n " ,
89+ " {\" name\" : \" date\" , \" type\" : \" Date\" },\n " ,
90+ " {\" name\" : \" ambiguous_ts\" , \" type\" : \" Timestamp\" },\n " ,
91+ " {\" name\" : \" ts\" , \" type\" : \" TimestampTZ\" },\n " ,
92+ " ]\n " ,
93+ " for var_spec in simple_variables:\n " ,
94+ " var = kh.Variable()\n " ,
95+ " var.name = var_spec[\" name\" ]\n " ,
96+ " var.type = var_spec[\" type\" ]\n " ,
97+ " root_dictionary.add_variable(var)\n " ,
98+ " \n " ,
99+ " # Creates a second dictionary (not linked yet)\n " ,
100+ " second_dictionary = kh.Dictionary(\n " ,
101+ " json_data={\" name\" : \" Service\" , \" key\" : [\" Id\" , \" id_product\" ]}\n " ,
102+ " )\n " ,
103+ " second_dictionary.add_variable(\n " ,
104+ " kh.Variable(json_data={\" name\" : \" Id\" , \" type\" : \" Categorical\" })\n " ,
105+ " )\n " ,
106+ " second_dictionary.add_variable(\n " ,
107+ " kh.Variable(json_data={\" name\" : \" id_product\" , \" type\" : \" Categorical\" })\n " ,
108+ " )\n " ,
109+ " # Creates a third dictionary (not linked yet)\n " ,
110+ " third_dictionary = kh.Dictionary(json_data={\" name\" : \" Address\" , \" key\" : [\" Id\" ]})\n " ,
111+ " third_dictionary.add_variable(\n " ,
112+ " kh.Variable(json_data={\" name\" : \" StreetNumber\" , \" type\" : \" Numerical\" })\n " ,
113+ " )\n " ,
114+ " third_dictionary.add_variable(\n " ,
115+ " kh.Variable(json_data={\" name\" : \" StreetName\" , \" type\" : \" Categorical\" })\n " ,
116+ " )\n " ,
117+ " third_dictionary.add_variable(\n " ,
118+ " kh.Variable(json_data={\" name\" : \" id_city\" , \" type\" : \" Categorical\" })\n " ,
119+ " )\n " ,
120+ " \n " ,
121+ " # Adds the variables used in a Multi-tables context in the first dictionary.\n " ,
122+ " # They link the root dictionary to the additional ones\n " ,
123+ " root_dictionary.add_variable(\n " ,
124+ " kh.Variable(json_data={\" name\" : \" Services\" , \" type\" : \" Table(Service)\" })\n " ,
125+ " )\n " ,
126+ " root_dictionary.add_variable(\n " ,
127+ " kh.Variable(json_data={\" name\" : \" Address\" , \" type\" : \" Entity(Address)\" })\n " ,
128+ " )\n " ,
129+ " \n " ,
130+ " # Creates a DictionaryDomain (set of dictionaries)\n " ,
131+ " dictionary_domain = kh.DictionaryDomain()\n " ,
132+ " dictionary_domain.add_dictionary(root_dictionary)\n " ,
133+ " dictionary_domain.add_dictionary(second_dictionary)\n " ,
134+ " dictionary_domain.add_dictionary(third_dictionary)\n " ,
135+ " \n " ,
136+ " output_dir = os.path.join(\" kh_samples\" , \" create_dictionary_domain\" )\n " ,
137+ " dictionary_file_path = os.path.join(output_dir, \" dict_from_scratch.kdic\" )\n " ,
138+ " \n " ,
139+ " # Create the output directory if needed\n " ,
140+ " if not os.path.isdir(output_dir):\n " ,
141+ " os.mkdir(output_dir)\n " ,
142+ " \n " ,
143+ " # Writes the dictionary domain to a file\n " ,
144+ " with open(dictionary_file_path, \" w\" ) as report_file:\n " ,
145+ " report_file_writer = KhiopsOutputWriter(report_file)\n " ,
146+ " dictionary_domain.write(report_file_writer)"
147+ ]
148+ },
60149 {
61150 "cell_type" : " markdown" ,
62151 "metadata" : {},
23392428 "metadata" : {},
23402429 "nbformat" : 4 ,
23412430 "nbformat_minor" : 2
2342- }
2431+ }
0 commit comments