Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. """
  2. Repository of scoring functions
  3. """
  4. import abc
  5. from typing import List
  6.  
  7.  
  8. class ScoringException(Exception):
  9.     """ Generic exception raised by any scoring function."""
  10.     pass
  11.  
  12.  
  13. class BaseScoringFunction(abc.ABC):
  14.     """ API that any Scoring function should implement. """
  15.  
  16.     @abc.abstractmethod
  17.     def score(self) -> float:
  18.         """
  19.        :return: Returns the score of the function.
  20.        """
  21.         pass
  22.  
  23.  
  24. class Term:
  25.     """Sentence term"""
  26.  
  27.     def __init__(self, word: str, dictionary: str = 'RS'):
  28.         self.word = word
  29.         self.dictionary = dictionary
  30.  
  31.  
  32. class NLPScoringFunction(BaseScoringFunction):
  33.     """ Scoring function that takes in a sentence and based on
  34.    good/bad word classification assignees a score."""
  35.     BAD_WORDS = ["slanina", "burger", "coca-cola"]
  36.     GOOD_WORDS = ["spanac", "blitva", "celer", "avokado"]
  37.     DICTIONARY = 'RS'
  38.  
  39.     def __init__(self, sentence: str):
  40.         self._sentence = sentence
  41.  
  42.     def terms(self) -> List[Term]:
  43.         return [Term(el) for el in self._sentence.split(" ")]
  44.  
  45.     def score(self) -> float:
  46.         score = 1.0
  47.         for term in self.terms():
  48.             if term.dictionary != 'RS':
  49.                 raise ScoringException('Dictionary must be RS.')
  50.             if term.word in self.BAD_WORDS:
  51.                 score = score * 1.5
  52.             elif term.word in self.GOOD_WORDS:
  53.                 score = score * 0.5
  54.         return score
  55.  
  56.  
  57. import unittest
  58. from unittest.mock import MagicMock
  59.  
  60. from testing_example.scoring_functions import NLPScoringFunction, Term, \
  61.     ScoringException, BaseScoringFunction
  62.  
  63.  
  64. def new_term():
  65.     return [Term('rec', 'CN')]
  66.  
  67.  
  68. class TestNLPScoringFunction(unittest.TestCase):
  69.  
  70.     def test_positive_scores(self):
  71.         scoring_function = NLPScoringFunction("Bolje spanac nego slanina")
  72.         score = scoring_function.score()
  73.         self.assertEquals(score, 0.75)
  74.         assert score == 0.75
  75.  
  76.     def test_raises(self):
  77.         scoring_function = NLPScoringFunction("Bolje spanac nego slanina")
  78.         scoring_function.terms = new_term
  79.         with self.assertRaises(ScoringException):
  80.             scoring_function.score()
  81.  
  82.     # noinspection PyUnresolvedReferences
  83.     def test_with_mock(self):
  84.         scoring_function = NLPScoringFunction("")
  85.         scoring_function.score = MagicMock()
  86.         scoring_function.score.return_value = 0.75
  87.  
  88.         class Optimizer:
  89.             def __init__(self, f: BaseScoringFunction):
  90.                 self.scoring_function = f
  91.  
  92.             def optimize(self):
  93.                 return self.scoring_function.score()
  94.  
  95.         o = Optimizer(scoring_function)
  96.         o.optimize()
  97.         scoring_function.score.assert_called()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement