Beispiel #1
0
def viterbi(documents):
  model = Scoring()
  results = []
  num_doc = 0
  for document, original_document in documents:
    print(num_doc)
    num_doc += 1
    best_edge = defaultdict(lambda: ['NULL' for _ in range(n_best_size)])
    best_score = defaultdict(lambda: [0 for _ in range(n_best_size)])

    # forward
    for i, candidates in enumerate(document, 1):
      cur_nbest = candidates[0]
      pre_nbest = candidates[1]
      scores = np.array(model.inference(pre_nbest, cur_nbest))
      scores = -np.log(sigmoid(scores))
      scores += np.broadcast_to(np.array(best_score[i]), (len(best_score[i]), len(best_score[i]))).T.reshape(-1)
      edges = np.argmin(scores.reshape(len(best_score[i]), len(best_score[i])), axis=1)
      scores = np.min(scores.reshape(len(best_score[i]), len(best_score[i])), axis=1)
      best_edge[i] = edges
      best_score[i] = scores
    best_edge = dict(best_edge)
    best_score = dict(best_score)

    # backward
    best_path = []
    next_edge = np.argmin(np.array(best_score[len(document)]))
    best_path += [next_edge]
    for i in range(len(original_document))[:0:-1]:
      next_edge = best_edge[i][next_edge]
      best_path += [next_edge]
    best_path.reverse()
    results += [original_document[i][edge] for i, edge in enumerate(best_path)]
  return results
Beispiel #2
0
 def __init__(self, player_agent, starting_cheques):
     self._player_agent = player_agent
     self._available_cheques = starting_cheques[:]
     self._unavailable_cheques = []
     self._scards = []
     self._scored_scards = []
     self._removed_scards = []
     self._counts = {c: 0 for c in Card if c != Card.Policeman}
     self._scoring_cheques = None
     self._scoring = Scoring(player_agent)
Beispiel #3
0
def grade(file, test):
    scoring = Scoring(file, test, 1)
    passed_tests, total_tests, wrong_tests, message = scoring.grade()
    print(
        json.dumps({
            "passed_tests": passed_tests,
            "total_tests": total_tests,
            "wrong_tests": wrong_tests,
            "message": message
        }))
Beispiel #4
0
 def evaluate_decision(self, board, genes):
     shadow_board = deepcopy(board)
     for i in range(0, rows):
         if not check_collision(shadow_board, self.stone,
                                (self.stone_x, self.stone_y)):
             self.stone_y += 1
         else:
             break
     join_matrixes(shadow_board, self.stone, (self.stone_x, self.stone_y))
     score = Scoring((shadow_board, rows, cols))
     return score.get_score((self.stone, self.stone_x, self.stone_y), genes)
Beispiel #5
0
 def model_scoring(self, data):
     self.my_logger.info("Data Size {0}".format(len(data)))
     scorer = Scoring()
     try:
         scorer.start(data)
     except:
         error_msg = 'Program failed \n {} \n {}'.format(
             sys.exc_info()[0],
             sys.exc_info()[1])
         self.my_logger.debug(error_msg)
         sys.exit(error_msg)
Beispiel #6
0
def analyzer_top5(word, option=2):
    candidate_creater = SearchLongDist()
    scorer = Scoring()

    candidates = candidate_creater.getCandidates(word)
    ret = scorer.scoring(candidates, option=option)

    if len(ret) > 0:
        return ret

    else:
        return [{"score": 1, "candidate": [{"cnt": 0, "word": word}]}]
Beispiel #7
0
def score_it():
    for player in player_set_up:
        points = Scoring(player[3])
        total_score = points.total()[0]
        player[6] += points.total()[2]
        if player[1] == maki_scores[0]:
            total_score += 6
        elif player[1] == maki_scores[1]:
            total_score += 3
        elif player[1] in maki_scores[2]:
            total_score += int(6 / len(maki_scores[2]))
        elif player[1] in maki_scores[3]:
            total_score += int(3 / len(maki_scores[3]))
        player[7] += total_score
    return
Beispiel #8
0
 def scoring(self, method='zagibolov'):
     # Supply argument in Corpus to connect to databse. user, password and db.
     corpus = Corpus(password='', db='project_major')
     corpus.getTweets()
     dataset = corpus.dataSet
     preprocess = Preprocess('zagibolov', self.lexicons, self.negatives, self.stopWords)
     scoring = Scoring(method, self.lexicons, self.negatives, self.stopWords, self.seeds)
     j = 0
     for data in dataset:
         preprocess.preprocessScoring(data)
         processed = preprocess.processed_data
         
     for data in processed:
         scoring.count(data['tweet'])
 ##        print self.seeds
     preprocess.seeds = scoring.lexicon_count
     preprocess.processLexicon()
     scoring.lexicons = preprocess.lexicons
 ##        print scoring.lexicon_count
     last_score = {}
     i = 0
     for i in range(0,3):
         total = 0
         j = 0
         negative = 0
         positive = 0
         scoring.resetLexiconCount()
 ##        print self.lexicons
         for data in processed:
             if j == 50:
                 break
             j += 1
             score = scoring.score(data)
             if score != 0:
                 total += 1
                 if score < 0:
                     negative += 1
                 else:
                     positive += 1
         scoring.adjustScoring()
         if last_score == {}:
             last_score = scoring.lexicons
             this_score = last_score
         else:
             this_score = scoring.lexicons
             if this_score == last_score:
                 break
             else:
                 last_score = this_score
         print this_score
         print "Total scored: " + str(total), "Negative: ", negative, "Positive: ", positive
     print this_score
     print "Total scored: " + str(total), "Negative: ", negative, "Positive: ", positive
Beispiel #9
0
 def run_strategy(self, background=False):
     scoring = Scoring(self.scenario)
     strategy = self.strategy_factory(self._generate_random_dance, scoring)
     self.publishers.publish_scenario(self.scenario)
     self.publishers.publish_settings(strategy.get_settings())
     self.publishers.publish_stats(0, 0, 0, 0)
     if background:
         self.background_thread = Thread(target=self._run_strategy,
                                         args=(strategy, ))
         self.background_thread.daemon = True
         self.background_thread.start()
     else:
         self._run_strategy(strategy)
Beispiel #10
0
def maki_score():
    maki_count_1st = 0
    maki_score_1st = None
    maki_count_2nd = 0
    maki_score_2nd = None
    maki_score_1st_tie = []
    maki_score_2nd_tie = []
    for player in player_set_up:
        points = Scoring(player[3])
        maki_count = points.total()[1]
        if maki_count >= maki_count_1st:
            if maki_count > maki_count_1st:
                if maki_count_1st >= maki_count_2nd and maki_count_1st != 0:
                    if maki_count_1st > maki_count_2nd:
                        maki_count_2nd = maki_count_1st
                        maki_score_2nd = maki_score_1st
                        maki_score_2nd_tie = maki_score_1st_tie
                    elif maki_count == maki_count_2nd:
                        maki_score_2nd_tie.append(maki_score_1st)
                        maki_score_2nd_tie.append(maki_score_1st_tie)
                maki_count_1st = maki_count
                maki_score_1st = player[1]
                maki_score_1st_tie = [player[1]]
            elif maki_count == maki_count_1st:
                maki_score_1st_tie.append(player[1])
                maki_score_1st = None
        elif maki_count >= maki_count_2nd:
            if maki_count > maki_count_2nd:
                maki_count_2nd = maki_count
                maki_score_2nd = player[1]
                maki_score_2nd_tie = [player[1]]
            elif maki_count == maki_count_2nd:
                maki_score_2nd_tie.append(player[1])
                maki_score_2nd = None
    maki_scores = (maki_score_1st, maki_score_2nd, maki_score_1st_tie,
                   maki_score_2nd_tie)
    return maki_scores
 def search(self, token: str) -> dict:
     response = []
     match = self.match(token)
     if match:
         for doc_id, freq in match['postings'].items():
             field_length = len(self.index[doc_id])
             avg_field_length = self.get_avg_field_length()
             documents_count = self.size()
             document_frequency = match['doc_freq']
             score = Scoring.tfidf(freq, field_length, avg_field_length,
                                   documents_count, document_frequency)
             response.append({
                 'id': doc_id,
                 'doc': self.index[doc_id],
                 'score': score
             })
         return sorted(response, key=itemgetter('score'), reverse=True)
     else:
         return {}
class TestEnglishScoring(unittest.TestCase):
    def setUp(self):
        self.subject = Scoring()

    def test_one_point(self):
        for letter in ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T']:
            with self.subTest(letter=letter):
                self.assertEqual(self.subject.score_letter(letter), 1)

    def test_two_points(self):
        for letter in ['D', 'G']:
            with self.subTest(letter=letter):
                self.assertEqual(self.subject.score_letter(letter), 2)

    def test_three_points(self):
        for letter in ['B', 'C', 'M', 'P']:
            with self.subTest(letter=letter):
                self.assertEqual(self.subject.score_letter(letter), 3)

    def test_four_points(self):
        for letter in ['F', 'H', 'V', 'W', 'Y']:
            with self.subTest(letter=letter):
                self.assertEqual(self.subject.score_letter(letter), 4)

    def test_five_points(self):
        for letter in ['K']:
            with self.subTest(letter=letter):
                self.assertEqual(self.subject.score_letter(letter), 5)

    def test_eight_points(self):
        for letter in ['J', 'X']:
            with self.subTest(letter=letter):
                self.assertEqual(self.subject.score_letter(letter), 8)

    def test_ten_points(self):
        for letter in ['Q', 'Z']:
            with self.subTest(letter=letter):
                self.assertEqual(self.subject.score_letter(letter), 10)
Beispiel #13
0
                                            loss=checkpoint["loss"])
    Encoder.load_state_dict(checkpoint["model_state_dict"])
    Encoder = Encoder.eval().cuda().to(device)
    logging.info("Encoder loaded from : " + config["model"]["encoder_dir"])

    # Generating Dataloader
    loader = Loader(config["data"])
    train_loader, val_loader, test_loader = loader.get_dataloader(
        "train"), loader.get_dataloader("val"), loader.get_dataloader("test")
    logging.info("Data loaded from : " + config["data"]["dataset"] +
                 " Size of loaders {}, {}, {}".format(train_loader.__len__(
                 ), val_loader.__len__(), test_loader.__len__()))

    #Initiating scoring modules
    test_scorer = Scoring(test_loader,
                          loader.get_dataset("test"),
                          device,
                          name="test")
    test_scorer.extract_embeddings(Encoder)
    logging.info("EER on test set : " + str(test_scorer.compute_EER()) + " %")

    train_scorer = Scoring(train_loader,
                           loader.get_dataset("train"),
                           device,
                           name="train")
    train_scorer.extract_embeddings(Encoder)
    logging.info("EER on train set : " + str(train_scorer.compute_EER()) +
                 " %")

    val_scorer = Scoring(val_loader,
                         loader.get_dataset("val"),
                         device,
Beispiel #14
0
n_training = len(df) - n_valid

print("splitting raw")
raw_train, raw_valid = cleaning.split_vals(df_raw, n_training)

print("Splitting X")
X_train, X_valid = cleaning.split_vals(df, n_training)

print("Splitting y")
y_train, y_valid = cleaning.split_vals(y, n_training)

print("Shape of X_train is %s, shape of X_valid is %s, shape of y_train is %s and of y_valid is %s"
            %(str(X_train.shape), str(X_valid.shape), str(y_train.shape), str(y_valid.shape)))


scoring = Scoring()

def print_score(m):
    res = [scoring.rmse(m.predict(X_train), y_train), scoring.rmse(m.predict(X_valid), y_valid),
            m.score(X_train, y_train), m.score(X_valid, y_valid)]
    if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
    print(res)

m = RandomForestRegressor(n_jobs=1)

print('Fitting the model')
m.fit(X_train, y_train)

print_score(m)

Beispiel #15
0
 def __init__(self):
     super(KinoPlex, self).__init__()  # make init call for agentkit
     self.score = Scoring(self)
     self.sources = [src(self) for src in SourceBase.getAll()]
Beispiel #16
0
import nfldb
from scoring import Scoring

scorer = Scoring()
db = nfldb.connect()
q = nfldb.Query(db).game(season_year=2013, season_type='Regular')
# get game stats
for game in q.as_games():
    print game
    print game.week, game.home_team, game.home_score
    print game.week, game.away_team, game.away_score
    performances = nfldb.aggregate(game.play_players)
    for pp in performances:
        print pp.player.full_name, pp.player.position, scorer.get_score(pp)
    print
Beispiel #17
0
    def test02_scoring(self):
        candObj = Scoring()
        testset = [[{
            'cnt': 0,
            'word': '한국사물인터넷진흥'
        }, {
            'cnt': 204,
            'word': '협회'
        }],
                   [{
                       'cnt': 0,
                       'word': '한국사물인터넷'
                   }, {
                       'cnt': 57,
                       'word': '진흥'
                   }, {
                       'cnt': 204,
                       'word': '협회'
                   }],
                   [{
                       'cnt': 0,
                       'word': '한국사물'
                   }, {
                       'cnt': 2,
                       'word': '인터넷'
                   }, {
                       'cnt': 0,
                       'word': '진흥협회'
                   }],
                   [{
                       'cnt': 0,
                       'word': '한국사물'
                   }, {
                       'cnt': 2,
                       'word': '인터넷'
                   }, {
                       'cnt': 57,
                       'word': '진흥'
                   }, {
                       'cnt': 204,
                       'word': '협회'
                   }],
                   [{
                       'cnt': 0,
                       'word': '한'
                   }, {
                       'cnt': 19,
                       'word': '국사'
                   }, {
                       'cnt': 0,
                       'word': '물인터넷진흥협회'
                   }],
                   [{
                       'cnt': 0,
                       'word': '한'
                   }, {
                       'cnt': 19,
                       'word': '국사'
                   }, {
                       'cnt': 0,
                       'word': '물인터넷진흥'
                   }, {
                       'cnt': 204,
                       'word': '협회'
                   }],
                   [{
                       'cnt': 0,
                       'word': '한'
                   }, {
                       'cnt': 19,
                       'word': '국사'
                   }, {
                       'cnt': 0,
                       'word': '물인터넷'
                   }, {
                       'cnt': 57,
                       'word': '진흥'
                   }, {
                       'cnt': 204,
                       'word': '협회'
                   }],
                   [{
                       'cnt': 0,
                       'word': '한'
                   }, {
                       'cnt': 19,
                       'word': '국사'
                   }, {
                       'cnt': 0,
                       'word': '물'
                   }, {
                       'cnt': 2,
                       'word': '인터넷'
                   }, {
                       'cnt': 0,
                       'word': '진흥협회'
                   }],
                   [{
                       'cnt': 0,
                       'word': '한'
                   }, {
                       'cnt': 19,
                       'word': '국사'
                   }, {
                       'cnt': 0,
                       'word': '물'
                   }, {
                       'cnt': 2,
                       'word': '인터넷'
                   }, {
                       'cnt': 57,
                       'word': '진흥'
                   }, {
                       'cnt': 204,
                       'word': '협회'
                   }],
                   [{
                       'cnt': 1779,
                       'word': '한국'
                   }, {
                       'cnt': 0,
                       'word': '사물인터넷진흥협회'
                   }],
                   [{
                       'cnt': 1779,
                       'word': '한국'
                   }, {
                       'cnt': 0,
                       'word': '사물인터넷진흥'
                   }, {
                       'cnt': 204,
                       'word': '협회'
                   }],
                   [{
                       'cnt': 1779,
                       'word': '한국'
                   }, {
                       'cnt': 0,
                       'word': '사물인터넷'
                   }, {
                       'cnt': 57,
                       'word': '진흥'
                   }, {
                       'cnt': 204,
                       'word': '협회'
                   }],
                   [{
                       'cnt': 1779,
                       'word': '한국'
                   }, {
                       'cnt': 292,
                       'word': '사물'
                   }, {
                       'cnt': 0,
                       'word': '인터넷진흥협회'
                   }],
                   [{
                       'cnt': 1779,
                       'word': '한국'
                   }, {
                       'cnt': 292,
                       'word': '사물'
                   }, {
                       'cnt': 0,
                       'word': '인터넷진흥'
                   }, {
                       'cnt': 204,
                       'word': '협회'
                   }],
                   [{
                       'cnt': 1779,
                       'word': '한국'
                   }, {
                       'cnt': 292,
                       'word': '사물'
                   }, {
                       'cnt': 2,
                       'word': '인터넷'
                   }, {
                       'cnt': 0,
                       'word': '진흥협회'
                   }],
                   [{
                       'cnt': 1779,
                       'word': '한국'
                   }, {
                       'cnt': 292,
                       'word': '사물'
                   }, {
                       'cnt': 2,
                       'word': '인터넷'
                   }, {
                       'cnt': 57,
                       'word': '진흥'
                   }, {
                       'cnt': 204,
                       'word': '협회'
                   }],
                   [{
                       'cnt': 4,
                       'word': '한국사'
                   }, {
                       'cnt': 0,
                       'word': '물인터넷진흥협회'
                   }],
                   [{
                       'cnt': 4,
                       'word': '한국사'
                   }, {
                       'cnt': 0,
                       'word': '물인터넷진흥'
                   }, {
                       'cnt': 204,
                       'word': '협회'
                   }],
                   [{
                       'cnt': 4,
                       'word': '한국사'
                   }, {
                       'cnt': 0,
                       'word': '물인터넷'
                   }, {
                       'cnt': 57,
                       'word': '진흥'
                   }, {
                       'cnt': 204,
                       'word': '협회'
                   }],
                   [{
                       'cnt': 4,
                       'word': '한국사'
                   }, {
                       'cnt': 0,
                       'word': '물'
                   }, {
                       'cnt': 2,
                       'word': '인터넷'
                   }, {
                       'cnt': 0,
                       'word': '진흥협회'
                   }],
                   [{
                       'cnt': 4,
                       'word': '한국사'
                   }, {
                       'cnt': 0,
                       'word': '물'
                   }, {
                       'cnt': 2,
                       'word': '인터넷'
                   }, {
                       'cnt': 57,
                       'word': '진흥'
                   }, {
                       'cnt': 204,
                       'word': '협회'
                   }]]

        for param in range(3):
            ret = candObj.scoring(testset, option=param)

            for item in ret:
                print(item)

            self.assertEqual(5, len(ret))
 def setUp(self):
     self.subject = Scoring()
Beispiel #19
0
class Player:
    def __init__(self, player_agent, starting_cheques):
        self._player_agent = player_agent
        self._available_cheques = starting_cheques[:]
        self._unavailable_cheques = []
        self._scards = []
        self._scored_scards = []
        self._removed_scards = []
        self._counts = {c: 0 for c in Card if c != Card.Policeman}
        self._scoring_cheques = None
        self._scoring = Scoring(player_agent)

    @property
    def player_agent(self):
        return self._player_agent

    @property
    def round_is_over(self):
        return len(self._available_cheques) == 0

    @property
    def num_available_cheques(self):
        return len(self._available_cheques)

    @property
    def num_unavailable_cheques(self):
        return len(self._unavailable_cheques)

    @property
    def highest_cheque(self):
        if not self.round_is_over:
            return max(self._available_cheques)

    @property
    def cheque_total(self):
        cheques = self._available_cheques[:]  # copy
        cheques.extend(self._unavailable_cheques)
        return sum(c.value for c in cheques)

    @property
    def num_bodyguards(self):
        return self._counts[Card.Bodyguard]

    @property
    def num_thieves(self):
        return self._counts[Card.Thief]

    def num_cards(self, card_kind):
        return self._counts[card_kind]

    def get_final_scoring(self):
        return self._scoring

    def available_cheques(self):
        return self._available_cheques[:]  # copy

    def has_cheque_available(self, cheque):
        return cheque in self._available_cheques

    def refresh_cheques(self):
        self._available_cheques.extend(self._unavailable_cheques)
        self._unavailable_cheques = []

    def remove_available_cheque(self, cheque):
        self._available_cheques.remove(cheque)  # removes first occurrence

    def add_unavailable_cheque(self, cheque):
        self._unavailable_cheques.append(cheque)

    def total_cheque_value(self):
        return sum(self._available_cheques) + sum(self._unavailable_cheques)

    def gain_cards(self, cards, round, cheque_value, cheque_ordinal):
        gained = [
            ScoringCard(c, round, cheque_value, cheque_ordinal) for c in cards
        ]
        self._scards.extend(gained)
        for c in cards:
            self._counts[c] += 1

    def remove_cards(self, cards_to_remove):
        # remove player's cards one-for-one
        for c in cards_to_remove:
            if c not in pieces.REMOVABLE_CARDS:
                raise Exception(
                    'Trying to remove an unremovable card ({}) from a player.'.
                    format(c))
            cards = [sc.card for sc in self._scards]
            idx_to_remove = cards.index(c)
            sc_to_remove = self._scards[idx_to_remove]
            self._scards.remove(sc_to_remove)
            self._removed_scards.append(sc_to_remove)
            self._counts[c] -= 1

    def card_counts(self):
        return dict(self._counts)  # copy

    def do_round_scoring(self, min_bodyguard, max_bodyguard):
        self._scoring.score_round(min_bodyguard, max_bodyguard, self._scards)
        self._scoring.assign_round_card_scores(min_bodyguard, max_bodyguard,
                                               self._scards)

        self._scored_scards.extend(sc for sc in self._scards
                                   if sc.card in pieces.REMOVABLE_CARDS)
        self._scards = [
            sc for sc in self._scards if sc.card not in pieces.REMOVABLE_CARDS
        ]
        for card_type in pieces.REMOVABLE_CARDS:
            self._counts[card_type] = 0

    def do_game_end_scoring(self, min_money, max_money):
        cheques = self._available_cheques + self._unavailable_cheques
        self._scoring_cheques = [ScoringCheque(c) for c in cheques]
        self._scoring.score_businesses(self._scards)
        self._scoring.score_cheques(min_money, max_money,
                                    self._scoring_cheques)
        self._scoring.assign_business_card_scores(self._scards)
        self._scoring.assign_cheque_scores(min_money, max_money,
                                           self._scoring_cheques)
        self._scoring.attach_final_scoring_cards(self._scored_scards +
                                                 self._scards)
        self._scoring.attach_final_scoring_cheques(self._scoring_cheques)

    def __str__(self):
        s = '\n' + str(self._player_agent) + '\n'
        s += '  Available cheques: ' + ', '.join(
            str(c) for c in self._available_cheques) + '\n'
        s += '  Unavailable cheques: ' + ', '.join(
            str(c) for c in self._unavailable_cheques) + '\n'
        s += '  Gained cards: \n    ' + '\n    '.join(
            str(c) for c in self._scards) + '\n'
        s += '  Counts: ' + ', '.join(
            ['{} {}'.format(k.name, v) for k, v in self._counts.items() if v])
        return s
 def setUp(self):
     self.subject = Scoring('spanish')