13 个版本 (5 个稳定版)
| 1.1.1 | 2022年7月24日 | 
|---|---|
| 1.0.2 | 2022年7月10日 | 
| 0.3.1 | 2022年7月8日 | 
| 0.2.2 | 2022年7月7日 | 
| 0.1.4 | 2022年7月3日 | 
#427 in WebAssembly
每月26次下载
705KB
 1K  SLoC
text-to-sounds
文本转声音解析工具。用于 Spoken Sounds Highlighter。

概述
该库提供了函数(parse、serialize)来将文本解析为 Vec<Sound>,并将 Vec<Sound> 序列化为 String。Sound 结构体包含关于英语发音的信息。highlight 函数可以向文本添加 html 标签,以便通过 css 在浏览器中突出显示声音。
如果您对 JavaScript(WASM)版本感兴趣,请移至下面的
Javascript / WASM部分。
use uuid::Uuid;
// English sound kinds
enum SoundKind {
    Ptk,
    Th,
    W,
    V,
    Ng,
    Ch,
    Dj,
    Undefined,
}
// Struct of the sound
pub struct Sound {
    id: Uuid,
    kind: SoundKind,
    text: String,
}
安装
为了使用此 crate,您必须在您的 [dependencies] 中将其添加到您的 Cargo.toml
[dependencies]
text-to-sounds = "1.1.1"
JavaScript / WASM
在 www 目录中,您可以找到网站 Spoken Sounds Highlighter 的源代码。它使用 wasm 版本的 highlight 函数。您也可以从 npm 获取它。
npm i --save text-to-sounds
并且使用
import {highlight_wasm} from "text-to-sounds";
// example #1
// "<span class='Th'>Th</span>e <span class='Ptk'>t</span>ex<span class='Ptk'>t</span> <span class='Dj'>j</span>us<span class='Ptk'>t</span> in <span class='Ptk'>c</span>ase"
const highlightedText = highlight_wasm("The text just in case");
// example #2
const contenteditableEl = document.getElementById('contenteditable');
contenteditableEl.innerHTML = highlight_wasm(contenteditableEl.textContent);
请考虑添加一些 css 样式给这些类,然后我们就完成了
.Ptk, .Th, .W, .V, .Ng, .Ch, .Dj {
    font-weight: 700;
}
.Ptk {
    color: #7F7EFF;
}
.Th {
    color: #A390E4;
}
.W {
    color: #C69DD2;
}
.V {
    color: #CC8B8C;
}
.Ng {
    color: #C68866;
}
.Ch {
    color: #417B5A;
}
.Dj {
    color: #4B3F72;
}
您可以在 Spoken Sounds Highlighter 网站的源代码中的 www 目录找到一个可工作的示例。
示例
use text_to_sounds::{parse, serialize, highlight, SoundKind, Sound};
let sounds = vec![
    Sound::new(SoundKind::Th, String::from("Th")),
    Sound::new(SoundKind::Undefined, String::from("e")),
    Sound::new(SoundKind::Undefined, String::from(" ")),
    Sound::new(SoundKind::Ptk, String::from("t")),
    Sound::new(SoundKind::Undefined, String::from("e")),
    Sound::new(SoundKind::Undefined, String::from("x")),
    Sound::new(SoundKind::Ptk, String::from("t")),
    Sound::new(SoundKind::Undefined, String::from(" ")),
    Sound::new(SoundKind::Dj, String::from("j")),
    Sound::new(SoundKind::Undefined, String::from("u")),
    Sound::new(SoundKind::Undefined, String::from("s")),
    Sound::new(SoundKind::Ptk, String::from("t")),
    Sound::new(SoundKind::Undefined, String::from(" ")),
    Sound::new(SoundKind::Undefined, String::from("i")),
    Sound::new(SoundKind::Undefined, String::from("n")),
    Sound::new(SoundKind::Undefined, String::from(" ")),
    Sound::new(SoundKind::Ptk, String::from("c")),
    Sound::new(SoundKind::Undefined, String::from("a")),
    Sound::new(SoundKind::Undefined, String::from("s")),
    Sound::new(SoundKind::Undefined, String::from("e")),
];
// parse
assert_eq!(parse("The text just in case"), sounds);
// serialize
assert_eq!(serialize(sounds), "The text just in case");
// highlight
assert_eq!(highlight("The text just in case"), "<span class='Th'>Th</span>e <span class='Ptk'>t</span>ex<span class='Ptk'>t</span> <span class='Dj'>j</span>us<span class='Ptk'>t</span> in <span class='Ptk'>c</span>ase".to_string());
此外,您还可以考虑文件中的测试。
依赖
~1–1.9MB
~36K SLoC