-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistinct.h
More file actions
40 lines (34 loc) 路 1.2 KB
/
Copy pathdistinct.h
File metadata and controls
40 lines (34 loc) 路 1.2 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
#ifndef SINGLE_TURN_CONVERSATION_DISTINCT_H
#define SINGLE_TURN_CONVERSATION_DISTINCT_H
#include <vector>
#include <string>
#include <iostream>
#include <set>
using namespace std;
const vector<string> ngram_sentence_level(const vector<string> &sentence, int gram_len) {
vector<string> ngram_vec;
for(int i = 0; i < sentence.size() + 1 - gram_len; ++i) {
string ngram("");
for (int j = i; j <= i + gram_len - 1; ++j) {
ngram += sentence.at(j);
}
ngram_vec.emplace_back(ngram);
}
return ngram_vec;
}
float computeDistinct(const vector<vector<string>> &sentences, int gram_len) {
float sum = 0.0f;
for (const auto & sentence : sentences) {
if (sentence.size() < gram_len) {
sum += 0.0f;
} else {
const auto &ngram_vec = ngram_sentence_level(sentence, gram_len);
int word_cnt = ngram_vec.size();
set<string>unique_set(ngram_vec.begin(), ngram_vec.end());
int unique_cnt = unique_set.size();
sum += (unique_cnt * 1.0f / word_cnt);
}
}
return sum / sentences.size();
}
#endif // SINGLE_TURN_CONVERSATION_DISTINCT_H