Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.72 KB | None | 0 0
  1. from math import sqrt
  2.  
  3.  
  4. oceniPoKorisnici = {
  5.     'Lisa Rose': {'Catch Me If You Can': 3.0, 'Snakes on a Plane': 3.5, 'Superman Returns': 3.5,
  6.                   'You, Me and Dupree': 2.5, 'The Night Listener': 3.0, 'Snitch': 3.0},
  7.     'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5, 'Just My Luck': 1.5, 'The Night Listener': 3.0,
  8.                      'You, Me and Dupree': 3.5},
  9.     'Michael Phillips': {'Catch Me If You Can': 2.5, 'Lady in the Water': 2.5, 'Superman Returns': 3.5,
  10.                          'The Night Listener': 4.0, 'Snitch': 2.0},
  11.     'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0, 'The Night Listener': 4.5, 'Superman Returns': 4.0,
  12.                      'You, Me and Dupree': 2.5},
  13.     'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0, 'Just My Luck': 2.0, 'Superman Returns': 3.0,
  14.                      'You, Me and Dupree': 2.0},
  15.     'Jack Matthews': {'Catch Me If You Can': 4.5, 'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
  16.                       'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5, 'Snitch': 4.5},
  17.     'Toby': {'Snakes on a Plane': 4.5, 'Snitch': 5.0},
  18.     'Michelle Nichols': {'Just My Luck': 1.0, 'The Night Listener': 4.5, 'You, Me and Dupree': 3.5,
  19.                          'Catch Me If You Can': 2.5, 'Snakes on a Plane': 3.0},
  20.     'Gary Coleman': {'Lady in the Water': 1.0, 'Catch Me If You Can': 1.5, 'Superman Returns': 1.5,
  21.                      'You, Me and Dupree': 2.0},
  22.     'Larry': {'Lady in the Water': 3.0, 'Just My Luck': 3.5, 'Snitch': 1.5, 'The Night Listener': 3.5}
  23. }
  24.  
  25.  
  26. # Vrakja merka za slicnost bazirana na rastojanieto za person1 i person2
  27. def sim_distance(prefs, person1, person2):
  28.     # Se pravi lista na zaednicki predmeti (filmovi)
  29.     si = {}
  30.     for item in prefs[person1]:
  31.         if item in prefs[person2]:
  32.             si[item] = 1
  33.     # ako nemaat zaednicki rejtinzi, vrati 0
  34.     if len(si) == 0:
  35.         return 0
  36.     # Soberi gi kvadratite na site razliki
  37.     sum_of_squares = sum([pow(prefs[person1][item] - prefs[person2][item], 2)
  38.     for item in prefs[person1] if item in prefs[person2]])
  39.     return round(1 / (1 + sqrt(sum_of_squares)),3)
  40.  
  41.  
  42. def sim_pearson(oceni, person1, person2):
  43.     zaednicki = {}
  44.     for item in oceni[person1]:
  45.         if item in oceni[person2]:
  46.             zaednicki[item] = 1
  47.  
  48.     n = len(zaednicki)
  49.  
  50.     if n == 0:
  51.         return [0, 0]
  52.  
  53.     sum1 = 0
  54.     sum2 = 0
  55.  
  56.     sum1Sq = 0
  57.     sum2Sq = 0
  58.  
  59.     pSum = 0
  60.     for item in zaednicki.keys():
  61.         ocena1 = oceni[person1][item]
  62.         ocena2 = oceni[person2][item]
  63.         sum1 += ocena1
  64.         sum1Sq += ocena1 ** 2
  65.         sum2 += ocena2
  66.         sum2Sq += ocena2 ** 2
  67.         pSum += ocena1 * ocena2
  68.  
  69.     # Presmetaj go koeficientot na korelacija
  70.     num = pSum - sum1 * sum2 / n
  71.     den = sqrt((sum1Sq - pow(sum1, 2) / n) * (sum2Sq - pow(sum2, 2) / n))
  72.     if den == 0:
  73.         return [0, 0]
  74.  
  75.     r = num / den
  76.     return round(r, 3), n
  77.  
  78.  
  79.  
  80. def TabelaNaSlicniKorisnici(oceni):
  81.     result = {}
  82.     for x in oceni:
  83.         for y in oceni:
  84.             if x != y:
  85.                 if x not in result.keys():
  86.                    result[x]={}
  87.                 pearson = sim_pearson(oceni, x, y)[0]
  88.                 evklidovo = sim_distance(oceni, x, y)
  89.                 zaedncki = sim_pearson(oceni, x, y)[1]
  90.                 result[x][y] = (evklidovo, pearson, zaedncki)
  91.     return result
  92.  
  93.  
  94. if __name__ == "__main__":
  95.     korisnik1 = input()
  96.     korisnik2 = input()
  97.     # korisnik1='Mick LaSalle'
  98.     # korisnik2='Lisa Rose'
  99.     # print oceniPoKorisnici
  100.     tabela = TabelaNaSlicniKorisnici(oceniPoKorisnici)
  101.     #print tabela
  102.     print tabela[korisnik1][korisnik2]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement