2014-03-13 41 views
3

我写了一个程序,从用户处收集问题。然后它将该问题与预先定义的问题列表进行匹配并返回答案。它应该是准确的,并且只与接近(模糊匹配)的问题或用户输入的问题相匹配。字符串/句子最近匹配算法不起作用?

我SSSCE:

http://ideone.com/JTcF73

代码:

#include <iostream> 
#include <cstdint> 
#include <algorithm> 
#include <numeric> 
#include <functional> 

int min_index(const std::vector<int>& list) 
{ 
    int index = 0; 
    int smallest = list[0]; 

    for (size_t i = 0; i < list.size(); ++i) { 
     if (list[i] < smallest) { 
      smallest = list[i]; 
      index = i; 
     } 
    } 
    return index; 
} 

std::uint32_t LevenshteinDistance(const std::string &First, const std::string &Second) 
{ 
    const std::size_t FirstLength = First.size(); 
    const std::size_t SecondLength = Second.size(); 

    std::vector<std::uint32_t> Current(SecondLength + 1); 
    std::vector<std::uint32_t> Previous(SecondLength + 1); 

    std::size_t I = 0; 
    std::generate(Previous.begin(), Previous.end(), [&] {return I++; }); 

    for (I = 0; I < FirstLength; ++I) 
    { 
     Current[0] = I + 1; 

     for (std::size_t J = 0; J < SecondLength; ++J) 
     { 
      auto Cost = First[I] == Second[J] ? 0 : 1; 
      Current[J + 1] = std::min(std::min(Current[J] + 1, Previous[J + 1] + 1), Previous[J] + Cost); 
     } 

     Current.swap(Previous); 
    } 
    return Previous[SecondLength]; 
} 

std::vector<std::string> questions = 
{ 
    "What is the most popular program at GBC?", 
    "How much is the tuition at GBC?", 
    "Do I have to pay my fees before I can register?", 
    "What are my fee payment options?", 
    "How do I know when I'm allowed to register?", 
    "How do I add and/or drop courses from my timetable?", 
    "What do I do if I can't find my PASSWORD?", 
    "How do I withdraw from a program?", 
    "What are the college policies?", 
    "How much math do I need to know?", 
    "What is the program code for computer programming?", 
    "What is stu-view?", 
    "What is the college best known for?", 
    "How easy is it to find work after program completion?", 
    "What if I plan to continue my education after?" 
}; 

std::vector<std::string> answers = 
{ 
    "Fashion", 
    "3000 a semester", 
    "Yes you have to pay the fees before registering", 
    "You may pay online on your student account through the student portal", 
    "You may register two weeks or more before the start of the program", 
    "You may drop courses from online through the student portal", 
    "You can call ... and an agent will assist you", 
    "You may withdraw using the student portal online", 
    "They are located at the following link...", 
    "It depends on the program you are entering", 
    "T127 is the code for computer science", 
    "Stu-View is a student portal to manage student account and view marks.", 
    "The cafeteria food", 
    "Depends on the field of work and timing", 
    "You may do so within three years after program completion" 
}; 

int main() 
{ 
    std::string user_question = "program"; 

    std::vector<int> distances = std::vector<int>(questions.size(), 0); 

    for (size_t I = 0; I < questions.size(); ++I) 
    { 
     int dist = LevenshteinDistance(user_question, questions[I]); 
     distances[I] = dist; 
    } 

    std::cout<<"Distance:  "<<distances[min_index(distances)]<<"\n"; 
    std::cout<<"User-Question: "<<user_question<<"\n"; 
    std::cout<<"Question-Key: "<<questions[min_index(distances)]<<"\n"; 
    std::cout<<"Answer-Value: "<<answers[min_index(distances)]<<"\n"; 

    return 0; 
} 

所以在上面,用户输入"program"。它应该找到问题列表最接近的匹配并返回相应的答案..

但是,它打印出:

Distance:  17 
User-Question: program 
Question-Key: What is stu-view? 
Answer-Value: Stu-View is a student portal to manage student account and view marks. 

它应该有更好的结果或准确性,但它似乎并不在乎某个句子是否包含用户输入的关键字:S适用于小型案例,但对于大型数据库或上述有5个以上的句子,这是一个艰难的时间..尤其是与关键字少; l

我做错了什么?任何想法如何我可以修复它,并使其更加准确?我尝试了HammingDistance,类似的结果..

+0

好了,'编辑距离(a,b)'永远不会大于'max(a.size(),b.size())'。 17的距离是正确的答案,因为“什么是stu-view?”只有17个字符长。你的程序根本没有任何条件,它不会**找到答案。 –

+1

微小的文体建议:你可以用'std :: iota'替换你的'std :: generate',并将'I'的声明向下移动到'for'循环中。 (对不起,我正在进行一场讨论,让'iota'知道......) – screwnut

回答

3

与其他字符串相比,"program""What is stu-view?"都相当短。尽管"program"这个词很常见,但将"program"更改为"What is stu-view?"比将"program"更改为"What is the most popular program at GBC?"更容易。

我在做什么错?

我不认为你做错了什么。如果你对结果不满意,这意味着你现在的形式主义(尽量减少Levenshtein距离)并不是你想要的。

您可以选择更多本地解决方案:符号化的字符串,计算两两Levenshtein距离之间的话,然后合并结果(平均,SUP INF ......)

更好的解决方案将需要做一些参考书目(可能是无监督的机器学习主题)