Added basic spellchecker

master
Mark 2022-07-20 21:31:14 -07:00
parent 65b5bb6400
commit cbea2cd663
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
5 changed files with 9956 additions and 1 deletions

File diff suppressed because it is too large Load Diff

View File

@ -27,6 +27,23 @@
// 0x02: FFT Animation
#define CMD_SEND_STATE 0x02
// Sent by keyboard to host when a complete word is typed.
// Host checks if this is a known word.
// If it is not, host responds with the same CMD (see below).
//
// Packet structure (sent by keyboard):
// Data: | cmd | word length | keycodes |
// # of Bytes: | 1 | 1 | ? |
//
// word length: number of bytes in `keycodes` block
//
//
// Packet structure (sent by host):
// Data: | cmd | typo? |
// # of Bytes: | 1 | 1 |
//
// typo: If this is 0x01, the word we got was a typo.
#define CMD_SPELLCHECK_WORD 0x04
// Animation data. Sent by host.
//

16
src/dict.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "dict.hpp"
std::unordered_set<std::string> word_dict;
void load_file() {
std::ifstream file("resources/google-10000-english-usa-no-swears.txt");
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
// using printf() in all tests for consistency
//printf("%s", line.c_str());
word_dict.insert(line.c_str());
}
file.close();
}
}

10
src/dict.hpp Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include <fstream>
#include <unordered_set>
#include <string>
extern std::unordered_set<std::string> word_dict;
void load_file();

View File

@ -11,6 +11,8 @@
// MPD client
#include "mpd/client.h"
#include <string>
// Local files
#include "utility/bitmap.hpp"
#include "utility/buffer.hpp"
@ -21,6 +23,7 @@
#include "spdlog/spdlog.h"
#include "dict.hpp"
// TODO:
//
@ -58,6 +61,11 @@ const size_t height = BOTTOM_SKIP + KB_RESOLUTION + TOP_SKIP;
int main(int argc, char *argv[]) {
spdlog::info("Loading dictionary...");
load_file();
spdlog::info("done!");
spdlog::set_level(spdlog::level::info);
// buffer size for waveform:
@ -147,7 +155,24 @@ int main(int argc, char *argv[]) {
uint8_t cmd = Dox.read_buf[0];
switch(cmd) {
case CMD_SEND_STATE:
case CMD_SPELLCHECK_WORD:
char word_chars[Dox.read_buf[1] + 1];
for (int i=0; i < Dox.read_buf[1]; i++) {
// A in ascii:
// a in ascii: 0x61
// KC_A: 0x04
word_chars[i] = Dox.read_buf[i + 2] + 0x5D;
}
word_chars[Dox.read_buf[1]] = 0x00; // Terminate with null char
std::string word = std::string(word_chars);
if (word_dict.find(word) == word_dict.end()) {
hid_buf[0] = 0x01;
Dox.write(CMD_SPELLCHECK_WORD, hid_buf, Dox.packet_size);
spdlog::info("Got typo: \"{0:s}\" not in dict", word);
}
break;
}
}