一个轻量级的Python包,用于处理ngram和skipgram
项目描述
nskipgrams是一个轻量级的Python包,用于处理ngram和skipgram。使用ngram和skipgram从序列数据中研究的领域,特别是计算语言学和自然语言处理,将发现这个包很有帮助。
亮点
简单:存储、访问和计数ngram和skipgram——仅此而已!
内存高效:使用字典进行内部存储。
无烦恼:无依赖。纯Python编写。今天是个好日子。
下载和安装
要下载和安装最新版本
$ pip install --upgrade nskipgrams
使用方法
以下已定义
- Ngrams
Ngrams类处理ngram集合。
ngrams_from_seq函数为给定序列生成ngram。
- Skipgrams
类 Skipgrams 处理一组 skipgrams。
函数 skipgrams_from_seq 为给定序列生成 skipgrams。
从序列获取 Ngrams
如果您只需要从序列中获取 ngrams,那么您要找的是 ngrams_from_seq
>>> from nskipgrams import ngrams_from_seq
>>> for ngram in ngrams_from_seq("abcdef", n=2):
... print(ngram)
('a', 'b')
('b', 'c')
('c', 'd')
('d', 'e')
('e', 'f')
初始化 Ngram 集合
>>> from nskipgrams import Ngrams
>>> char_ngrams = Ngrams(n=3) # handles unigrams, bigrams, and trigrams
添加 Ngrams
>>> char_ngrams.add_from_seq("my cats")
>>> char_ngrams.add_from_seq("your cat", count=2)
在这里,序列是可以迭代的任何东西,相应的 ngrams 是从序列的各个元素中提取出来的。例如,如果序列是上面提到的文本字符串 "my cats",那么 ngrams 是基于字符的(因此选择了变量名 char_ngrams)。
添加单个 ngram
>>> char_ngrams.add(("y", "o", "u"))
作为最佳实践,建议将 ngram 表示为一个 tuple,无论各个元素是什么,例如,对于基于字符的 ngrams,为 ("y", "o", "u")。下面的输出示例也显示,此包也使用 tuple 数据类型来表示 ngrams。
访问 Ngrams
>>> for ngram, count in char_ngrams.ngrams_with_counts(n=1): # unigrams
... print(ngram, count)
...
('m',), 1
('y',), 3
(' ',), 3
('c',), 3
('a',), 3
('t',), 3
('s',), 1
('o',), 2
('u',), 2
('r',), 2
>>>
>>> for ngram, count in char_ngrams.ngrams_with_counts(n=2): # bigrams
... print(ngram, count)
...
('m', 'y'), 1
('y', ' '), 1
('y', 'o'), 2
(' ', 'c'), 3
('c', 'a'), 3
('a', 't'), 3
('t', 's'), 1
('o', 'u'), 2
('u', 'r'), 2
('r', ' '), 2
>>>
>>> for ngram, count in char_ngrams.ngrams_with_counts(n=3): # trigrams
... print(ngram, count)
...
('m', 'y', ' '), 1
('y', ' ', 'c'), 1
('y', 'o', 'u'), 3
(' ', 'c', 'a'), 3
('c', 'a', 't'), 3
('a', 't', 's'), 1
('o', 'u', 'r'), 2
('u', 'r', ' '), 2
('r', ' ', 'c'), 2
以特定前缀访问 Ngrams
>>> for ngram, count in char_ngrams.ngrams_with_counts(n=3, prefix=("y",)):
... print(ngram, count)
...
('y', ' ', 'c'), 1
('y', 'o', 'u'), 3
访问特定 Ngram 的数量
>>> char_ngrams.count(("c", "a", "t"))
3
检查成员资格
检查 ngram 是否在集合中具有精确匹配
>>> ("c", "a", "t") in char_ngrams
True
组合 Ngram 集合
要组合 Ngram 集合(例如,当并行处理数据源并具有多个 Ngrams 对象时)
>>> char_ngrams1 = Ngrams(n=2)
>>> char_ngrams1.add_from_seq("my cat")
>>> set(char_ngrams1.ngrams_with_counts(n=2))
{((' ', 'c'), 1),
(('a', 't'), 1),
(('c', 'a'), 1),
(('m', 'y'), 1),
(('y', ' '), 1)}
>>>
>>> char_ngrams2 = Ngrams(n=2)
>>> char_ngrams2.add_from_seq("your cats")
>>> set(char_ngrams2.ngrams_with_counts(n=2))
{((' ', 'c'), 1),
(('a', 't'), 1),
(('c', 'a'), 1),
(('o', 'u'), 1),
(('r', ' '), 1),
(('t', 's'), 1),
(('u', 'r'), 1),
(('y', 'o'), 1)}
>>>
>>> char_ngrams3 = Ngrams(n=2)
>>> char_ngrams3.add_from_seq("her cats")
>>> set(char_ngrams3.ngrams_with_counts(n=2))
{((' ', 'c'), 1),
(('a', 't'), 1),
(('c', 'a'), 1),
(('e', 'r'), 1),
(('h', 'e'), 1),
(('r', ' '), 1),
(('t', 's'), 1)}
>>>
>>> char_ngrams1.combine(char_ngrams2, char_ngrams3) # `combine` takes as many Ngrams objects as desired
>>> set(char_ngrams1.ngrams_with_counts(n=2))
{((' ', 'c'), 3),
(('a', 't'), 3),
(('c', 'a'), 3),
(('e', 'r'), 1),
(('h', 'e'), 1),
(('m', 'y'), 1),
(('o', 'u'), 1),
(('r', ' '), 2),
(('t', 's'), 2),
(('u', 'r'), 1),
(('y', ' '), 1),
(('y', 'o'), 1)}
如果您不想更改任何 Ngrams 实例(combine 方法就地工作并修改 these_ngrams 当 these_ngrams.combine 被调用时),则可以创建一个空的 ngram 集合,并将所有 ngrams 合并到其中
>>> collections = [char_ngrams1, char_ngrams2, char_ngrams3]
>>> all_ngrams = Ngrams(n=2) # A new, empty collection of ngrams
>>> all_ngrams.combine(*collections)
任何“序列”及其对应的“Ngrams”都有效
虽然上面的示例使用文本字符串作为序列和基于字符的 ngrams,但在计算语言学和 NLP 中的另一种常见用法是将分段的短语/句子作为序列和基于单词的 ngrams
>>> from nskipgrams import Ngrams
>>> word_ngrams = Ngrams(n=2)
>>> word_ngrams.add_from_seq(("in", "the", "beginning"))
>>> word_ngrams.add_from_seq(("in", "the", "end"))
>>> for ngram, count in word_ngrams.ngrams_with_counts(n=2):
... print(ngram, count)
...
('in', 'the'), 2
('the', 'beginning'), 1
('the', 'end'), 1
Skipgrams
Ngrams 是 skipgrams 的特例,其中 skip = 0。类 Skipgrams 的工作方式与 Ngrams 相同,但有以下区别
Skipgrams 有 skipgrams_with_counts 方法而不是 ngrams_with_counts。 skipgrams_with_counts 还有一个关键字参数 skip(除了 n 和 prefix)。
对于 Skipgrams,方法 add 和 count,以及集合实例化(即 __init__),也都有一个有意义的 skip 关键字参数。
函数 skipgrams_from_seq 的工作方式与 ngrams_from_seq 相同,但有一个 skip 关键字参数(除了 seq 和 n)。
引用
Lee, Jackson L. 2023. nskipgrams:一个轻量级的 Python 包,用于处理 ngrams 和 skipgrams。 https://doi.org/10.5281/zenodo.4002095
@software{leengrams,
author = {Jackson L. Lee},
title = {nskipgrams: A lightweight Python package to work with ngrams and skipgrams},
year = 2021,
doi = {10.5281/zenodo.4002095},
url = {https://doi.org/10.5281/zenodo.4002095}
}
许可证
MIT 许可证。有关详细信息,请参阅 GitHub 源代码中的 LICENSE.txt。
变更日志
请参阅 GitHub 源代码中的 CHANGELOG.md。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。