Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[analyzer] minor fixes #201

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions kalamine/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import threading
import webbrowser
from http.server import HTTPServer, SimpleHTTPRequestHandler
from importlib import metadata
from pathlib import Path
from xml.etree import ElementTree as ET

Expand Down Expand Up @@ -40,6 +41,10 @@ def main_page(layout: KeyboardLayout, angle_mod: bool = False) -> str:
<script>angle_mod = {"true" if angle_mod else "false"}; </script>
</head>
<body>
<p style="float: right; text-align: right;">
<a href="https://github.com/OneDeadKey/kalamine">kalamine</a>
v{metadata.version('kalamine')}<br>\U0001f986
</p>
<dl>
<dt>Name</dt>
<dd>{layout_ref}</dd>
Expand Down Expand Up @@ -77,6 +82,7 @@ def main_page(layout: KeyboardLayout, angle_mod: bool = False) -> str:
<option>en</option>
<option>en+fr</option>
<option>fr</option>
<option value="fra_mixed-typical_2012_1M-sentences">fr (Leipzig)</option>
</select>
<label for="corpus">corpus</label>
</form>
Expand Down Expand Up @@ -153,11 +159,10 @@ def __init__(self, *args, **kwargs) -> None: # type: ignore
super().__init__(*args, **kwargs)

def do_GET(self) -> None:
self.send_response(200)

def send(
page: str, content: str = "text/plain", charset: str = "utf-8"
) -> None:
self.send_response(200)
self.send_header("Content-type", f"{content}; charset={charset}")
# no cash as one is likely working live on it
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
Expand All @@ -170,9 +175,7 @@ def send(
# XXX always reloads the layout on the root page, never in sub pages
nonlocal kb_layout
nonlocal angle_mod
if self.path == "/favicon.ico":
pass
elif self.path == "/json":
if self.path == "/json":
send(web.pretty_json(kb_layout), content="application/json")
elif self.path == "/keylayout":
# send(keylayout.keylayout(kb_layout), content='application/xml')
Expand All @@ -196,7 +199,7 @@ def send(
kb_layout = KeyboardLayout(load_layout(file_path), angle_mod) # refresh
send(main_page(kb_layout, angle_mod), content="text/html")
else:
return SimpleHTTPRequestHandler.do_GET(self)
SimpleHTTPRequestHandler.do_GET(self)

webserver = HTTPServer((host_name, webserver_port), LayoutHandler)
thread = threading.Thread(None, webserver.serve_forever)
Expand Down
17 changes: 8 additions & 9 deletions kalamine/www/corpus/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ electronic works. Nearly all the individual works in the collection are in the
public domain in the United States. If an individual work is unprotected by
copyright law in the United States and you are located in the United States, we
do not claim a right to prevent you from copying, distributing, performing,
displaying or creating derivative works based on the work as long as all
references to Project Gutenberg are removed. Of course, we hope that you
will support the Project Gutenberg-tm mission of promoting free access
to electronic works by freely sharing Project Gutenberg-tm works in
compliance with the terms of this agreement for keeping the Project
Gutenberg-tm name associated with the work. You can easily comply with
the terms of this agreement by keeping this work in the same format with
its attached full Project Gutenberg-tm License when you share it without
charge with others.
displaying or creating derivative works based on the work as long as all
references to Project Gutenberg are removed. Of course, we hope that you will
support the Project Gutenberg-tm mission of promoting free access to electronic
works by freely sharing Project Gutenberg-tm works in compliance with the terms
of this agreement for keeping the Project Gutenberg-tm name associated with the
work. You can easily comply with the terms of this agreement by keeping this
work in the same format with its attached full Project Gutenberg-tm License when
you share it without charge with others.

[*] This particular work is one of the few individual works protected by
copyright law in the United States and most of the remainder of the world,
Expand Down
19 changes: 1 addition & 18 deletions kalamine/www/corpus/fra_mixed-typical_2012_1M-sentences.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"ä": 0.001,
"=": 0.001
},
"digrams": {
"bigrams": {
"es": 2.5852,
"le": 2.053,
"on": 1.8595,
Expand Down Expand Up @@ -793,9 +793,7 @@
"ln": 0.0014,
"xy": 0.0014,
"x?": 0.0013,
"â€": 0.0013,
"ml": 0.0013,
"€™": 0.0013,
"dû": 0.0013,
"»,": 0.0013,
"tn": 0.0013,
Expand Down Expand Up @@ -873,7 +871,6 @@
"i ": 0.0009,
"ït": 0.0009,
"r:": 0.0009,
"œu": 0.0009,
"kk": 0.0009,
"-q": 0.0009,
"k,": 0.0009,
Expand Down Expand Up @@ -939,7 +936,6 @@
"ïc": 0.0006,
"fp": 0.0006,
"€.": 0.0006,
"™e": 0.0006,
"à ": 0.0006,
"cg": 0.0006,
"fm": 0.0006,
Expand Down Expand Up @@ -1064,7 +1060,6 @@
"©t": 0.0004,
"td": 0.0004,
"jd": 0.0004,
"™a": 0.0004,
"œi": 0.0004,
"x:": 0.0004,
"dê": 0.0004,
Expand Down Expand Up @@ -1118,7 +1113,6 @@
".m": 0.0003,
".t": 0.0003,
"m´": 0.0003,
"cœ": 0.0003,
"y:": 0.0003,
"nœ": 0.0003,
"k!": 0.0003,
Expand Down Expand Up @@ -1163,7 +1157,6 @@
"lj": 0.0003,
"h!": 0.0003,
"\"v": 0.0003,
"€.": 0.0003,
"y!": 0.0003,
"/k": 0.0003,
"ür": 0.0003,
Expand Down Expand Up @@ -1327,7 +1320,6 @@
"ón": 0.0001,
"b:": 0.0001,
"nî": 0.0001,
"'œ": 0.0001,
"jr": 0.0001,
"zs": 0.0001,
"'ã": 0.0001,
Expand Down Expand Up @@ -1434,7 +1426,6 @@
",p": 0.0001,
"k'": 0.0001,
"xq": 0.0001,
"nœ": 0.0001,
"gã": 0.0001,
"&p": 0.0001,
"rď": 0.0001,
Expand Down Expand Up @@ -1488,7 +1479,6 @@
"ªm": 0.0001,
"ïw": 0.0001,
"wf": 0.0001,
"™ã": 0.0001,
"\"?": 0.0001,
"½t": 0.0001,
"nï": 0.0001,
Expand Down Expand Up @@ -1556,7 +1546,6 @@
"­s": 0.0001,
"«t": 0.0001,
"zw": 0.0001,
"™i": 0.0001,
"äi": 0.0001,
"sx": 0.0001,
"lö": 0.0001,
Expand Down Expand Up @@ -1618,7 +1607,6 @@
"vô": 0.0001,
",f": 0.0001,
"är": 0.0001,
"™h": 0.0001,
"é": 0.0001,
"bœ": 0.0001,
"§a": 0.0001,
Expand All @@ -1638,7 +1626,6 @@
"ló": 0.0001,
"qm": 0.0001,
"éï": 0.0001,
"œi": 0.0001,
":m": 0.0001,
"hg": 0.0001,
"üc": 0.0001,
Expand Down Expand Up @@ -1692,7 +1679,6 @@
"t(": 0.0001,
"r­": 0.0001,
"âi": 0.0001,
"™o": 0.0001,
"´t": 0.0001,
">s": 0.0001,
"“c": 0.0001,
Expand All @@ -1718,7 +1704,6 @@
":d": 0.0001,
"lď": 0.0001,
"m’": 0.0001,
"sœ": 0.0001,
"l¹": 0.0001,
":b": 0.0001,
"`u": 0.0001,
Expand Down Expand Up @@ -4864,7 +4849,6 @@
"gyp": 0.001,
"ww.": 0.001,
"âgé": 0.001,
"’": 0.001,
"oyo": 0.001,
"arp": 0.001,
"thl": 0.001,
Expand Down Expand Up @@ -5828,7 +5812,6 @@
"d&r": 0.001,
"iég": 0.001,
"thy": 0.001,
"€™e": 0.001,
"-hu": 0.001,
"lch": 0.001,
"mst": 0.001,
Expand Down
2 changes: 1 addition & 1 deletion kalamine/www/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ window.addEventListener('DOMContentLoaded', () => {

const keyboard = document.querySelector('x-keyboard');
const input = document.querySelector('input');
const geometry = document.querySelector('select');
const geometry = document.querySelector('#geometry');

if (!keyboard.layout) {
console.warn('web components are not supported');
Expand Down
Binary file added kalamine/www/favicon.ico
Binary file not shown.
8 changes: 4 additions & 4 deletions kalamine/www/mjs/layout-analyzer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const substituteChars = {
'\u00a0': ' ', // ( ) no-break space
'\u202f': ' ', // ( ) narrow no-break space
'\u00a0': ' ', // no-break space
'\u202f': ' ', // narrow no-break space

'\u00ab': '"', // («) left-pointing double angle quotation mark
'\u00bb': '"', // (») right-pointing double angle quotation mark
Expand Down Expand Up @@ -268,8 +268,8 @@ export function analyzeKeyboardLayout(
};

const isScissor = (kc1, kc2, finger1, finger2) => {
var finger1Height = getKeyRow(kc1);
var finger2Height = getKeyRow(kc2);
let finger1Height = getKeyRow(kc1);
let finger2Height = getKeyRow(kc2);

switch (finger1.at(1) + finger2.at(1)) {
case '45':
Expand Down
2 changes: 1 addition & 1 deletion kalamine/www/mjs/stats-canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class StatsCanvas extends HTMLElement {

const scale = canvas.height / maxValue;

const renderBarPart = (groupIndex, columnIndex, column, flipVerically) => {
const renderBarPart = (groupIndex, columnIndex, column) => {
let renderedBarHeight = 0;

const colors = Object.entries(this.colors);
Expand Down
26 changes: 9 additions & 17 deletions kalamine/www/mjs/stats-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class StatsTable extends HTMLElement {
super();
const shadow = this.attachShadow({ mode: 'open' });

// Stupid hack to get the height of a 'tr' element
// Stupid hack to get the height of a 'tr' element (XXX not working)
const tableRowElement = document.createElement('tr');
tableRowElement.innerHTML = 'random placeholder text';
shadow.appendChild(tableRowElement);
this.maxHeightCollapsed =
this.maxLinesCollapsed * tableRowElement.offsetHeight;
Math.max(180, this.maxLinesCollapsed * tableRowElement.offsetHeight);

// Actually build the content of the element (+ remove the stupid tr)
shadow.innerHTML = `
Expand Down Expand Up @@ -48,13 +48,11 @@ class StatsTable extends HTMLElement {
cursor: pointer;
clip-path: polygon(50% 100%, 0% 0%, 100% 0%);
}
button.showLess {
.showLess + button {
clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}
</style>

<!-- Using a style attribute on top of the stylesheet, as it is used by
the button 'click' event-listner -->
<div id='wrapper' style='max-height: ${this.maxHeightCollapsed}px;'></div>
<button style='display: none'></button>
`;
Expand All @@ -64,17 +62,11 @@ class StatsTable extends HTMLElement {
this.innerHTML = ''; // Remove original content

// Setting up the `see more` button
// Using 'function' to set 'this' to the button (self is the web component)
const self = this;
shadow.querySelector('button').addEventListener('click', function () {
const wrapper = shadow.getElementById('wrapper');
if (wrapper.style.maxHeight == `${self.maxHeightCollapsed}px`) {
wrapper.style.maxHeight = `${wrapper.children[0].offsetHeight}px`;
this.className = 'showLess';
} else {
wrapper.style.maxHeight = `${self.maxHeightCollapsed}px`;
this.className = '';
}
shadow.querySelector('button').addEventListener('click', () => {
wrapper.style.maxHeight = wrapper.className === ''
? `${wrapper.children[0].offsetHeight}px`
: `${this.maxHeightCollapsed}px`;
wrapper.classList.toggle('showLess');
});
}

Expand All @@ -84,7 +76,7 @@ class StatsTable extends HTMLElement {
table.innerHTML =
`<tr><th colspan='2'>${table.title}</td></tr>` +
Object.entries(values)
.filter(([digram, freq]) => freq >= 10 ** -precision)
.filter(([_, freq]) => freq >= 10 ** -precision)
.sort(([_, freq1], [__, freq2]) => freq2 - freq1)
.map(
([digram, freq]) =>
Expand Down
26 changes: 11 additions & 15 deletions kalamine/www/mjs/stats.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { getSupportedChars, analyzeKeyboardLayout } from './layout-analyzer.js';
import { analyzeKeyboardLayout } from './layout-analyzer.js';

window.addEventListener('DOMContentLoaded', () => {
const inputField = document.querySelector('input');
const keyboard = document.querySelector('x-keyboard');

const headingColor = 'rgb(127, 127, 127)'; // getComputedStyle(document.querySelector('h3')).color;

const keyboard = document.querySelector('x-keyboard');
let corpus = {};
let keyChars = {};
let impreciseData = false;

// display a percentage value
const fmtPercent = (num, p) => `${Math.round(10 ** p * num) / 10 ** p}%`;
Expand Down Expand Up @@ -57,10 +51,7 @@ window.addEventListener('DOMContentLoaded', () => {
};

const showReport = () => {
keyChars = getSupportedChars(keyboard.layout.keyMap, keyboard.layout.deadKeys);
const report = analyzeKeyboardLayout(keyboard, corpus, keyChars, headingColor);
console.log(corpus);
console.log(report);
const report = analyzeKeyboardLayout(keyboard, corpus);

document.querySelector('#sfu stats-canvas').renderData({
values: report.totalSfuSkuPerFinger,
Expand All @@ -82,7 +73,7 @@ window.addEventListener('DOMContentLoaded', () => {
showPercentAll('#load small', report.loadGroups.map(sumUpBarGroup), 1);
showPercent('#unsupported-all', report.totalUnsupportedChars, 3);

document.querySelector('#imprecise-data').hidden = !impreciseData;
document.querySelector('#imprecise-data').hidden = !report.impreciseData;

document
.querySelector('#bottlenecks stats-table')
Expand All @@ -94,11 +85,16 @@ window.addEventListener('DOMContentLoaded', () => {
document
.getElementById('corpus')
.addEventListener('change', event => {
fetch(`corpus/${event.target.value}.json`)
const corpusName = event.target.value;
const noCorpus = (corpusName === '-');
document.getElementById('analyzer').hidden = noCorpus;
if (noCorpus) {
return;
}
fetch(`corpus/${corpusName}.json`)
.then(response => response.json())
.then(data => {
corpus = data;
document.getElementById('analyzer').hidden = false;
showReport();
});
});
Expand Down
4 changes: 4 additions & 0 deletions kalamine/www/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ select {
text-wrap: balance;
}

#analyzer {
padding-bottom: 2em;
}

h2 {
border-bottom: 1px dotted currentcolor;
}
Expand Down
Loading