jhylands

Anti-attack ratings

Mar 12th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. import sys
  2. def originalR(rev, the_j):
  3.     Cj = [x for i, j, x in rev if j == the_j]
  4.     return sum(Cj) / len(Cj)
  5.  
  6.  
  7. def f(r, x, alpha):
  8.     return 1 if abs(r-x) <= alpha else 0
  9. def r(listOfi, j, T, X):
  10.     return sum([T[i]*X[(i, j)] for i in listOfi])/sum([T[i] for i in listOfi])
  11. def t(i, the_r, listOfj, X, alpha):
  12.     return float((1+ sum([f(the_r[j], X[(i, j)], alpha) for j in listOfj])))/float((2+len(listOfj)))
  13.  
  14. the_file = sys.argv[1]
  15. with open(the_file, 'r') as f:
  16.     IOreviw = f.read()
  17. rev = [[float(x) for x in X.split(' ') if x<> ''] for X in IOreviw.split('\n') if X.split('')<>[]]
  18.  
  19. #retrive the alpha
  20. alpha = float(sys.argv[2])
  21.  
  22. #calculate the new one
  23. #what states do we have to hold that change as the file is read in
  24. C = {} # keys are j values are arrays of
  25. P = {} # keys are i
  26. X = {} # keys are (i,j) pairs
  27. T = {}
  28. the_r = {}
  29.  
  30. #main loop
  31. for i,j,x in rev:
  32.     print('%d:%d:%d' % (i,j,x))
  33.     #If this is the first rating from customer i, using eq. (3) to initialise the trust level of customer i with the value corresponding to Pi ={} (i.e. the empty set);
  34.     if not i in T:
  35.         T[i] = 1./2
  36.     #Using eq. (2) and the current trust levels of the relevant customers to calculate the overall rating for product j;    
  37.         #add the persons rating to the ratings for j
  38.         #add this product to the set of products reviewed by i
  39.     if i in P:
  40.         P[i].append(j) #assume the person only review the product once
  41.     else:
  42.         P[i] = [j]
  43.  
  44.         #add this consumer to the set of people who have reviwed j
  45.     if j in C:
  46.         C[j].append(i) #same assumption
  47.     else:
  48.         C[j] = [i]
  49.         #add i's review of product j
  50.     X[(i,j)] = x
  51.         #calulate the rating
  52.     the_r[j] = r(C[j],j,T,X)
  53.     #Using eq. (3) to update the trust levels of all customers who provided ratings for product j so far;
  54.     for i in C[j]:
  55.         T[i] = t(i,the_r,P[i],X,alpha)
  56.     #Recalculating the overall ratings for the products  other than product j  affected by this updating of trust levels
  57.     for j in P[i]:
  58.         the_r[j] = r(C[j],j,T,X)
  59.  
  60.  
  61.     #should this be for all of i in Cj?
  62.     print('r:%.2f;t:%.2f;i:%d;j:%d;x:%d;' % (the_r[j],T[i],i,j,x))
  63.  
  64. ###################################################
  65. outfile = sys.argv[3]
  66. #for each product in the product list
  67. with open(outfile,'w+') as f:
  68.     for j in list(set([j for i,j,x in rev[:-1]])):
  69.         f.write('%d %.2f (%.2f)\n'%(j,the_r[j], 1))
  70.     f.write('\n')
  71.     for i in list(set([i for i,j,x in rev[:-1]])):
  72.         f.write('%d %.2f\n' % (i,T[i]))
Advertisement
Add Comment
Please, Sign In to add comment