test.cpp

来自「自然语言处理句子匹配算法」· C++ 代码 · 共 79 行

CPP
79
字号
// Artificial Intelligence: Sentence Matching
#include <iostream>
#include "matcher.h"
using std::string;
using std::vector;

void get_distance();
void find_best_match();

template<typename T>
void print_array(vector<T> array, size_t array_size)
{
	for(int i = 0; i < array_size; ++i)
	{
		std::cout << "   " << array[i] << std::endl;
	}
}


int main() {
	find_best_match();
	return 0;
}

// measures distance between strings
void get_distance() {
	std::cout << "\nMeasuring distances between strings";
	char *str1 = new char[128];
	char *str2 = new char[128];
	std::cout << "\n   please enter some text: ";
	std::cin.getline(str1, 128);
	std::cout << "   enter pattern to search: ";
	std::cin.getline(str2, 128);
	std::cout << "   distance = " << find_distance(str1, str2) << std::endl;
	delete str1;
	delete str2;
}

// shows how to find the best matching sentence
// between a list of sentences
void find_best_match() {
	std::cout << "\n\t\t\t\tFINDING BEST MATCH\n\n\n";
	std::cout << "Sentences:\n";
	vstring vSentences;
	vSentences.push_back("i wont tell you my name.");
	vSentences.push_back("i dont think that i know your name.");
	vSentences.push_back("what difference would it make if i told you my name?");
	vSentences.push_back("can you tell me your name?");
	vSentences.push_back("do you know my name?");
	vSentences.push_back("how are you today?");
	vSentences.push_back("where are you from?");
	vSentences.push_back("what is your name?");
	vSentences.push_back("please,tell me your name.");
	vSentences.push_back("do you know my name?");
	print_array(vSentences, vSentences.size());
	string sSearch = "so,your name is?";
	std::cout << "\nsearch: " << sSearch << std::endl;
	string best_match;
	float match_val;
	float best_match_val = 2;
	int nSentencesNum = vSentences.size();
	for(int i = 0; i < nSentencesNum; ++i) {
		match_val = match(sSearch.c_str(), vSentences[i].c_str());
		if(match_val < best_match_val)
		{
			best_match_val = match_val;
			best_match = vSentences[i];
		}
		std::cout << "   \nsentence " << i + 1 << ": " << vSentences[i] << std::endl;
		std::cout << "   match val:= " << match_val << std::endl;
		std::cout << "   best match val:= " << best_match_val << std::endl;
		std::cout << "   best match: " << best_match << std::endl;
	}
	std::cout << "\n\nBest match: " << best_match << std::endl << std::endl;
}



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?