Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. class SeedCLF:
  2. def __init__(self, id2word, aspects_ids, seed_weights=None, verbose=0, general_ind=4, general_thres=0,
  3. hard_pred=False):
  4. self.id2word = id2word
  5. self.aspects_ids = aspects_ids
  6. self.num_aspects = len(aspects_ids)
  7. self.seed_weights = seed_weights
  8. self.general_ind = general_ind
  9. self.general_thres = general_thres
  10. self.seed_dict = self.create_seed_dict()
  11.  
  12. def create_seed_dict(self):
  13. # Dict: seed_word: (aspect_id, seed_weight)
  14. seed_dict = {}
  15. for i, aspect_seeds in enumerate(self.aspects_ids):
  16. for j, word_id in enumerate(aspect_seeds):
  17. if self.seed_weights is None:
  18. seed_dict[word_id] = (i, 1)
  19. else:
  20. seed_dict[word_id] = (i, self.seed_weights[i][j])
  21. return seed_dict
  22.  
  23. def predict(self, seg):
  24. seg = list(seg)
  25. aspect_scores = [0] * self.num_aspects
  26.  
  27. for word_id in seg:
  28. if word_id in self.seed_dict:
  29. aspect_id, seed_weight = self.seed_dict[word_id]
  30. aspect_scores[aspect_id] += seed_weight
  31.  
  32. if sum(aspect_scores) <= self.general_thres:
  33. aspect_scores[self.general_ind] = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement