public async Task <IHttpActionResult> Preview(string ScenarioName, string IntentName) { LSTMDecSurfaceRealizer rnnlg = new LSTMDecSurfaceRealizer(); int number = unique_key++; string svDictPath = workDir + @"\train\" + number.ToString() + "slot_value.txt"; string tokenPath = workDir + @"\train\" + number.ToString() + "token.txt"; string modelPath = workDir + @"\models\" + number.ToString() + "lm.dnn"; Scenario scenario = lstmDb.Scenarios.SingleOrDefault(e => e.name == ScenarioName); if (scenario == null) { return(BadRequest("No Such Scenario")); } Intent intent = lstmDb.Intents.SingleOrDefault(e => e.scenarioID == scenario.id && e.name == IntentName); if (intent == null) { return(BadRequest("No Such Intent")); } List <Sample> samples = lstmDb.Samples.Where(e => e.intentID == intent.id && e.response == null && e.template == null).ToList <Sample>(); List <Sample> temp_samples = new List <Sample>(); if (intent.sv_dict == null || intent.token == null || intent.model == null) { return(BadRequest("Please train model first")); } System.IO.File.WriteAllBytes(svDictPath, intent.sv_dict); System.IO.File.WriteAllBytes(tokenPath, intent.token); System.IO.File.WriteAllBytes(modelPath, intent.model); rnnlg.Initialize(modelPath, tokenPath, svDictPath); var slotDescriptions = intent.mySlotDescriptions; foreach (Sample sample in samples) { IList <String> templateStringList = new List <String>(); IList <String> responseStringList = new List <String>(); Dictionary <String, String> featureList = new Dictionary <String, String>(); string svPair = sample.slotValuePair; List <SVPair> svpairs = JsonConvert.DeserializeObject <List <SVPair> >(svPair); foreach (SVPair svpair in svpairs) { bool condition = false; foreach (SlotDescription des in slotDescriptions) { if (des.name == svpair.slot && des.condition == true) { condition = true; break; } } if (condition) { featureList.Add((svpair.slot + "(@C)"), svpair.value); } else { featureList.Add(svpair.slot, svpair.value); } } rnnlg.ApplyBeamSearch(featureList, ref templateStringList, ref responseStringList, 1); Sample temp = new Sample(sample); temp.response = responseStringList[0]; temp.template = templateStringList[0]; temp_samples.Add(temp); } await lstmDb.SaveChangesAsync(); try { System.IO.File.Delete(svDictPath); System.IO.File.Delete(tokenPath); System.IO.File.Delete(modelPath); } catch { } return(Ok(temp_samples)); }
public async Task <IHttpActionResult> Test(string ScenarioName, string IntentName, int n) { List <string> topn = new List <string>(); LSTMDecSurfaceRealizer rnnlg = new LSTMDecSurfaceRealizer(); int number = unique_key++; string svDictPath = workDir + @"\train\" + number.ToString() + "slot_value.txt"; string tokenPath = workDir + @"\train\" + number.ToString() + "token.txt"; string modelPath = workDir + @"\models\" + number.ToString() + "lm.dnn"; Scenario scenario = lstmDb.Scenarios.SingleOrDefault(e => e.name == ScenarioName); if (scenario == null) { return(BadRequest("No Such Scenario")); } Intent intent = lstmDb.Intents.SingleOrDefault(e => e.scenarioID == scenario.id && e.name == IntentName); if (intent == null) { return(BadRequest("No Such Intent")); } if (intent.sv_dict == null || intent.token == null || intent.model == null) { return(BadRequest("Please train model first")); } System.IO.File.WriteAllBytes(svDictPath, intent.sv_dict); System.IO.File.WriteAllBytes(tokenPath, intent.token); System.IO.File.WriteAllBytes(modelPath, intent.model); try { rnnlg.Initialize(modelPath, tokenPath, svDictPath); var slotDescriptions = intent.mySlotDescriptions; IList <String> templateStringList = new List <String>(); IList <String> responseStringList = new List <String>(); Dictionary <String, String> featureList = new Dictionary <String, String>(); string svPair = await Request.Content.ReadAsStringAsync(); List <SVPair> svpairs = JsonConvert.DeserializeObject <List <SVPair> >(svPair); foreach (SVPair svpair in svpairs) { bool condition = false; foreach (SlotDescription des in slotDescriptions) { if (des.name == svpair.slot && des.condition == true) { condition = true; break; } } if (condition) { featureList.Add((svpair.slot + "(@C)"), svpair.value); } else { featureList.Add(svpair.slot, svpair.value); } } //return Ok(featureList); rnnlg.ApplyBeamSearch(featureList, ref templateStringList, ref responseStringList, n); await lstmDb.SaveChangesAsync(); Dictionary <String, IList <String> > answer = new Dictionary <string, IList <string> >(); answer.Add("template", templateStringList); answer.Add("response", responseStringList); File.Delete(svDictPath); File.Delete(tokenPath); File.Delete(modelPath); return(Ok(answer)); } catch { File.Delete(svDictPath); File.Delete(tokenPath); File.Delete(modelPath); return(BadRequest("Test failed")); } }