首页
/ Prosodic 技术文档

Prosodic 技术文档

2024-12-26 11:34:02作者:魏侃纯Zoe

1. 安装指南

1.1 安装 Python 包

首先,您需要通过 pip 安装 Prosodic 包。确保您的 Python 版本 >= 3.9。

pip install prosodic

1.2 安装 espeak

Prosodic 依赖于 espeak 来进行文本到语音的转换。请根据您的操作系统安装 espeak:

  • Mac: 使用 Homebrew 安装:

    brew install espeak
    

    如果尚未安装 Homebrew,请先安装 Homebrew

  • Linux: 使用 apt-get 安装:

    sudo apt-get install espeak libespeak1 libespeak-dev
    
  • Windows: 从 espeak-ng 的 GitHub 页面 下载并安装。

2. 项目的使用说明

2.1 Web 应用

Prosodic 提供了一个基于 Web 的图形用户界面(GUI)。安装完成后,您可以通过以下命令启动 Web 应用:

prosodic web

启动后,在浏览器中访问 http://127.0.0.1:8181/ 即可使用 Prosodic 的 Web 界面。

2.2 Python 使用

2.2.1 读取文本

您可以通过 Python 脚本加载并分析文本。以下是一个简单的示例:

import prosodic

# 加载文本
sonnet = prosodic.Text("""
Those hours, that with gentle work did frame
The lovely gaze where every eye doth dwell,
Will play the tyrants to the very same
And that unfair which fairly doth excel;
For never-resting time leads summer on
To hideous winter, and confounds him there;
Sap checked with frost, and lusty leaves quite gone,
Beauty o’er-snowed and bareness every where:
Then were not summer’s distillation left,
A liquid prisoner pent in walls of glass,
Beauty’s effect with beauty were bereft,
Nor it, nor no remembrance what it was:
But flowers distill’d, though they with winter meet,
Leese but their show; their substance still lives sweet.
""")

# 也可以通过文件名加载文本
shaksonnets = prosodic.Text(fn='corpora/corppoetry_en/en.shakespeare.txt')

2.2.2 文本结构

Prosodic 将文本组织成树状结构。Text 对象的 .children 属性是一个 Stanza 列表,每个 Stanza.parent 属性指向 Text 对象。每个 Stanza.children 属性是一个 Line 列表,依此类推。

# 查看文本的树状结构
sonnet.show(maxlines=30, incl_phons=True)

2.2.3 数据框表示

您还可以将文本转换为数据框形式进行查看:

# 查看音节级别的数据框表示
sonnet.df

3. 项目 API 使用文档

3.1 prosodic.Text

prosodic.Text 是 Prosodic 的核心类,用于加载和分析文本。它支持从字符串或文件加载文本。

3.1.1 方法

  • Text(text=None, fn=None): 初始化一个 Text 对象。text 参数为文本字符串,fn 参数为文件名。
  • show(maxlines=None, incl_phons=False): 显示文本的树状结构。maxlines 参数限制显示的行数,incl_phons 参数决定是否包含音素信息。
  • df: 返回文本的音节级别数据框表示。

3.2 prosodic.Stanzaprosodic.Line

StanzaLine 类分别表示文本中的诗节和行。它们通过 .children.parent 属性与 Text 对象关联。

4. 项目安装方式

Prosodic 的安装方式已在 1. 安装指南 中详细介绍。您可以通过 pip 安装 Prosodic 包,并根据操作系统安装 espeak 以支持文本到语音的转换。


通过本文档,您应该能够顺利安装并使用 Prosodic 进行文本分析。如果您有任何问题,请参考官方文档或联系开发者。

登录后查看全文
热门项目推荐