适用于多种人类语言的分词器、文本清理器和音素化工具。
项目描述
Gruut
支持SSML(语音合成标记语言)的多种人类语言的分词器、文本清理器和国际音标音素化工具。
from gruut import sentences
text = 'He wound it around the wound, saying "I read it was $10 to read."'
for sent in sentences(text, lang="en-us"):
for word in sent:
if word.phonemes:
print(word.text, *word.phonemes)
输出结果
He h ˈi
wound w ˈaʊ n d
it ˈɪ t
around ɚ ˈaʊ n d
the ð ə
wound w ˈu n d
, |
saying s ˈeɪ ɪ ŋ
I ˈaɪ
read ɹ ˈɛ d
it ˈɪ t
was w ə z
ten t ˈɛ n
dollars d ˈɑ l ɚ z
to t ə
read ɹ ˈi d
. ‖
请注意,“wound”和“read”在不同的(语法)语境中具有不同的发音。
也支持SSML的一个子集
from gruut import sentences
ssml_text = """<?xml version="1.0" encoding="ISO-8859-1"?>
<speak version="1.1" xmlns="http://www.w3.org/2001/10/synthesis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2001/10/synthesis
http://www.w3.org/TR/speech-synthesis11/synthesis.xsd"
xml:lang="en-US">
<s>Today at 4pm, 2/1/2000.</s>
<s xml:lang="it">Un mese fà, 2/1/2000.</s>
</speak>"""
for sent in sentences(ssml_text, ssml=True):
for word in sent:
if word.phonemes:
print(sent.idx, word.lang, word.text, *word.phonemes)
输出结果
0 en-US Today t ə d ˈeɪ
0 en-US at ˈæ t
0 en-US four f ˈɔ ɹ
0 en-US P p ˈi
0 en-US M ˈɛ m
0 en-US , |
0 en-US February f ˈɛ b j u ˌɛ ɹ i
0 en-US first f ˈɚ s t
0 en-US , |
0 en-US two t ˈu
0 en-US thousand θ ˈaʊ z ə n d
0 en-US . ‖
1 it Un u n
1 it mese ˈm e s e
1 it fà f a
1 it , |
1 it due d j u
1 it gennaio d͡ʒ e n n ˈa j o
1 it duemila d u e ˈm i l a
1 it . ‖
有关详细信息,请参阅文档。
安装
pip install gruut
在安装过程中可以添加除英语之外的语言。例如,支持法语和意大利语
pip install -f 'https://synesthesiam.github.io/prebuilt-apps/' gruut[fr,it]
需要额外的pip仓库来更新num2words分支,其中包含对更多语言的支持。
您还可以手动下载语言文件并将其放在$XDG_CONFIG_HOME/gruut/
(默认为$HOME/.config/gruut
)。
gruut将在目录$XDG_CONFIG_HOME/gruut/<lang>/
中查找语言文件,如果未安装相应的Python包。请注意,这里的<lang>
是完整的语言名称,例如de-de
而不是仅仅是de
。
支持的语言
gruut目前支持
- 阿拉伯语(
ar
) - 捷克语(
cs
或cs-cz
) - 德语(
de
或de-de
) - 英语(
en
或en-us
) - 西班牙语(《es》或《es-es》)
- 波斯语/阿拉伯语(《fa》)
- 法语(《fr》或《fr-fr》)
- 意大利语(《it》或《it-it》)
- 卢森堡语(《lb》)
- 荷兰语(《nl》)
- 俄语(《ru》或《ru-ru》)
- 瑞典语(《sv》或《sv-se》)
- 斯瓦希里语(《sw》)
目标是支持voice2json的所有语言
依赖关系
- Python 3.7或更高版本
- Linux
- 已在Debian Bullseye上测试
- num2words分支和Babel
- 货币/数字处理
- num2words分支包括额外的语言支持(阿拉伯语、波斯语、瑞典语、斯瓦希里语)
- gruut-ipa
- 国际音标发音操作
- pycrfsuite
- 词性标注和字符到音素的模型的一部分
- pydateparser
- 多语言日期解析
数字、日期等
gruut
可以自动将数字、日期和其他表达式转换为口头表达。这种转换既适用于解析也适用于口头表达,因此“1/1/2020”可能被解释为“M/D/Y”或“D/M/Y”,具体取决于单词或句子的语言(例如,<s lang="...">
)。
gruut
可以自动将以下类型的表达式扩展为单词
- 数字 - "123"到"one hundred and twenty three"(通过
verbalize_numbers=False
或--no-numbers
禁用)- 依赖于
Babel
进行解析和num2words
进行口头表达
- 依赖于
- 日期 - "1/1/2020"到"January first, twenty twenty"(通过
verbalize_dates=False
或--no-dates
禁用)- 依赖于
pydateparser
进行解析和Babel
和num2words
进行口头表达
- 依赖于
- 货币 - "$10"到"ten dollars"(通过
verbalize_currency=False
或--no-currency
禁用)- 依赖于
Babel
进行解析和Babel
和num2words
进行口头表达
- 依赖于
- 时间 - "12:01am"到"twelve oh one A M"(通过
verbalize_times=False
或--no-times
禁用)- 仅英语
- 依赖于
num2words
进行口头表达
命令行使用
可以使用python3 -m gruut --language <LANGUAGE> <TEXT>
或使用gruut
命令(来自setup.py
)执行gruut
模块。
gruut
命令是面向行的,消耗文本并产生JSONL。您可能需要安装jq来操作来自gruut
的JSONL输出。
纯文本
接受原始文本并输出包含清理后的单词/标记的JSONL。
echo 'This, right here, is some "RAW" text!' \
| gruut --language en-us \
| jq --raw-output '.words[].text'
This
,
right
here
,
is
some
"
RAW
"
text
!
更多详细信息可在完整的JSON输出中找到
gruut --language en-us 'More text.' | jq .
输出
{
"idx": 0,
"text": "More text.",
"text_with_ws": "More text.",
"text_spoken": "More text",
"par_idx": 0,
"lang": "en-us",
"voice": "",
"words": [
{
"idx": 0,
"text": "More",
"text_with_ws": "More ",
"leading_ws": "",
"training_ws": " ",
"sent_idx": 0,
"par_idx": 0,
"lang": "en-us",
"voice": "",
"pos": "JJR",
"phonemes": [
"m",
"ˈɔ",
"ɹ"
],
"is_major_break": false,
"is_minor_break": false,
"is_punctuation": false,
"is_break": false,
"is_spoken": true,
"pause_before_ms": 0,
"pause_after_ms": 0
},
{
"idx": 1,
"text": "text",
"text_with_ws": "text",
"leading_ws": "",
"training_ws": "",
"sent_idx": 0,
"par_idx": 0,
"lang": "en-us",
"voice": "",
"pos": "NN",
"phonemes": [
"t",
"ˈɛ",
"k",
"s",
"t"
],
"is_major_break": false,
"is_minor_break": false,
"is_punctuation": false,
"is_break": false,
"is_spoken": true,
"pause_before_ms": 0,
"pause_after_ms": 0
},
{
"idx": 2,
"text": ".",
"text_with_ws": ".",
"leading_ws": "",
"training_ws": "",
"sent_idx": 0,
"par_idx": 0,
"lang": "en-us",
"voice": "",
"pos": null,
"phonemes": [
"‖"
],
"is_major_break": true,
"is_minor_break": false,
"is_punctuation": false,
"is_break": true,
"is_spoken": false,
"pause_before_ms": 0,
"pause_after_ms": 0
}
],
"pause_before_ms": 0,
"pause_after_ms": 0
}
对于整个输入行和每个单词,text
属性包含经过处理的输入文本,其中包含规范化的空白,而text_with_ws
保留原始空白。仅包含口头表达单词的text_spoken
属性,因此不包括标点符号和分隔符。
在单词内部
idx
- 句子中单词的零基索引sent_idx
- 输入文本中句子的零基索引pos
- 词性标签(如果可用)phonemes
- 单词的国际音标音素列表(如果可用)is_minor_break
- 如果“单词”分隔短语(逗号、分号等),则返回true
is_major_break
- 如果“单词”分隔句子(句号、问号等),则返回true
is_break
- 如果“单词”是主要或次要的分隔符,则返回true
is_punctuation
- 如果“单词”是周围的标点符号(引号、括号等),则返回true
is_spoken
- 如果不是分隔符或标点符号,则返回true
有关更多信息,请参阅python3 -m gruut <LANGUAGE> --help
。
SSML
支持 SSML(Synchronized Multimedia Integration Language)的一个子集
<speak>
- 包裹 SSML 文本lang
- 设置文档语言
<p>
- 段落lang
- 设置段落语言
<s>
- 句子(禁用自动句子断句)lang
- 设置句子语言
<w>
/<token>
- 单词(禁用自动分词)lang
- 设置单词语言role
- 设置单词角色(见单词角色)
<lang lang="...">
- 设置内部文本的语言<voice name="...">
- 设置内部文本的语音<say-as interpret-as="">
- 强制解释内部文本interpret-as
可以是 "spell-out", "date", "number", "time", 或 "currency"format
- 根据interpret-as
格式化文本的方式- number - "cardinal", "ordinal", "digits", 或 "year" 中的一个
- date - 包含 "d"(基数天),"o"(序数天),"m"(月),或 "y"(年)的字符串
<break time="">
- 暂停给定的时间- time - 秒数("123s")或毫秒数("123ms")
<mark name="">
- 用户定义的标记(单词/句子的marks_before
和marks_after
属性)- name - 标记的名称
<sub alias="">
- 用alias
替换内部文本<phoneme ph="...">
- 为内部文本提供音素ph
- 内部文本每个单词的音素,由空格分隔
<lexicon id="...">
- 内联或外部的发音词典id
- 词典的唯一 ID(用于<lookup ref="...">
)uri
- 如果为空或缺失,则词典是内联的- 一个或多个
<lexeme>
子元素- 可选的
role="..."
([单词角色][#word-roles] 由空格分隔) <grapheme>WORD</grapheme>
- 单词文本<phoneme>P H O N E M E S</phoneme>
- 单词发音(音素由空格分隔)
- 可选的
<lookup ref="...">
- 为子元素使用发音词典ref
- 来自<lexicon id="...">
的 ID
单词角色
在语音合成过程中,单词角色用于消除发音歧义。除非手动指定,否则单词的角色是从其词性标签派生的,形式为 gruut:<TAG>
。对于缩写和 spell-out
,使用角色 gruut:letter
来指示例如 "a" 应该读作 /eɪ/
而不是 /ə/
。
对于 en-us
,从词性标签器提供了以下额外的角色
gruut:CD
- 数字gruut:DT
- 限定词gruut:IN
- 介词或从属连词gruut:JJ
- 形容词gruut:NN
- 名词gruut:PRP
- 人称代词gruut:RB
- 副词gruut:VB
- 动词gruut:VB
- 动词(过去式)
内联词典
通过 <lexicon>
和 <lookup>
标签支持内联 发音词典。gruut 在这里与 SSML 标准 略有不同,允许在 SSML 文档内部定义词典(url
为空或缺失)。此外,可以省略 <lexicon>
元素的 id
属性,以指示一个不需要相应 <lookup>
标签的“默认”内联词典。
例如,以下文档将为单词 "tomato" 产生三种不同的发音
<?xml version="1.0"?>
<speak version="1.1"
xmlns="http://www.w3.org/2001/10/synthesis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2001/10/synthesis
http://www.w3.org/TR/speech-synthesis11/synthesis.xsd"
xml:lang="en-US">
<lexicon xml:id="test" alphabet="ipa">
<lexeme>
<grapheme>
tomato
</grapheme>
<phoneme>
<!-- Individual phonemes are separated by whitespace -->
t ə m ˈɑ t oʊ
</phoneme>
</lexeme>
<lexeme>
<grapheme role="fake-role">
tomato
</grapheme>
<phoneme>
<!-- Made up pronunciation for fake word role -->
t ə m ˈi t oʊ
</phoneme>
</lexeme>
</lexicon>
<w>tomato</w>
<lookup ref="test">
<w>tomato</w>
<w role="fake-role">tomato</w>
</lookup>
</speak>
第一个 "tomato" 将在美式英语词典中查找(/t ə m ˈeɪ t oʊ/
)。在 <lookup>
标签的作用域内,第二个和第三个 "tomato" 单词将在内联词典中查找。第三个 "tomato" 单词附有一个 角色(在这种情况下选择一个虚构的发音)。
甚至更远离SSML标准,gruut允许您完全省略<lexicon>
的id。没有id
,就不需要<lookup>
标签,让您可以覆盖文档中任何单词的发音。
<?xml version="1.0"?>
<speak version="1.1"
xmlns="http://www.w3.org/2001/10/synthesis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2001/10/synthesis
http://www.w3.org/TR/speech-synthesis11/synthesis.xsd"
xml:lang="en-US">
<!-- No id means change all words without a lookup -->
<lexicon>
<lexeme>
<grapheme>
tomato
</grapheme>
<phoneme>
t ə m ˈɑ t oʊ
</phoneme>
</lexeme>
</lexicon>
<w>tomato</w>
</speak>
这将使文档中所有“tomato”的发音为/t ə m ˈɑ t oʊ/
(除非它们有<lookup>
)。
目标受众
gruut可以将原始文本转换为音标发音,类似于phonemizer。与phonemizer不同,gruut在预构建的词汇表(发音字典)中查找单词,或者使用预训练的字符到音素模型猜测单词发音。每种语言的音素都来自精心选择的库存。
对于每种支持的语言,gruut包括以下内容:
- 由开源数据构建的单词发音词汇表
- 用于猜测单词发音的预训练字符到音素模型
某些语言还包括
- 由开源数据构建的预训练词性标注器
项目详情
gruut-2.4.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a49f693266a3a1ab5a6bde77a8f560ef27712b4169b5a6b02e6a1a873342e19e |
|
MD5 | bd39118707abc1b256f296e4f7bf779a |
|
BLAKE2b-256 | fce16b5a01ef36b5341d5d0899401e4413594dfaa21f86cfc05be8efb25baf81 |