private static void TestColumnTableQA() { Parser parser = new Parser(); PatternBased pattern_qa = new PatternBased(); List <string> questions = new List <string> { //"肯尼思·洛纳根导演过哪些电影", // 肯尼思·洛纳根 splited //"你的名字是哪个国家拍的", // 你的名字 in NER, but 你的名字。in CellID //"十二怒汉是讲什么的", // have no 十二怒汉 //"活着是讲什么的", //"你的名字。是讲什么的", // the period //"赌神是讲什么的", //"天下无贼是谁导演的", //"林家栋拍过什么电影", //拍 act? direct? //"大话西游之大圣娶亲是什么时候拍的", //"有木有徐克的", //"美人鱼上映了吗", //"绝世高手上映了吗", //"建军大业什么时候上映的", //there is no 建军大业 in OSearch "我想看张艺谋导演的电影", "有什么类型的电影呢", "战狼什么时候上映的", //there is no 建军大业 in OSearch "最近有什么好电影" }; foreach (string question in questions) { Query query = new Query(question); parser.PosTagging(ref query); Console.WriteLine("Question:" + question); Console.WriteLine("Answer:"); Console.WriteLine(KBQA.DoKBQA(query, parser)); Console.WriteLine(); } }
private static void TestColumnTableQuery() { List <ColumnTableQueryInfo> info_list = new List <ColumnTableQueryInfo> { new ColumnTableQueryInfo(KBQAEntityType.Movie, "墨攻", "PublishDate"), new ColumnTableQueryInfo(KBQAEntityType.Movie, "墨攻", "Rating"), new ColumnTableQueryInfo(KBQAEntityType.Movie, "墨攻", "Genres"), new ColumnTableQueryInfo(KBQAEntityType.Movie, "墨攻", "Country"), new ColumnTableQueryInfo(KBQAEntityType.Movie, "墨攻", "Description"), new ColumnTableQueryInfo(KBQAEntityType.Movie, "墨攻", "Artists:Name"), new ColumnTableQueryInfo(KBQAEntityType.Movie, "墨攻", "Directors:Name"), new ColumnTableQueryInfo(KBQAEntityType.Celebrity, "刘德华", "Act:Name"), new ColumnTableQueryInfo(KBQAEntityType.Celebrity, "张艺谋", "Direct:Name"), new ColumnTableQueryInfo(KBQAEntityType.RecentMovie, "", "Name"), new ColumnTableQueryInfo(KBQAEntityType.IsPublish, "美人鱼", "PublishDate"), new ColumnTableQueryInfo(KBQAEntityType.IsPublish, "绝世高手", "PublishDate"), }; List <string> res; foreach (var item in info_list) { res = KBQA.GetColumnData(item.type, item.entity, item.property); Console.WriteLine(string.Join(",", res.ToArray())); Console.WriteLine(); } }
private void ClarifyArtistDirector() { List <string> duplicate_name = new List <string>(); foreach (string art in session.carried_artist) { foreach (string dir in session.carried_director) { if (art.Equals(dir)) { duplicate_name.Add(art); } } } if (duplicate_name.Count != 0) { Console.WriteLine("Start to Clarify Artist Director"); foreach (string name in duplicate_name) { Utils.WriteMachine($"QAQ有一点儿疑惑...,因为 {name} 既是演员也是导演呢..."); while (true) { Utils.WriteMachine("你想看他拍的还是他演的呢?"); string query_str = Utils.ReadLine(); Utils.WriteQuery(query_str); Query query = new Query(query_str); parser.PosTagging(ref query); parser.ParseAllTag(ref query); int is_artist_director = parser.isArtistOrDirector(query); if (is_artist_director != -1) { if (is_artist_director == 1) { session.carried_director.Remove(name); } else { if (is_artist_director == 2) { session.carried_artist.Remove(name); } } if (session.carried_artist.Count == 0) { session.is_considerd[ParseStatus.Artist] = false; } if (session.carried_director.Count == 0) { session.is_considerd[ParseStatus.Director] = false; } break; } if (KBQA.DoKBQA(query, parser)) { continue; } } } } }
private void ConfirmSession() { Console.WriteLine("Going to Confirm Session"); Utils.WriteMachine("我知道你想看什么啦"); bool jump_show_candidate_dueto_kbqa = false; int offset = 0; int confirm_turn = 1; while (true) { if (!jump_show_candidate_dueto_kbqa) { if (confirm_turn > 3) { break; } List <string> movies = new List <string>(); for (int j = offset; j < offset + confirm_turn && j < session.candidate_movies.Count; j++) { movies.Add(session.candidate_movies[j].name); } if (movies.Count < 1) { break; } Utils.WriteMachine("你想看 " + String.Join(" 或者 ", movies.ToArray()) + " 吗"); confirm_turn++; offset += confirm_turn; } string query_str = Utils.ReadLine(); Utils.WriteQuery(query_str); Query query = new Query(query_str); parser.PosTagging(ref query); if (parser.isDenyCandidate(query)) { jump_show_candidate_dueto_kbqa = false; continue; } if (parser.isAcceptCandidate(query)) { Utils.WriteMachine("Have Fun~~~"); return; } if (KBQA.DoKBQA(query, parser)) { jump_show_candidate_dueto_kbqa = true; } else { jump_show_candidate_dueto_kbqa = true; Utils.WriteUnknow("数据库中没有相关的答案...", query.raw_query); } } Utils.WriteUnknow("好像找不到你喜欢看的电影...", "好像找不到你喜欢看的电影..."); Utils.WriteMachine(new string('=', 24)); }
public void DialogFlow(string input) { // begin session.parse_status = ParseStatus.All; string query_str = ""; while (true) { Query query; // get query. if it is the very beginning, then taking the parameter as input if (session.parse_status == ParseStatus.All) { if (string.IsNullOrWhiteSpace(input)) { query_str = Utils.ReadLine(); Utils.WriteQuery(query_str); } else { query_str = input; input = null; } query = new Query(query_str); parser.PosTagging(ref query); // movie recommendation trigger // don't need anymore due to Xiaoice API Connection if (!parser.isAboutMovie(query)) { Utils.WriteMachine(new string('=', 24)); Utils.WriteMachine("\n"); return; } } else { query_str = Utils.ReadLine(); Utils.WriteQuery(query_str); query = new Query(query_str); Query query_kbqa = new Query(query_str); parser.PosTagging(ref query_kbqa); if (KBQA.DoKBQA(query_kbqa, parser)) { continue; } parser.PosTagging(ref query); } // query parse according to parse status switch (session.parse_status) { case ParseStatus.All: parser.ParseAllTag(ref query); break; case ParseStatus.Movie: parser.ParseMovieName(ref query); break; case ParseStatus.Artist: parser.ParseArtistName(ref query); break; case ParseStatus.Director: parser.ParseDirectorName(ref query); break; case ParseStatus.Country: parser.ParseCountryName(ref query); break; case ParseStatus.Genre: parser.ParseGenreName(ref query); break; case ParseStatus.PublishDate: parser.ParsePublishDate(ref query); break; //case ParseStatus.Rating: // parser.ParseRating(ref query); // break; //case ParseStatus.Duration: // parser.ParseDuration(ref query); // break; default: Utils.WriteError("error parse status!"); break; } // refresh session status using user query session.RefreshSessionStatus(query); ClarifyArtistDirector(); // refresh session movie candidate status GetAllResult(ref session); // is recommendation end or is user accept the kbqa answer if (isRecommendationEnd(session) || parser.isAcceptCandidate(query)) { if (parser.isAcceptCandidate(query)) { Utils.WriteMachine("Have Fun~~~"); return; } if (session.is_considerd[ParseStatus.Movie] && session.candidate_movies.Count > 0) { Utils.WriteMachine("想看" + session.candidate_movies[0].name + "啊"); Utils.WriteMachine("眼光不错哦"); } else { // if it is end, then show some movie candidate, let user confirm the result ConfirmSession(); return; } } else { // in this trun we asked some information, but user did not provide if (session.parse_status == ParseStatus.Artist || session.parse_status == ParseStatus.Director || session.parse_status == ParseStatus.Country || session.parse_status == ParseStatus.Genre || session.parse_status == ParseStatus.PublishDate) { if (session.is_considerd[session.parse_status] != true) { Utils.WriteUnknow("Pardon me", query.raw_query); continue; } } if (session.parse_status == ParseStatus.All) { session.parse_status = MakeClearParseStatus(session); } // using current turn status(session.parse_status) to compute the Transition(next turn) status, a transition matrix requeried. ParseStatus nextturn_status = GetTransitionStatus(session); List <string> answer_entity_candidate = new List <string>(); // response according to the nextturn_status we just chosen. switch (nextturn_status) { //case ParseStatus.All: // answer_entity_candidate = AnalyseAll(session); // break; case ParseStatus.Artist: answer_entity_candidate = AnalyseArtistName(session); break; case ParseStatus.Director: answer_entity_candidate = AnalyseDirectorName(session); break; case ParseStatus.Country: answer_entity_candidate = AnalyseCountryName(session); break; case ParseStatus.Genre: answer_entity_candidate = AnalyseGenreName(session); break; case ParseStatus.PublishDate: answer_entity_candidate = AnalysePublishDate(session); break; default: Utils.WriteError("error turn status!"); break; } // answer and go to the next turn Console.WriteLine("from {0} transite to {1}", session.parse_status.ToString(), nextturn_status.ToString()); string answer = AnswerGenerator.AnswerIt(answer_entity_candidate, session, nextturn_status); Utils.WriteMachine(answer); session.parse_status = nextturn_status; } } }