问题:
查找域名的二元组、三元组和二元组分数。我有一个数据集,我想使用一些简单的分类来区分它们是否是 dga 域。所以我想从使用二元组、三元组和熵开始。
我尝试过的:
from nltk import ngrams
sentence = 'some big sentence'
n = 2
sixgrams = ngrams(sentence.split(), n)
for grams in sixgrams:
print grams
这里我得到了一个句子的二元组。但我的意图不是这个。
我要转换
示例域名:google.co.in
bigrams
[‘$g’, ‘go’, ‘oo’, ‘og’, ‘gl’, ‘le’, ‘e$’, ‘$c’, ‘co’, ‘o$’, ‘$i’, ‘in’, ‘n$’]
trigrams
[‘$go’, ‘goo’, ‘oog’, ‘ogl’, ‘gle’, ‘le$’, ‘$co’, ‘co$’, ‘$in’, ‘in$’]
然后计算bigrams_score。从中我可以用它来预测模块并进行分析。
任何人都可以帮助我了解如何解决这个问题吗?
最佳答案
>>> from nltk import word_tokenize, ngrams
>>> s = "foo bar sentence"
# Word ngrams.
>>> list(ngrams(word_tokenize(s), 2))
[('foo', 'bar'), ('bar', 'sentence')]
# Character ngrams.
>>> list(ngrams(s, 2))
[('f', 'o'), ('o', 'o'), ('o', ' '), (' ', 'b'), ('b', 'a'), ('a', 'r'), ('r', ' '), (' ', 's'), ('s', 'e'), ('e', 'n'), ('n', 't'), ('t', 'e'), ('e', 'n'), ('n', 'c'), ('c', 'e')]
关于python - 如何查找字符二元组和三元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50004602/