Hyphen js+wasm test add

This commit is contained in:
Flexus
2022-09-15 15:14:01 +03:00
parent 4716b633e2
commit 34a2091dfc
7 changed files with 160 additions and 18 deletions

View File

@ -2,4 +2,7 @@ hyphen
test.pro.user
*.dic
msvc_make.bat
build-test-*
build-test-*
*.wasm
hyphen.js
hyphen.data

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<form>
<textarea id = "textarea"></textarea>
<button type = "submit">OK</button>
</form>
<script src = "deploy/hyphen.js"></script>
<script src = "main.js"></script>
</body>
</html>

View File

@ -0,0 +1,72 @@
/**
* @param {Module} module
* @param {Number} length
* @return {Number}
* Allocate memory for wasm, returns pointer
*/
function allocateMemory(module, length) {
const ptr = module._malloc(length);
return ptr;
}
/**
* @param {Module} module
* @param {Number} ptr
* @return {void}
* Free memory
*/
function freeMemory(module, ptr) {
module._free(ptr);
}
/**
* @param {Module} module
* @param {Uint8Array} data
* @param {Number} ptr
* @return {void}
* Fill memory
*/
function setMemory(module, data, ptr) {
module.HEAP8.set(data, ptr);
}
/**
* @type {HTMLTextAreaElement}
*/
var textarea = document.getElementById("textarea");
var form = document.querySelector("form");
function hyphenate(word)
{
var len = word.length;
var word_size = 4 * len + 1;
var hyphen_size = word_size + 5;
var hword_size = word_size * 2;
var pword = allocateMemory(Module, word_size);
var phyphens = allocateMemory(Module, hyphen_size);
var phword = allocateMemory(Module, hword_size);
Module.stringToUTF8(word, pword, word_size);
Module._hyphenate(pword, phyphens, phword)
var hword = Module.UTF8ToString(phword);
freeMemory(Module, pword);
freeMemory(Module, phyphens);
freeMemory(Module, phword);
return hword;
}
form.onsubmit = function(event) {
event.preventDefault();
var text = textarea.value.split("\n").join(" ").split(" ");
Module._load_dictionary();
for(var i = 0; i < text.length; i++) {
console.log(hyphenate(text[i]));
}
Module._free_dictionary();
}

View File

@ -0,0 +1,25 @@
#include "exported_functions.h"
#include <iostream>
HyphenDict *dict;
void load_dictionary()
{
dict = hnj_hyphen_load("dictionaries/hyph_ru_RU.dic");
}
void free_dictionary()
{
hnj_hyphen_free(dict);
}
void hyphenate(const char *word, char *hyphens, char *hword)
{
int n = strlen(word);
char **rep = NULL;
int *pos = NULL;
int *cut = NULL;
hnj_hyphen_hyphenate2(dict, word, n, hyphens, hword, &rep, &pos, &cut);
}
// emcc src/exported_functions.cpp ../hyphen/hyphen.c ../hyphen/hnjalloc.c -o deploy/effects.js -sEXPORTED_FUNCTIONS='_malloc','_free','_hyphenate','_load_dictionary','_free_dictionary' -sEXPORTED_RUNTIME_METHODS='stringToUTF8','UTF8ToString' --preload-file dictionaries

View File

@ -0,0 +1,16 @@
#include <fstream>
#include "./../../hyphen/hyphen.h"
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
void load_dictionary();
void free_dictionary();
void hyphenate(const char *word, char *hyphens, char *hword);
#ifdef __cplusplus
}
#endif // __cplusplus

View File

@ -0,0 +1,10 @@
button {
width: 60px;
height: 30px;
}
#textarea{
display: block;
width: 300px;
height: 300px;
}

View File

@ -12,7 +12,7 @@ int main(int argc, char *argv[])
std::string result_filename = PRO_DIR;
// set your filenames here
dict_filename += "hyph_ru_RU.dic";
dict_filename += "hyph_en_US.dic";
words_filename += "words.txt";
result_filename += "result.txt";
@ -50,27 +50,27 @@ int main(int argc, char *argv[])
* @brief
* input data:
*
* word: input word
* word_size: byte length of the input word
* hyphens: allocated character buffer (size = word_size + 5)
* hyphenated_word: allocated character buffer (size ~ word_size * 2) or NULL
* rep, pos, cut: pointers (point to the allocated and _zeroed_ buffers
* (size=word_size) or with NULL value) or NULL
* word: input word
* word_size: byte length of the input word
* hyphens: allocated character buffer (size = word_size + 5)
* hyphenated_word: allocated character buffer (size ~ word_size * 2) or NULL
* rep, pos, cut: pointers (point to the allocated and _zeroed_ buffers
* (size=word_size) or with NULL value) or NULL
*
* output data:
*
* hyphens: hyphenation vector (hyphenation points signed with odd numbers).
* hyphenated_word: hyphenated input word (hyphens signed with `=').
* optional (NULL input).
* rep: NULL (only standard hyph.), or replacements (hyphenation points
* signed with `=' in replacements).
* pos: NULL, or difference of the actual position and the beginning
* positions of the change in input words.
* cut: NULL, or counts of the removed characters of the original words
* at hyphenation.
* hyphens: hyphenation vector (hyphenation points signed with odd numbers).
* hyphenated_word: hyphenated input word (hyphens signed with `=').
* optional (NULL input).
* rep: NULL (only standard hyph.), or replacements (hyphenation points
* signed with `=' in replacements).
* pos: NULL, or difference of the actual position and the beginning
* positions of the change in input words.
* cut: NULL, or counts of the removed characters of the original words
* at hyphenation.
*
* Note: rep, pos, cut are complementary arrays to the hyphens, indexed with the
* character positions of the input word.
* character positions of the input word.
*/
hnj_hyphen_hyphenate2(dict, word.c_str(), n, hyphens, hword, &rep, &pos, &cut);