|
57 | 57 | ")" |
58 | 58 | ] |
59 | 59 | }, |
| 60 | + { |
| 61 | + "cell_type": "markdown", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "### `create_dictionary_domain()`\n\n", |
| 65 | + "Creates a dictionary domain from scratch\n\n This dictionary domain contains a set of dictionaries,\n with all 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 | + "# Create 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 | + "# Start 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 | + "# Create a second dictionary\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 | + "# Create a third dictionary\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 | + "# Add the variables used in a multi-table 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 | + "# Create 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 | + "# Write the dictionary domain to a file\n", |
| 144 | + "dictionary_domain.export_khiops_dictionary_file(dictionary_file_path)" |
| 145 | + ] |
| 146 | + }, |
60 | 147 | { |
61 | 148 | "cell_type": "markdown", |
62 | 149 | "metadata": {}, |
|
0 commit comments