-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheapRegression.py
55 lines (40 loc) · 1.6 KB
/
heapRegression.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
import numpy as np
class HeapRegression:
"""
Calculate the variables of Heap law using linear regression
"""
def __init__(self, nb_tokens, nb_vocab):
"""
Initialize the variables used for regression
:param nb_tokens: array of number of tokens from variable text lengths
:param nb_vocab: array of number of vocabulary from variable text lengths
"""
self.nb_tokens = nb_tokens
self.nb_vocab = nb_vocab
self.b = 0
self.k = 0
def calculate_regression(self):
"""
Calculate (b,k) from Heap law: nb_vocab = k * nb_tokens ^ b
"""
regression = np.polyfit(np.log10(self.nb_tokens), np.log10(self.nb_vocab), deg=1)
self.b = regression[0]
self.k = 10 ** regression[1]
return self.b, self.k
def calculate_vocab(self, size):
return self.k * (size ** self.b)
if __name__ == "__main__":
# CACM parameters, result from CACMIndex.py
CACM_tokens = np.array([188887, 85151])
CACM_vocab = np.array([9238, 6334])
# CS276 parameters, result from CS276Index.py
CS276_tokens = np.array([25527977, 12796571])
CS276_vocab = np.array([284418, 140665])
# Change here which collection you want to use
heap = HeapRegression(CACM_tokens, CACM_vocab)
# heap = HeapRegression(CS276_tokens, CS276_vocab)
parameters = heap.calculate_regression()
print("The Heap law parameters are:")
print("(b, k) = {}".format(parameters))
print("For 1 million tokens there would be (by Heap law) {} vocabulary"
.format(heap.calculate_vocab(1000000)))