-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScorer.h
More file actions
55 lines (49 loc) · 1.37 KB
/
Scorer.h
File metadata and controls
55 lines (49 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef SCORER_H
#define SCORER_H
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <unordered_map>
#include "Buffer.h"
class Scorer {
protected:
std::unordered_map<std::string, int> dictionary;
public:
Scorer(const std::unordered_map<std::string, int>& dict) : dictionary(dict) {}
virtual ~Scorer() {}
virtual double calculateScore(const Buffer& buffer) = 0;
};
class WordCountScorer : public Scorer {
public:
using Scorer::Scorer;
double calculateScore(const Buffer& buffer) override {
if (!buffer.getData()) return 0.0;
double score = 0;
std::stringstream ss(buffer.getData());
std::string token;
while (ss >> token) {
if (dictionary.find(token) != dictionary.end()) {
score += (dictionary.at(token) > 0) ? 1.0 : -1.0;
}
}
return score;
}
};
class WeightedScorer : public Scorer {
public:
using Scorer::Scorer;
double calculateScore(const Buffer& buffer) override {
if (!buffer.getData()) return 0.0;
double totalScore = 0;
std::stringstream ss(buffer.getData());
std::string token;
while (ss >> token) {
if (dictionary.find(token) != dictionary.end()) {
totalScore += dictionary.at(token);
}
}
return totalScore;
}
};
#endif