-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlearning_Feature_evaluation.py
181 lines (163 loc) · 8.7 KB
/
learning_Feature_evaluation.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from datetime import datetime
import os
def computeMajorityVoteForProductCategories(category_path,categoryName):
print("Procedure to read category File, get a product from it and read its file and compute the average and write to a file for a cetegory file")
print("This file willl contain each product_ID Sales_Rank")
print("Considering " + category_path)
#product_Id Category sales_rank
print("Started")
start = datetime.now()
print(start)
filehandle = open("F:\Yassien_PhD\Experiment_3\K_Fold_PerCategory_Basic__With_10_Time_Interval_TQ_Target_25_Transfer/" + categoryName + ".txt",'w')
num_products = 0
with open(category_path, 'r') as fpcat:
for line in fpcat:
row = line.split('\t')
product = row[0]
product_file_path = "C:\Yassien_RMIT PhD\Datasets\TruthDiscovery_Datasets\Web data Amazon reviews/Unique_Products_Stanford_three/Product_Reviews/"+product+".txt"
overallRate = 0
counter = 0
num_products+=1
with open(product_file_path, 'r') as filep:
for item in filep:
review = item.split('\t')
overallRate = overallRate + float(review[5])
counter = counter + 1
print(num_products)
filehandle.write(product)
filehandle.write("\t")
overallRate = overallRate/counter
overallRate = round(overallRate,4)
filehandle.write(str(overallRate))
filehandle.write("\n")
filehandle.close()
Finished = datetime.now()
done = Finished - start
print("Finished in "+str(round(done.total_seconds()/60,3))+" minutes")
return
def computerankusinghelpfulnessForProductCategories(category_path,categoryName):
print("Procedure to read category File, get a product from it and read its file and compute the average and write to a file for a cetegory file")
print("This file willl contain each product_ID Sales_Rank")
print("Considering " + category_path)
#product_Id Category sales_rank
print("Started")
start = datetime.now()
print(start)
filehandle = open("F:\Yassien_PhD\Experiment_3\K_Fold_PerCategory_Basic__With_10_Time_Interval_TQ_Target_25_Transfer/" + categoryName + ".txt",'w')
num_products = 0
with open(category_path, 'r') as fpcat:
for line in fpcat:
row = line.split('\t')
product = row[0]
product_file_path = "C:\Yassien_RMIT PhD\Datasets\TruthDiscovery_Datasets\Web data Amazon reviews/Unique_Products_Stanford_three/Product_Reviews/"+product+".txt"
overallRate = 0
counter = 0
num_products+=1
#print("------------------------")
with open(product_file_path, 'r') as filep:
for item in filep:
review = item.split('\t')
helpful =float(review[4])
total_votes = 0
if review[3] != "":
nonhelpful =float(review[3])-helpful
else:
nonhelpful = total_votes - helpful
#print("helpful "+str(helpful) +" nonhelpful " + str(nonhelpful))
if (helpful-nonhelpful)!=0:
overallRate = overallRate + float(review[5])*(helpful-nonhelpful)
else:
overallRate = overallRate + float(review[5])
counter = counter + 1
print(num_products)
filehandle.write(product)
filehandle.write("\t")
overallRate = overallRate/counter
overallRate = round(overallRate,4)
filehandle.write(str(overallRate))
filehandle.write("\n")
filehandle.close()
Finished = datetime.now()
done = Finished - start
print("Finished in "+str(round(done.total_seconds()/60,3))+" minutes")
return
def computerankusingpolartiesForProductCategories(category_path,categoryName):
print("Procedure to read category File, get a product from it and read its file and compute the total comment polarity and write to a file for a cetegory file")
print("This file willl contain each product_ID Sales_Rank")
print("Considering " + category_path)
#product_Id Category sales_rank
print("Started")
start = datetime.now()
polarity_file_path = "C:\Yassien_RMIT PhD\Datasets\TruthDiscovery_Datasets\Web data Amazon reviews/Unique_Products_Stanford_three/Experiment 2/product_polarties_Per_RatingLevel.txt"
product_polarity_dict = dict()
first_line = 0
with open(polarity_file_path, 'r') as fpcat:
for line in fpcat:
if first_line == 0:
first_line = 1
continue
row = line.split('\t')
print(row[0])
total_polarity_val = int(row[1])+int(row[5])+int(row[9])+int(row[13])+int(row[17])
product_polarity_dict[row[0]]=total_polarity_val
print(start)
filehandle = open("F:\Yassien_PhD\Experiment_3\K_Fold_PerCategory_Basic__With_10_Time_Interval_TQ_Target_25_Transfer/" + categoryName + ".txt",'w')
num_products = 0
with open(category_path, 'r') as fpcat:
for line in fpcat:
row = line.split('\t')
product = row[0]
total_polarity_val=product_polarity_dict[product]
num_products+=1
print(num_products)
filehandle.write(product)
filehandle.write("\t")
filehandle.write(str(total_polarity_val))
filehandle.write("\n")
filehandle.close()
Finished = datetime.now()
done = Finished - start
print("Finished in "+str(round(done.total_seconds()/60,3))+" minutes")
return
def computerankusingdirichiletForProductCategories(category_path,categoryName):
print("Procedure to read category File, get a product from it and read its file and compute the dirichilet and write to a file for a cetegory file")
print("This file willl contain each product_ID Sales_Rank")
print("Considering " + category_path)
# product_Id Category sales_rank
print("Started")
start = datetime.now()
print(start)
productBaseDirectory = "C:\Yassien_RMIT PhD\Datasets\TruthDiscovery_Datasets\Web data Amazon reviews/Unique_Products_Stanford_three/Product_Reviews/"
destDirectory="F:\Yassien_PhD\Experiment_3\K_Fold_PerCategory_Basic__With_10_Time_Interval_TQ_Target_25_Transfer/"
computeNormalDirichelet(productBaseDirectory, category_path, categoryName, destDirectory)
Finished = datetime.now()
done = Finished - start
print("Finished in " + str(round(done.total_seconds() / 60, 3)) + " minutes")
#
from dirichlet_True_Rating import computeNormalDirichelet
categoryList = ["Arts, Crafts & Sewing","Industrial & Scientific", "Jewelry", "Toys & Games", "Video Games","Computers & Accessories", "Software", "Cell Phones & Accessories", "Electronics"]
categories_Path = "C:\Yassien_RMIT PhD\Datasets\TruthDiscovery_Datasets\Web data Amazon reviews/Unique_Products_Stanford_three\categories/"
for cat in categoryList:
current_path= categories_Path+cat+".txt"
#computeMajorityVoteForProductCategories(current_path,cat)
#computerankusingpolartiesForProductCategories(current_path, cat)
#computerankusinghelpfulnessForProductCategories(current_path, cat)
#computerankusingdirichiletForProductCategories(current_path, cat)
categoriesList = ["Arts","Industrial", "Jewelry" ,"Toys", "Video Games","Computers", "Software", "Cell Phones", "Electronics"]
orig_catNames = ["Arts, Crafts & Sewing","Industrial & Scientific", "Jewelry","Toys & Games", "Video Games","Computers & Accessories", "Software", "Cell Phones & Accessories", "Electronics"]
from Learn_Ranking.RankingHelper import createSortedRankAndRunR
for i in range(len(categoriesList)):
categoryName = categoriesList[i]
orig_CatName = orig_catNames[i]
original_directory = "F:\Yassien_PhD\Experiment_3\K_Fold_PerCategory_Basic__With_10_Time_Interval_TQ_Target_25_Transfer/" + orig_CatName + "/"
new_directory = "F:\Yassien_PhD\Experiment_3\K_Fold_PerCategory_Basic__With_10_Time_Interval_TQ_Target_25_Transfer/" + categoryName + "/"
print("Will Rename folder")
os.chmod(original_directory, 0o777)
os.rename(original_directory, new_directory)
print("Renamed the folder")
categoryMainDirectory = "F:\Yassien_PhD\Experiment_3\K_Fold_PerCategory_Basic__With_10_Time_Interval_TQ_Target_25_Transfer/" + categoryName + "/"
createSortedRankAndRunR(categoryMainDirectory, "Lamda", categoryName, orig_CatName)
os.chmod(new_directory, 0o777)
os.rename(new_directory, original_directory)
print("Returned the folder back to original name")
#'''