-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheaps.py
162 lines (137 loc) · 4.67 KB
/
heaps.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from __future__ import unicode_literals
import json
import math
import numpy as np
import parsivar
import xlrd
import xlsxwriter
import matplotlib.pyplot as plt
# from parsivar import *
from hazm import *
data_set = 'D:\ترم7\بازیابی\Project-1st Phase\IR-1st-Phase\IR1_7k_news.xlsx'
data_reader = xlrd.open_workbook(data_set)
content = data_reader.sheet_by_index(0)
data_writer = xlsxwriter.Workbook(data_set)
worksheet = data_writer.add_worksheet()
number_of_rows = content.nrows
# normalizer = parsivar.Normalizer(statistical_space_correction=True, date_normalizing_needed=True,
# pinglish_conversion_needed=True)
tokenizer = parsivar.Tokenizer()
stemmer = parsivar.FindStems()
stemmer_hazm = Stemmer()
normalizer = Normalizer()
positional_index = dict()
positional_index_with_stops = dict()
garbage = ['۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', '۰', 'a', 'b', 'c', 'd', 'e', 't', 'o', 'p', 'x', 'y', 'z',
'https', '،', '.', ':', '**', '-', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '?', '**', '[', ']',
'(', ')', '://', '/?', '=', '&', '/', '؛', '&', '/', '.', '_', '،', '?**', ":"]
all_token = 2590736
def count_term_stemm(tokens, terms_num, terms):
count = 0
if len(terms_num) != 0:
count = terms_num[-1]
for token in tokens:
stem_token = stemmer.convert_to_stem(token)
if "&" in stem_token:
mazi, mozare = stem_token.split("&")
if mazi not in terms:
terms.append(mazi)
count += 1
if mozare not in terms:
terms.append(mozare)
count += 1
else:
stem_token = stemmer_hazm.stem(stem_token)
if stem_token not in terms:
terms.append(stem_token)
count += 1
terms_num.append(count)
return terms_num, terms
def count_term_no_stemm(tokens, terms_num, terms):
count = 0
if len(terms_num) != 0:
count = terms_num[-1]
for token in tokens:
terms.append(token)
count += 1
terms_num.append(count)
return terms_num, terms
# for stemming case
heaps_tokens_num = []
heaps_tokens = []
heaps_terms = []
terms_num = []
token_num = 0
for i in range(1, 2001):
#
temp = data_reader.sheet_by_index(0).cell(i, 0).value
temp = normalizer.normalize(temp)
temp_tokens = tokenizer.tokenize_words(temp)
if (i == 500 or i == 1000 or i == 1500 or i == 2000):
token_num += len(temp_tokens)
heaps_tokens += temp_tokens
terms_num, heaps_terms = count_term_stemm(heaps_tokens, terms_num, heaps_terms)
heaps_tokens_num.append(token_num)
heaps_tokens = []
else:
token_num += len(temp_tokens)
heaps_tokens += temp_tokens
f = open('index.json', encoding='utf-8')
data = json.load(f)
positional_index = data
f.close()
x = np.array(heaps_tokens_num)
y = np.array(terms_num)
log_token = np.log10(x)
log_term = np.log10(y)
b, k = np.polyfit(log_token, log_term, 1)
dic_predict = math.pow(10, k) * math.pow(all_token, b)
print("For stemming case")
print(" b is = {} and k is = {}".format(b, k))
print("Predicted size for dictionary is {}".format(dic_predict))
print("Real size for dictionary is {}".format(len(positional_index)))
plt.plot(log_token, log_term)
plt.plot(log_token, b * log_token + k)
plt.xlabel("Total tokens")
plt.ylabel("Distinct words")
plt.show()
# for non stemming case
heaps_tokens_num = []
heaps_tokens = []
heaps_terms = []
terms_num = []
token_num = 0
for i in range(1, 2001):
#
temp = data_reader.sheet_by_index(0).cell(i, 0).value
temp = normalizer.normalize(temp)
temp_tokens = tokenizer.tokenize_words(temp)
if (i == 500 or i == 1000 or i == 1500 or i == 2000):
token_num += len(temp_tokens)
heaps_tokens += temp_tokens
terms_num, heaps_terms = count_term_no_stemm(heaps_tokens, terms_num, heaps_terms)
heaps_tokens_num.append(token_num)
heaps_tokens = []
else:
token_num += len(temp_tokens)
heaps_tokens += temp_tokens
# def main():
f = open('index.json', encoding='utf-8')
data = json.load(f)
positional_index = data
f.close()
x = np.array(heaps_tokens_num)
y = np.array(terms_num)
log_token = np.log10(x)
log_term = np.log10(y)
b, k = np.polyfit(log_token, log_term, 1)
dic_predict = math.pow(10, k) * math.pow(all_token, b)
print("For non stemming case")
print(" b is = {} and k is = {}".format(b, k))
print("Predicted size for dictionary is {}".format(dic_predict))
print("Real size for dictionary is {}".format(len(positional_index)))
plt.plot(log_token, log_term)
plt.plot(log_token, b * log_token + k)
plt.xlabel("Total tokens")
plt.ylabel("Distinct words")
plt.show()