-
Notifications
You must be signed in to change notification settings - Fork 0
/
N-Gram.Auth.Attr.py
122 lines (106 loc) · 4.35 KB
/
N-Gram.Auth.Attr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import nltk
import re
import os
import sys
import math
import operator
import random
import numpy as np
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer
from nltk.stem import WordNetLemmatizer
from nltk.util import ngrams
from collections import Counter
from collections import defaultdict
def get_ngrams(text, n):
n_grams = ngrams(word_tokenize(text), n)
return [ ' '.join(grams) for grams in n_grams]
def get_ngrams_count(text, n):
n_grams = ngrams(word_tokenize(text), n)
return Counter(n_grams)
def get_ngrams_prob(text, n):
n_grams = ngrams(word_tokenize(text), n)
n_grams_count = Counter(n_grams)
n_grams_prob = defaultdict(float)
for grams in n_grams_count:
n_grams_prob[grams] = n_grams_count[grams] / len(n_grams_count)
return n_grams_prob
def get_ngrams_prob_smoothing(text, n):
n_grams = ngrams(word_tokenize(text), n)
n_grams_count = Counter(n_grams)
n_grams_prob = defaultdict(float)
for grams in n_grams_count:
n_grams_prob[grams] = (n_grams_count[grams] + 1) / (len(n_grams_count) + len(set(n_grams_count)))
return n_grams_prob
def get_ngrams_prob_backoff(text, n):
n_grams = ngrams(word_tokenize(text), n)
n_grams_count = Counter(n_grams)
n_grams_prob = defaultdict(float)
for grams in n_grams_count:
n_grams_prob[grams] = (n_grams_count[grams] + 1) / (len(n_grams_count) + len(set(n_grams_count)))
return n_grams_prob
def get_ngrams_prob_interpolation(text, n):
n_grams = ngrams(word_tokenize(text), n)
n_grams_count = Counter(n_grams)
n_grams_prob = defaultdict(float)
for grams in n_grams_count:
n_grams_prob[grams] = (n_grams_count[grams] + 1) / (len(n_grams_count) + len(set(n_grams_count)))
return n_grams_prob
def get_ngrams_prob_katz(text, n):
n_grams = ngrams(word_tokenize(text), n)
n_grams_count = Counter(n_grams)
n_grams_prob = defaultdict(float)
for grams in n_grams_count:
n_grams_prob[grams] = (n_grams_count[grams] + 1) / (len(n_grams_count) + len(set(n_grams_count)))
return n_grams_prob
def get_ngrams_prob_kneser_ney(text, n):
n_grams = ngrams(word_tokenize(text), n)
n_grams_count = Counter(n_grams)
n_grams_prob = defaultdict(float)
for grams in n_grams_count:
n_grams_prob[grams] = (n_grams_count[grams] + 1) / (len(n_grams_count) + len(set(n_grams_count)))
return n_grams_prob
def get_ngrams_prob_witten_bell(text, n):
n_grams = ngrams(word_tokenize(text), n)
n_grams_count = Counter(n_grams)
n_grams_prob = defaultdict(float)
for grams in n_grams_count:
n_grams_prob[grams] = (n_grams_count[grams] + 1) / (len(n_grams_count) + len(set(n_grams_count)))
return n_grams_prob
def get_ngrams_prob_absolute_discounting(text, n):
n_grams = ngrams(word_tokenize(text), n)
n_grams_count = Counter(n_grams)
n_grams_prob = defaultdict(float)
for grams in n_grams_count:
n_grams_prob[grams] = (n_grams_count[grams] + 1) / (len(n_grams_count) + len(set(n_grams_count)))
return n_grams_prob
def get_ngrams_prob_good_turing(text, n):
n_grams = ngrams(word_tokenize(text), n)
n_grams_count = Counter(n_grams)
n_grams_prob = defaultdict(float)
for grams in n_grams_count:
n_grams_prob[grams] = (n_grams_count[grams] + 1) / (len(n_grams_count) + len(set(n_grams_count)))
return n_grams_prob
def get_ngrams_prob_kneser_ney_smoothing(text, n):
n_grams = ngrams(word_tokenize(text), n)
n_grams_count = Counter(n_grams)
n_grams_prob = defaultdict(float)
for grams in n_grams_count:
n_grams_prob[grams] = (n_grams_count[grams] + 1) / (len(n_grams_count) + len(set(n_grams_count)))
return n_grams_prob
def get_ngrams_prob_kneser_ney_discounting(text, n):
n_grams = ngrams(word_tokenize(text), n)
n_grams_count = Counter(n_grams)
n_grams_prob = defaultdict(float)
for grams in n_grams_count:
n_grams_prob[grams] = (n_grams_count[grams] + 1) / (len(n_grams_count) + len(set(n_grams_count)))
return n_grams_prob
def get_ngrams_prob_kneser_ney_smoothing_discounting(text, n):
n_grams = ngrams(word_tokenize(text), n)
n_grams_count = Counter(n_grams)
n_grams_prob = defaultdict(float)
for grams in n_grams_count:
n_grams_prob[grams] = (n_grams_count[grams] + 1) / (len(n_gram)