json Copy code
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Norme-AZERTY Simplified Overview\n",
"This notebook provides a simplified, Gurobi-free version of the Norme-AZERTY keyboard optimization functionality. This version is compatible with Google Colab."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 0
},
"id": "setup-cell"
},
"outputs": [],
"source": [
"# Setup for Google Colab\n",
"import codecs\n",
"import numpy as np\n",
"import pandas as pd\n",
"import unicodedata\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.patches as patches\n",
"from matplotlib import cm\n",
"from matplotlib import colors\n",
"\n",
"# Ensure UTF-8 Encoding\n",
"PYTHONIOENCODING = \"utf-8\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Core Utility Functions\n",
"def correct_diacritic(c):\n",
" unicode_diacritic = {u\"\\u0302\": u\"^d\", u\"\\u0308\": u\"¨d\", u\"\\u0303\": u\"~d\", u\"\\u0306\": u\"˘d\"}\n",
" return unicode_diacritic.get(c, c)\n",
"\n",
"def normalize_dict_values(d):\n",
" maximum = np.max(list(d.values()))\n",
" minimum = np.min(list(d.values()))\n",
" return {k: (v - minimum) / (maximum - minimum) for k, v in d.items()}\n",
"\n",
"def load_file_as_dict(path, key_col=0, val_col=1):\n",
" data = pd.read_csv(path, sep=\"\\t\", header=None, encoding=\"utf-8\")\n",
" return dict(zip(data[key_col], data[val_col]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load Input Data\n",
"def load_characters(path):\n",
" with open(path, encoding=\"utf-8\") as f:\n",
" return [correct_diacritic(line.strip()) for line in f.readlines()]\n",
"\n",
"def load_frequencies(path):\n",
" data = pd.read_csv(path, sep=\" \", header=0, encoding=\"utf-8\")\n",
" return data.set_index('letter')['frequency'].to_dict()\n",
"\n",
"characters = load_characters(\"./input/characters/characters_setFINAL.txt\")\n",
"frequencies = load_frequencies(\"./input/frequencies/frequency_letters_formal_setFINAL.txt\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Plot Keyboard Layout\n",
"def plot_keyboard(mapping, title=\"Keyboard Layout\"):\n",
" fig, ax = plt.subplots(figsize=(12, 6))\n",
" key_width, key_height = 4, 4\n",
" \n",
" for i, (char, pos) in enumerate(mapping.items()):\n",
" row, col = divmod(i, 10)\n",
" x, y = col * (key_width + 1), -row * (key_height + 1)\n",
" ax.add_patch(patches.Rectangle((x, y), key_width, key_height, fill=False))\n",
" ax.text(x + key_width / 2, y + key_height / 2, char, ha='center', va='center', fontsize=10)\n",
" \n",
" ax.set_xlim(0, 10 * (key_width + 1))\n",
" ax.set_ylim(-5 * (key_height + 1), 0)\n",
" ax.axis('off')\n",
" plt.title(title)\n",
" plt.show()\n",
"\n",
"# Example Usage\n",
"example_mapping = {char: f\"Row {i // 10 + 1}, Col {i % 10 + 1}\" for i, char in enumerate(characters)}\n",
"plot_keyboard(example_mapping)"
]
}
],
"metadata": {
"colab": {
"name": "norme_azerty_simple.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}