2021-02-23 10:00:29
nltk.util.Index(pairs)[源代码]基类:collections.defaultdict
nltk.util.bigrams(sequence, **kwargs)[源代码]以迭代器的形式返回从一系列项生成的大内存。例如:
>>> from nltk.util import bigrams >>> list(bigrams([1,2,3,4,5])) [(1, 2), (2, 3), (3, 4), (4, 5)]
将bigrams用于此函数的列表版本。
sequence (sequence or iter) -- 要转换为bigrams的源数据
iter(tuple)
nltk.util.binary_search_file(file, key, cache={}, cacheDepth=- 1)[源代码]用第一个字键返回文件中的行。使用二进制搜索算法搜索已排序的文件。
file (file) -- 要搜索的文件。
key (str) -- 我们正在搜索的标识符。
nltk.util.breadth_first(tree, children=以宽度第一的顺序遍历树的节点。(不需要检查循环。)第一个参数应该是树根;子级应该是一个函数,将树节点作为参数,并返回节点子级的迭代器。
nltk.util.choose(n, k)[源代码]这个函数是一种快速计算二项式系数的方法,通常称为nck,即一次取k的n个事物的组合数。(https://en.wikipedia.org/wiki/binomial_coefficient)。
这就是 scipy.special.comb()。 对于长整数计算,但此近似值更快,请参阅https://github.com/nltk/nltk/issues/1181
>>> choose(4, 2) 6 >>> choose(6, 2) 15
n (int) -- 事物的数量。
r (int) -- 一件事被带走的次数。
nltk.util.clean_html(html)[源代码]nltk.util.clean_url(url)[源代码]nltk.util.elementtree_indent(elem, level=0)[源代码]用于缩进元素树的递归函数。用于漂亮打印的元素接口。在elem上运行indent,然后以正常方式输出。
elem (ElementTree._ElementInterface) -- 要缩进的元素。将被修改。
level (nonnegative integer) -- 此元素的缩进级别
ElementTree._ElementInterface
缩进以反映其结构的元素的内容
nltk.util.everygrams(sequence, min_len=1, max_len=- 1, pad_left=False, pad_right=False, **kwargs)[源代码]以迭代器的形式返回从一系列项生成的所有可能的ngrams。
>>> sent = 'a b c'.split()
>>> list(everygrams(sent))
[('a',), ('a', 'b'), ('a', 'b', 'c'), ('b',), ('b', 'c'), ('c',)]
>>> sorted(everygrams(sent), key=len)
[('a',), ('b',), ('c',), ('a', 'b'), ('b', 'c'), ('a', 'b', 'c')]
>>> list(everygrams(sent, max_len=2))
[('a',), ('a', 'b'), ('b',), ('b', 'c'), ('c',)]
sequence (sequence or iter) -- 要转换为ngram的源数据。如果未提供max_len,则此序列将加载到内存中
min_len (int) -- NGRAMS的最小长度,aka。n-gram阶数/n gram阶数
max_len (int) -- NGRAMS的最大长度(默认设置为序列长度)
pad_left (bool) -- NGRAMS是否应留有衬垫
pad_right (bool) -- NGRAMS是否应正确填充
iter(tuple)
nltk.util.filestring(f)[源代码]nltk.util.flatten(*args)[源代码]把清单弄平。
>>> from nltk.util import flatten >>> flatten(1, 2, ['b', 'a' , ['c', 'd']], 3) [1, 2, 'b', 'a', 'c', 'd', 3]
args -- 将项目和列表合并为一个列表
list
nltk.util.guess_encoding(data)[源代码]给定一个字节字符串,尝试对其进行解码。尝试标准的“utf8”和“Latin-1”编码,以及从区域设置信息中收集到的一些编码。
呼叫程序 must 第一次呼叫:
locale.setlocale(locale.LC_ALL, '')
如果成功,它会返回 (decoded_unicode, successful_encoding) . 如果失败,它会引发 UnicodeError .
nltk.util.in_idle()[源代码]如果此函数在空闲状态下运行,则返回true。在空闲状态下运行的tkinter程序不应调用 Tk.mainloop ;因此,应使用此函数将所有调用 Tk.mainloop .
此功能通过检查 sys.stdin . 如果用户已修改 sys.stdin ,则可能返回不正确的结果。
bool
nltk.util.invert_dict(d)[源代码]nltk.util.invert_graph(graph)[源代码]反转有向图。
graph (dict(set)) -- 用集合字典表示的图
倒转图
dict(set)
nltk.util.ngrams(sequence, n, **kwargs)[源代码]以迭代器的形式返回从一系列项生成的ngrams。例如:
>>> from nltk.util import ngrams >>> list(ngrams([1,2,3,4,5], 3)) [(1, 2, 3), (2, 3, 4), (3, 4, 5)]
使用列表换行以获得此函数的列表版本。将Pad_Left或Pad_Right设置为真,以获得额外的Ngrams:
>>> list(ngrams([1,2,3,4,5], 2, pad_right=True)) [(1, 2), (2, 3), (3, 4), (4, 5), (5, None)] >>> list(ngrams([1,2,3,4,5], 2, pad_right=True, right_pad_symbol='')) [(1, 2), (2, 3), (3, 4), (4, 5), (5, '')] >>> list(ngrams([1,2,3,4,5], 2, pad_left=True, left_pad_symbol=''))[('', 1), (1, 2), (2, 3), (3, 4), (4, 5)] >>> list(ngrams([1,2,3,4,5], 2, pad_left=True, pad_right=True, left_pad_symbol='', right_pad_symbol='')) [('', 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, '')]
sequence (sequence or iter) -- 要转换为ngrams的源数据
n (int) -- NGRAMS的等级
pad_left (bool) -- NGRAMS是否应留有衬垫
pad_right (bool) -- NGRAMS是否应正确填充
left_pad_symbol (any) -- 用于左填充的符号(默认为无)
right_pad_symbol (any) -- 用于右填充的符号(默认为无)
sequence or iter
返回在提取ngram之前填充的项序列。
>>> list(pad_sequence([1,2,3,4,5], 2, pad_left=True, pad_right=True, left_pad_symbol='', right_pad_symbol='')) ['', 1, 2, 3, 4, 5, ''] >>> list(pad_sequence([1,2,3,4,5], 2, pad_left=True, left_pad_symbol='')) ['', 1, 2, 3, 4, 5] >>> list(pad_sequence([1,2,3,4,5], 2, pad_right=True, right_pad_symbol='')) [1, 2, 3, 4, 5, '']
sequence (sequence or iter) -- 要填充的源数据
n (int) -- NGRAMS的等级
pad_left (bool) -- NGRAMS是否应留有衬垫
pad_right (bool) -- NGRAMS是否应正确填充
left_pad_symbol (any) -- 用于左填充的符号(默认为无)
right_pad_symbol (any) -- 用于右填充的符号(默认为无)
sequence or iter
s->(s0,s1),(s1,s2),(s2,s3)。。。
漂亮地打印一系列数据项
data (sequence or iter) -- 要打印的数据流
start (int) -- 起始位置
end (int) -- 结束位置
漂亮地打印一个字符串,在空白处断行
s (str) -- 要打印的字符串,由单词和空格组成
width (int) -- 显示宽度
返回一个字符串,其中标记围绕匹配的子字符串。搜索str以查找匹配的子字符串 regexp 用大括号把火柴包起来。这便于学习正则表达式。
regexp (str) -- 正则表达式。
string (str) -- 正在匹配的字符串。
left (str) -- 左分隔符(打印在匹配的子字符串之前)
right (str) -- 右分隔符(在匹配的子字符串后打印)
str
为python设置HTTP代理以通过下载。
如果 proxy 如果为“无”,则尝试从环境或系统设置中设置代理。
proxy -- 要使用的HTTP代理服务器。例如:“http://proxy.example.com:3128/”
user -- 要进行身份验证的用户名。使用“无”禁用身份验证。
password -- 要进行身份验证的密码。
以迭代器的形式返回从一系列项生成的所有可能的skipgrams。skipgrams是允许跳过令牌的ngrams。请参阅http://homepages.inf.ed.ac.uk/ballison/pdf/lrec_skipgrams.pdf
>>> sent = "Insurgents killed in ongoing fighting".split()
>>> list(skipgrams(sent, 2, 2))
[('Insurgents', 'killed'), ('Insurgents', 'in'), ('Insurgents', 'ongoing'), ('killed', 'in'), ('killed', 'ongoing'), ('killed', 'fighting'), ('in', 'ongoing'), ('in', 'fighting'), ('ongoing', 'fighting')]
>>> list(skipgrams(sent, 3, 2))
[('Insurgents', 'killed', 'in'), ('Insurgents', 'killed', 'ongoing'), ('Insurgents', 'killed', 'fighting'), ('Insurgents', 'in', 'ongoing'), ('Insurgents', 'in', 'fighting'), ('Insurgents', 'ongoing', 'fighting'), ('killed', 'in', 'ongoing'), ('killed', 'in', 'fighting'), ('killed', 'ongoing', 'fighting'), ('in', 'ongoing', 'fighting')]
sequence (sequence or iter) -- 要转换为三角函数的源数据
n (int) -- NGRAMS的等级
k (int) -- 跳跃距离
iter(tuple)
漂亮地打印文本标记列表,在空白处断行
tokens (list) -- 要打印的令牌
separator (str) -- 用于分隔标记的字符串
width (int) -- 显示宽度(默认值=70)
计算有向图的传递闭包,可以选择自反传递闭包。
该算法是对ioannidis&ramakrishnan(1998)“高效传递闭包算法”的“标记算法”的一个微小修改。
graph (dict(set)) -- 初始图,表示为集合字典
reflexive (bool) -- 如果设置,也使闭包具有反身性
dict(set)
以迭代器的形式返回从一系列项生成的三角图。例如:
>>> from nltk.util import trigrams >>> list(trigrams([1,2,3,4,5])) [(1, 2, 3), (2, 3, 4), (3, 4, 5)]
使用三角函数作为此函数的列表版本。
sequence (sequence or iter) -- 要转换为三角函数的源数据
iter(tuple)
想要了解NLTK,请点击这里
想要了解安装NLTK,请点击这里