跳转到主要内容

通过AWS Comprehend在SQLite数据库中运行数据的工具

项目描述

sqlite-comprehend

PyPI Changelog Tests License

通过AWS Comprehend在SQLite数据库中运行数据的工具

有关此项目的背景信息,请参阅sqlite-comprehend: run AWS entity extraction against content in a SQLite database

安装

使用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

每行只考虑前5000个字符。请务必查阅Comprehend的定价 - 起价为每百个字符0.0001美元。

如果您的上下文包含HTML标签,可以在提取实体之前通过添加--strip-tags将其删除。

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

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

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

sqlite-comprehend entities --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 <TEXT TEXT>...  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.

架构

假设有一个名为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

项目详情


下载文件

下载适合您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

sqlite-comprehend-0.2.2.tar.gz (13.6 kB 查看散列)

上传时间

构建分布

sqlite_comprehend-0.2.2-py3-none-any.whl (12.7 kB 查看散列)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面