在计算置信度时,您会生成置信度的加权平均值,以便为第一个字符赋予更大的权重,而对最后一个字符赋予更少的权重。
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
using namespace std;
double getWeightedConfidence(vector<pair<char /* character */, double /*confidence of that character */>> word) {
if (word.empty()) {
return 1.0;
}
double confidence = 0;
if (isdigit(word[0].first)) {
// okay it is a number
double weight = 1;
double sumOfWeights = 0;
for (const auto &c : word) {
confidence += c.second * weight;
sumOfWeights += weight;
weight /= 10; // you can decay it by whatever number you want based on how much do you think next digit is less valueble then previous
}
confidence /= sumOfWeights;
} else {
// not a number - just calculate a normal average
for (const auto &c : word) {
confidence += c.second;
}
confidence /= word.size();
}
return confidence;
}
int main() {
vector<pair<char, double>> number_with_first_digit_wrong;
number_with_first_digit_wrong.emplace_back('7', 0.1);
number_with_first_digit_wrong.emplace_back('4', 0.9);
number_with_first_digit_wrong.emplace_back('6', 0.9);
number_with_first_digit_wrong.emplace_back('2', 0.9);
number_with_first_digit_wrong.emplace_back('.', 0.9);
number_with_first_digit_wrong.emplace_back('9', 0.9);
vector<pair<char, double>> number_with_last_digit_wrong;
number_with_last_digit_wrong.emplace_back('7', 0.9);
number_with_last_digit_wrong.emplace_back('4', 0.9);
number_with_last_digit_wrong.emplace_back('6', 0.9);
number_with_last_digit_wrong.emplace_back('2', 0.9);
number_with_last_digit_wrong.emplace_back('.', 0.9);
number_with_last_digit_wrong.emplace_back('9', 0.1);
cout << getWeightedConfidence(number_with_first_digit_wrong) << " " << getWeightedConfidence(number_with_last_digit_wrong) << endl;
return 0;
}
一些简单的结果:
0.179999 - 当 0.1 是第一个数字的置信度时(其他为 0.9)
0.899993 - 当 0.1 是最后一位数字的置信度(其他为 0.9)时
如果您认为某些职位比其他职位更有价值,您可以指定不同的权重。