sqlite-comprehend 作者 simonw

星标

README 源代码

sqlite-comprehend

PyPI Changelog Tests License

用于通过 AWS Comprehend 处理 SQLite 数据库中数据的工具

有关此项目的背景信息,请参阅 sqlite-comprehend:对 SQLite 数据库中的内容运行 AWS 实体提取

安装

使用 pip 安装此工具

pip install sqlite-comprehend

演示

您可以在此处查看使用此命令生成的表的示例

配置

您将需要具有 comprehend:BatchDetectEntities IAM 权限的 AWS 凭证。

您可以按照这些说明配置凭证。您也可以将它们保存到 JSON 或 INI 配置文件中,并通过 -a credentials.ini 将其传递给命令,或者使用 --access-key--secret-key 选项传递它们。

实体提取

sqlite-comprehend entities 命令对指定表中的每一行运行实体提取,并将结果保存到您的数据库中。

指定数据库、表以及该表中包含文本的一个或多个列。以下命令对 sfms.db SQLite 数据库的 pages 表中的 text 列运行实体提取

sqlite-comprehend sfms.db pages text

结果将写入 pages_comprehend_entities 表。通过传递 -o other_table_name 更改输出表的名称。

您可以通过添加 --where 子句来对行的一个子集运行提取

sqlite-comprehend sfms.db pages text --where 'id < 10'

您也可以在 --where 子句中使用命名参数

sqlite-comprehend sfms.db pages text --where 'id < :maxid' -p maxid 10

每行只会考虑前 5,000 个字符。请务必查看 Comprehend 的定价 - 每百字符起价为 $0.0001。

如果您的上下文包含 HTML 标签,您可以在提取实体之前通过添加 --strip-tags 将它们剥离出来

sqlite-comprehend sfms.db pages text --strip-tags

已处理的行会被记录在 pages_comprehend_entities_done 表中。如果您多次运行该命令,它只会处理新添加的行。

您可以删除该 _done 表中的记录,以便再次运行它们。

sqlite-comprehend entities --help

... SQL 查询的命名 :参数 -o, --output TEXT 自定义输出表 -r, --reset 从头开始,删除以前的结果 --strip-tags 在提取实体前剥离 HTML 标签 --access-key TEXT AWS 访问密钥 ID --secret-key TEXT AWS 秘密访问密钥 --session-token TEXT AWS 会话令牌 --endpoint-url TEXT 自定义端点 URL -a, --auth FILENAME 包含凭证的 JSON/INI 文件路径 --help 显示此消息并退出。 ">
Usage: sqlite-comprehend entities [OPTIONS] DATABASE TABLE COLUMNS...

  Detect entities in columns in a table

  To extract entities from columns text1 and text2 in mytable:

      sqlite-comprehend entities my.db mytable text1 text2

  To run against just a subset of the rows in the table, add:

      --where "id < :max_id" -p max_id 50

  Results will be written to a table called mytable_comprehend_entities

  To specify a different output table, use -o custom_table_name

Options:
  --where TEXT                WHERE clause to filter table
  -p, --param ...  Named :parameters for SQL query
  -o, --output TEXT           Custom output table
  -r, --reset                 Start from scratch, deleting previous results
  --strip-tags                Strip HTML tags before extracting entities
  --access-key TEXT           AWS access key ID
  --secret-key TEXT           AWS secret access key
  --session-token TEXT        AWS session token
  --endpoint-url TEXT         Custom endpoint URL
  -a, --auth FILENAME         Path to JSON/INI file containing credentials
  --help                      Show this message and exit.

Schema(模式)

假设输入表名为 pages,则此工具创建的表将具有以下模式

CREATE TABLE [pages] (
   [id] INTEGER PRIMARY KEY,
   [text] TEXT
);
CREATE TABLE [comprehend_entity_types] (
   [id] INTEGER PRIMARY KEY,
   [value] TEXT
);
CREATE TABLE [comprehend_entities] (
   [id] INTEGER PRIMARY KEY,
   [name] TEXT,
   [type] INTEGER REFERENCES [comprehend_entity_types]([id])
);
CREATE TABLE [pages_comprehend_entities] (
   [id] INTEGER REFERENCES [pages]([id]),
   [score] FLOAT,
   [entity] INTEGER REFERENCES [comprehend_entities]([id]),
   [begin_offset] INTEGER,
   [end_offset] INTEGER
);
CREATE UNIQUE INDEX [idx_comprehend_entity_types_value]
    ON [comprehend_entity_types] ([value]);
CREATE UNIQUE INDEX [idx_comprehend_entities_type_name]
    ON [comprehend_entities] ([type], [name]);
CREATE TABLE [pages_comprehend_entities_done] (
   [id] INTEGER PRIMARY KEY REFERENCES [pages]([id])
);

开发

要为这个工具做贡献,首先克隆代码。然后创建一个新的虚拟环境

cd sqlite-comprehend
python -m venv venv
source venv/bin/activate

现在安装依赖和测试依赖

pip install -e '.[test]'

运行测试

pytest