private static Collection <CompletionResult> CompleteParameterValues( KeywordResult keyword, Token precedingToken) { if (keyword.Schema == ResourceKeywordSchema.Value) { return(null); } string parameterName = precedingToken.Text.Substring(1); IEnumerable <string> values = keyword.Schema.GetParameterValues(keyword.Frame, parameterName); if (values is null) { return(null); } var completions = new Collection <CompletionResult>(); foreach (string value in values) { completions.Add( new CompletionResult( value, value, CompletionResultType.ParameterValue, value)); } return(completions); }
private static Collection <CompletionResult> GetCompletions( Ast ast, IReadOnlyList <Token> tokens, IScriptPosition cursorPosition, Hashtable options, out bool clobberCompletions) { KeywordResult?result = GetCurrentKeyword(ast, tokens, cursorPosition); Token lastToken = result?.Frame.ParentContext?.LastToken; if (lastToken is null) { clobberCompletions = false; return(null); } KeywordResult keyword = result.Value; switch (lastToken.Kind) { case TokenKind.NewLine: case TokenKind.Semi: case TokenKind.Pipe: case TokenKind.LParen: case TokenKind.LCurly: case TokenKind.AtParen: case TokenKind.DollarParen: clobberCompletions = false; return(CompleteKeywords(keyword)); case TokenKind.Identifier: case TokenKind.Command: if (keyword.Frame.ParentContext.HasCommandAtPosition(cursorPosition)) { clobberCompletions = false; return(CompleteKeywords(keyword)); } clobberCompletions = true; return(CompleteParameters(keyword, cursorPosition)); case TokenKind.Generic: if (lastToken.Extent.EndOffset == cursorPosition.Offset) { clobberCompletions = true; return(CompleteParameters(keyword, cursorPosition)); } break; case TokenKind.Parameter: clobberCompletions = true; return(CompleteParameters(keyword, cursorPosition)); } clobberCompletions = false; return(null); }
internal override void Detect(Lexer l) { if (!(l.Char == ' ' || (l.Char >= '0' && l.Char <= '9') || (l.Char >= 'a' && l.Char <= 'z') || (l.Char >= 'A' && l.Char <= 'Z') || l.Char == '\n' || l.Char == '\t' || l.Char == '\r' || l.Char == ';' || l.Char == '_' || l.Char == ':' || l.Char == '*' || l.Char == '<' || l.Char == '>') && !l.Text.Contains("=")) { l.ForceExclude(); // contains characters we can't accept. return; } if (!(l.GetParent() is ClassDefinitionToken)) { l.ForceExclude(); // not within a class. return; } KeywordResult res = Keywords.GetKeywords(l.Text); if (res.Keywords.Count > 0 && res.PossibleKeyword == false) { // Now use regular expression checking against the substring starting // at res.DeclIndex to determine whether it's a variable declaration. string decl = l.Text.Substring(res.DeclIndex).Trim(); if (!decl.EndsWith(";") && !decl.EndsWith("{")) { return; // Skip if we don't have a terminating character. } Regex r = new Regex( "(?<Type>[a-zA-Z][a-zA-z0-9_\\:\\<\\>]+[ \t\r\n\\*]+)?" + "(?<Name>[a-zA-Z][a-zA-z0-9_]*[ \t\r\n]*)" + "(?<Assign>\\=[ \t\r\n]*((\\\".*\\\")|([a-zA-Z][a-zA-z0-9_\\:\\<\\>]+)|([0-9_.]*)))?;" ); Match m = r.Match(decl); if (m.Success) { // It's a variable declaration. l.TakeOwnership(); if (m.Groups["Assign"].Success) { l.AddNode(new ClassVariableDeclarationNode(res.Keywords, m.Groups["Type"].Value.Trim(), m.Groups["Name"].Value.Trim(), m.Groups["Assign"].Value.TrimStart('=').TrimStart())); } else { l.AddNode(new ClassVariableDeclarationNode(res.Keywords, m.Groups["Type"].Value.Trim(), m.Groups["Name"].Value.Trim(), "")); } l.EndOwnership(); } else { // A different kind of declaration that we aren't handling... l.ForceExclude(); } } }
internal override void Detect(Lexer l) { if (!(l.Char == ' ' || (l.Char >= '0' && l.Char <= '9') || (l.Char >= 'a' && l.Char <= 'z') || (l.Char >= 'A' && l.Char <= 'Z') || l.Char == '\n' || l.Char == '\t' || l.Char == '\r' || l.Char == '{' || l.Char == '}' || l.Char == ':' || l.Char == '_' || l.Char == ';')) { l.ForceExclude(); // contains characters we can't accept. return; } if (!(l.GetParent() is ClassDefinitionToken)) { l.ForceExclude(); // not within a class. return; } KeywordResult res = null; if (l.Text.IndexOf('{') != -1) { res = Keywords.GetKeywords(l.Text.Substring(0, l.Text.IndexOf('{'))); } else { res = Keywords.GetKeywords(l.Text); } if (res.Keywords.Count > 0 && res.PossibleKeyword == false && res.Keywords.Contains("property")) { // Now use regular expression checking against the substring starting // at res.DeclIndex to determine whether it's a variable declaration. string decl = l.Text.Substring(res.DeclIndex).Trim(); if (!decl.EndsWith("}")) { return; // Skip if we don't have a terminating character. } Regex r = new Regex("(?<PropertyName>[a-zA-Z_][a-zA-z0-9_]*)[ \t\r\n]*\\{(?<PropertyContents>[^\\}]*)\\}"); Match m = r.Match(decl); if (m.Success) { // It's a property declaration. l.TakeOwnership(); l.AddNode(new DirectNode("\n")); l.AddNode(new ClassPropertyDeclarationNode(res.Keywords, m.Groups["PropertyName"].Value, m.Groups["PropertyContents"].Value.Trim())); l.EndOwnership(); } else if (decl.IndexOf('{') != -1 && decl.IndexOf('}') != -1) { // We can only force exclude if there's an opening bracket // and a closing bracket and we still don't match. l.ForceExclude(); } } }
private void CompareGridRow(List <KeywordGridRow> gridRows, List <KeywordResult> KeywordResults) { Debug.Assert(gridRows.Count == KeywordResults.Count); for (int i = 0; i < gridRows.Count; i++) { KeywordResult keywordResult = KeywordResults[i]; int gridRowIndex = gridRows.FindIndex((x) => x.Keyword == keywordResult.Keyword); Debug.Assert(gridRowIndex != -1); KeywordGridRow gridRow = gridRows[gridRowIndex]; Debug.Assert(int.Parse(gridRow.Count) == keywordResult.Occurences); Debug.Assert(gridRow.Keyword == keywordResult.Keyword); } }
private static Collection <CompletionResult> CompleteParameters( KeywordResult keyword, IScriptPosition cursorPosition) { if (keyword.Schema.ShouldUseDefaultParameterCompletions) { return(null); } // If we're still on the last token, we need to look further back Token lastToken = keyword.Frame.ParentContext.LastToken; if (cursorPosition.Offset == lastToken.Extent.EndOffset) { lastToken = keyword.Frame.ParentContext.Tokens[keyword.Frame.ParentContext.LastTokenIndex - 1]; } return(lastToken.Kind == TokenKind.Parameter ? CompleteParameterValues(keyword, lastToken) : CompleteParameterNames(keyword)); }
private static Collection <CompletionResult> CompleteParameterNames( KeywordResult keyword) { IEnumerable <string> parameterNames = keyword.Schema.GetParameterNames(keyword.Frame); if (parameterNames is null) { return(null); } Token lastToken = keyword.Frame.ParentContext.LastToken; string prefix = lastToken.Kind == TokenKind.Parameter ? lastToken.Text.Substring(1) : null; var completions = new Collection <CompletionResult>(); foreach (string parameterName in parameterNames) { if (prefix != null && !parameterName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) { continue; } string parameterType = keyword.Schema.GetParameterType(keyword.Frame, parameterName); string completionText = $"-{parameterName}"; string completionToolTip = $"[{parameterType}] {parameterName}"; completions.Add( new CompletionResult( completionText, parameterName, CompletionResultType.ParameterName, completionToolTip)); } return(completions); }
private static Collection <CompletionResult> CompleteKeywords(KeywordResult keyword) { Token lastToken = keyword.Frame.ParentContext.LastToken; string keywordPrefix = lastToken.Kind == TokenKind.Identifier ? lastToken.Text : null; var completions = new Collection <CompletionResult>(); foreach (KeyValuePair <string, DslKeywordSchema> innerKeyword in keyword.Schema.GetInnerKeywords(keyword.Frame)) { string keywordName = innerKeyword.Key; if (keywordPrefix != null && !keywordName.StartsWith(keywordPrefix, StringComparison.OrdinalIgnoreCase)) { continue; } completions.Add(new CompletionResult(keywordName, keywordName, CompletionResultType.Command, keywordName)); } return(completions); }
/// <summary> /// Converts keyword result to RF XmlRpc Structure /// </summary> public static XmlRpcStruct ToXmlRpcResult(KeywordResult kwresult) { var result = new XmlRpcStruct(); //add status result.Add("status", kwresult.status.ToString()); //add error, traceback, output result.Add("error", kwresult.error); result.Add("traceback", kwresult.traceback); result.Add("output", kwresult.output); //add return if ([email protected]().Equals(typeof(System.Int64))) { //64bit int has to be returned as string result.Add("return", [email protected]()); } else { result.Add("return", kwresult.@return); } //check error type if (kwresult.status == KeywordStatus.FAIL) { if (kwresult.errortype == KeywordErrorTypes.Continuable) { //continuable error result.Add("continuable", true); } if (kwresult.errortype == KeywordErrorTypes.Fatal) { //fatal error result.Add("fatal", true); } } return(result); }
private LexiconSpeechResult CreateSpeechResult(SpeechRecognitionResult watsonResult, float realtimeStart) { if (watsonResult.alternatives.Length == 0) { return(null); } LexiconSpeechResult speechResult = new LexiconSpeechResult(); SpeechRecognitionAlternative bestAlternative = watsonResult.alternatives[0]; speechResult.Transcript = bestAlternative.transcript.Trim(); speechResult.IsFinal = watsonResult.final; speechResult.Confidence = (float)bestAlternative.confidence; speechResult.RealtimeStart = realtimeStart; speechResult.RealtimeEnd = -1; string[] words = speechResult.Transcript.Split(' '); int wordCount = words.Length; if (wordCount > 0) { speechResult.WordResults = new LexiconSpeechResult.WordResult[wordCount]; for (int i = 0; i < wordCount; i++) { speechResult.WordResults[i] = new LexiconSpeechResult.WordResult(); speechResult.WordResults[i].Word = words[i]; } if (bestAlternative.Timestamps != null) { if (bestAlternative.Timestamps.Length == wordCount) { for (int i = 0; i < wordCount; i++) { if (string.Equals(words[i], bestAlternative.Timestamps[i].Word, StringComparison.OrdinalIgnoreCase)) { speechResult.WordResults[i].TimeStart = (float)bestAlternative.Timestamps[i].Start; speechResult.WordResults[i].TimeEnd = (float)bestAlternative.Timestamps[i].End; speechResult.WordResults[i].RealtimeStart = realtimeStart + speechResult.WordResults[i].TimeStart; speechResult.WordResults[i].RealtimeEnd = realtimeStart + speechResult.WordResults[i].TimeEnd; } else { Debug.LogWarning("word: " + words[i] + " does not match timestamp word: " + bestAlternative.Timestamps[i].Word); } } if (speechResult.WordResults.Length > 0) { speechResult.RealtimeEnd = speechResult.WordResults[speechResult.WordResults.Length - 1].RealtimeEnd; } } else { Debug.LogWarning("word count: " + wordCount + ", timestamp count: " + bestAlternative.Timestamps.Length); } } if (bestAlternative.WordConfidence != null) { if (bestAlternative.WordConfidence.Length == wordCount) { for (int i = 0; i < wordCount; i++) { if (string.Equals(words[i], bestAlternative.WordConfidence[i].Word, StringComparison.OrdinalIgnoreCase)) { speechResult.WordResults[i].Confidence = (float)bestAlternative.WordConfidence[i].Confidence; } else { Debug.LogWarning("word: " + words[i] + " does not match confidence word: " + bestAlternative.WordConfidence[i].Word); } } } else { Debug.LogWarning("word count: " + wordCount + ", confidence count: " + bestAlternative.WordConfidence.Length); } } } if (watsonResult.keywords_result != null && watsonResult.keywords_result.keyword != null && watsonResult.keywords_result.keyword.Length > 0) { speechResult.KeywordResults = new LexiconSpeechResult.KeywordResult[watsonResult.keywords_result.keyword.Length]; for (int i = 0; i < watsonResult.keywords_result.keyword.Length; i++) { KeywordResult watsonKeywordResult = watsonResult.keywords_result.keyword[i]; LexiconSpeechResult.KeywordResult keywordResult = new LexiconSpeechResult.KeywordResult(); keywordResult.Keyword = watsonKeywordResult.keyword; keywordResult.TranscriptText = watsonKeywordResult.normalized_text; keywordResult.Confidence = (float)watsonKeywordResult.confidence; keywordResult.TimeStart = (float)watsonKeywordResult.start_time; keywordResult.TimeEnd = (float)watsonKeywordResult.end_time; keywordResult.RealtimeStart = realtimeStart + keywordResult.TimeStart; keywordResult.RealtimeEnd = realtimeStart + keywordResult.TimeEnd; speechResult.KeywordResults[i] = keywordResult; } } if (watsonResult.word_alternatives != null && watsonResult.word_alternatives.Length > 0) { speechResult.AlternativeWordResults = new LexiconSpeechResult.WordAlternativeResults[watsonResult.word_alternatives.Length]; for (int i = 0; i < watsonResult.word_alternatives.Length; i++) { WordAlternativeResults watsonAlternativeResults = watsonResult.word_alternatives[i]; LexiconSpeechResult.WordAlternativeResults alternativeResults = new LexiconSpeechResult.WordAlternativeResults(); alternativeResults.Alternatives = new LexiconSpeechResult.WordAlternative[watsonAlternativeResults.alternatives.Length]; alternativeResults.TimeStart = (float)watsonAlternativeResults.start_time; alternativeResults.TimeEnd = (float)watsonAlternativeResults.end_time; alternativeResults.RealtimeStart = realtimeStart + alternativeResults.TimeStart; alternativeResults.RealtimeEnd = realtimeStart + alternativeResults.TimeEnd; for (int j = 0; j < watsonAlternativeResults.alternatives.Length; j++) { LexiconSpeechResult.WordAlternative alternative = new LexiconSpeechResult.WordAlternative(); alternative.Word = watsonAlternativeResults.alternatives[j].word; alternative.Confidence = (float)watsonAlternativeResults.alternatives[j].confidence; alternativeResults.Alternatives[j] = alternative; } speechResult.AlternativeWordResults[i] = alternativeResults; } } return(speechResult); }
internal override void Detect(Lexer l) { if (!(l.Char == ' ' || (l.Char >= '0' && l.Char <= '9') || (l.Char >= 'a' && l.Char <= 'z') || (l.Char >= 'A' && l.Char <= 'Z') || l.Char == '\n' || l.Char == '\t' || l.Char == '\r' || l.Char == '{' || l.Char == '_' || l.Char == ',' || l.Char == '(' || l.Char == ')' || l.Char == ':' || l.Char == ';' || l.Char == '*' || l.Char == '<' || l.Char == '>' || l.Char == '[' || l.Char == ']' || l.Char == '-' || l.Char == '.' || l.Char == '"' || l.Char == '&')) { l.ForceExclude(); // contains characters we can't accept. return; } if (l.Text.Contains('=')) { l.ForceExclude(); // contains characters we can't accept. return; } if (!(l.GetParent() is ClassDefinitionToken)) { l.ForceExclude(); // not within a class. return; } KeywordResult res = Keywords.GetKeywords(l.Text); if (res.Keywords.Count > 0 && res.PossibleKeyword == false) { // Now use regular expression checking against the substring starting // at res.DeclIndex to determine whether it's a variable declaration. string decl = l.Text.Substring(res.DeclIndex).Trim(); if (decl.EndsWith(";")) { // Show 'expected {' error. Console.WriteLine("Error: expected '{', not ';' on line " + l.LineNumber + " within file " + l.FileName + ".\nError: Check the end of your function definitions."); l.Abort(); return; } if (!decl.EndsWith("{")) { return; // Skip if we don't have a terminating character. } Regex r = new Regex( "(?<Type>[a-zA-Z][a-zA-z0-9_\\:\\<\\>]+[ \t\r\n\\*]+)?" + "(?<Name>[a-zA-Z][a-zA-z0-9_]*[ \t\r\n]*\\([ \t\r\na-zA-Z0-9_&,-\\.\\:\\*\\<\\>\\[\\]\\\"]*\\))[ \t\r\n]*\\{" ); Match m = r.Match(decl); if (m.Success) { // It's a function declaration. l.TakeOwnership(); l.AddNode(new DirectNode("\n")); l.AddNode(new ClassFunctionDeclarationNode(res.Keywords, m.Groups["Type"].Value.Trim(), m.Groups["Name"].Value.Trim())); l.AddParent(); l.EndOwnership(); } else { // A different kind of declaration that we aren't handling... l.ForceExclude(); } } }
public PeekResult Peek(PeekParameters parameters) { try { if (parameters == null) { throw new ArgumentNullException("parameters"); } if (string.IsNullOrWhiteSpace(parameters.ItemUri)) { throw new ArgumentException(Resources.MissingItemUri); } if (Client.IsExistingObject(parameters.ItemUri)) { var readOptions = new ReadOptions { LoadFlags = LoadFlags.Expanded | LoadFlags.WebDavUrls }; var item = Client.Read(parameters.ItemUri, readOptions); switch (GetItemType(item.Id)) { case ItemType.Category: return(CategoryResult.From((CategoryData)item, Client, CurrentUserId)); case ItemType.Component: return(ComponentResult.From((ComponentData)item, CurrentUserId)); case ItemType.ComponentTemplate: return(ComponentTemplateResult.From((ComponentTemplateData)item, Client, CurrentUserId)); case ItemType.Folder: return(FolderResult.From((FolderData)item, CurrentUserId)); case ItemType.Group: return(GroupResult.From((GroupData)item)); case ItemType.Keyword: return(KeywordResult.From((KeywordData)item, CurrentUserId)); case ItemType.MultimediaType: return(MultimediaTypeResult.From((MultimediaTypeData)item)); case ItemType.Page: return(PageResult.From((PageData)item, Client, CurrentUserId)); case ItemType.PageTemplate: return(PageTemplateResult.From((PageTemplateData)item, Client, CurrentUserId)); case ItemType.Publication: return(PublicationResult.From((PublicationData)item, CurrentUserId)); case ItemType.PublicationTarget: return(PublicationTargetResult.From((PublicationTargetData)item)); case ItemType.Schema: return(SchemaResult.From((SchemaData)item, CurrentUserId)); case ItemType.StructureGroup: return(StructureGroupResult.From((StructureGroupData)item, CurrentUserId)); case ItemType.TargetGroup: return(TargetGroupResult.From((TargetGroupData)item, CurrentUserId)); case ItemType.TargetType: return(TargetTypeResult.From((TargetTypeData)item)); case ItemType.TemplateBuildingBlock: return(TemplateBuildingBlockResult.From((TemplateBuildingBlockData)item, Client, CurrentUserId)); case ItemType.User: return(UserResult.From((UserData)item, Client)); case ItemType.VirtualFolder: return(VirtualFolderResult.From((VirtualFolderData)item, CurrentUserId)); } } return(new EmptyResult()); } catch (Exception ex) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } }
private SpeechRecognitionEvent ParseRecognizeResponse(IDictionary resp) { if (resp == null) { return(null); } try { List <SpeechRecognitionResult> results = new List <SpeechRecognitionResult>(); IList iresults = resp["results"] as IList; if (iresults == null) { return(null); } foreach (var r in iresults) { IDictionary iresult = r as IDictionary; if (iresults == null) { continue; } SpeechRecognitionResult result = new SpeechRecognitionResult(); result.final = (bool)iresult["final"]; IList iwordAlternatives = iresult["word_alternatives"] as IList; if (iwordAlternatives != null) { List <WordAlternativeResults> wordAlternatives = new List <WordAlternativeResults>(); foreach (var w in iwordAlternatives) { IDictionary iwordAlternative = w as IDictionary; if (iwordAlternative == null) { continue; } WordAlternativeResults wordAlternativeResults = new WordAlternativeResults(); if (iwordAlternative.Contains("start_time")) { wordAlternativeResults.start_time = (double)iwordAlternative["start_time"]; } if (iwordAlternative.Contains("end_time")) { wordAlternativeResults.end_time = (double)iwordAlternative["end_time"]; } if (iwordAlternative.Contains("alternatives")) { List <WordAlternativeResult> wordAlternativeResultList = new List <WordAlternativeResult>(); IList iwordAlternativeResult = iwordAlternative["alternatives"] as IList; if (iwordAlternativeResult == null) { continue; } foreach (var a in iwordAlternativeResult) { WordAlternativeResult wordAlternativeResult = new WordAlternativeResult(); IDictionary ialternative = a as IDictionary; if (ialternative.Contains("word")) { wordAlternativeResult.word = (string)ialternative["word"]; } if (ialternative.Contains("confidence")) { wordAlternativeResult.confidence = (double)ialternative["confidence"]; } wordAlternativeResultList.Add(wordAlternativeResult); } wordAlternativeResults.alternatives = wordAlternativeResultList.ToArray(); } wordAlternatives.Add(wordAlternativeResults); } result.word_alternatives = wordAlternatives.ToArray(); } IList ialternatives = iresult["alternatives"] as IList; if (ialternatives != null) { List <SpeechRecognitionAlternative> alternatives = new List <SpeechRecognitionAlternative>(); foreach (var a in ialternatives) { IDictionary ialternative = a as IDictionary; if (ialternative == null) { continue; } SpeechRecognitionAlternative alternative = new SpeechRecognitionAlternative(); alternative.transcript = (string)ialternative["transcript"]; if (ialternative.Contains("confidence")) { alternative.confidence = (double)ialternative["confidence"]; } if (ialternative.Contains("timestamps")) { IList itimestamps = ialternative["timestamps"] as IList; TimeStamp[] timestamps = new TimeStamp[itimestamps.Count]; for (int i = 0; i < itimestamps.Count; ++i) { IList itimestamp = itimestamps[i] as IList; if (itimestamp == null) { continue; } TimeStamp ts = new TimeStamp(); ts.Word = (string)itimestamp[0]; ts.Start = (double)itimestamp[1]; ts.End = (double)itimestamp[2]; timestamps[i] = ts; } alternative.Timestamps = timestamps; } if (ialternative.Contains("word_confidence")) { IList iconfidence = ialternative["word_confidence"] as IList; WordConfidence[] confidence = new WordConfidence[iconfidence.Count]; for (int i = 0; i < iconfidence.Count; ++i) { IList iwordconf = iconfidence[i] as IList; if (iwordconf == null) { continue; } WordConfidence wc = new WordConfidence(); wc.Word = (string)iwordconf[0]; wc.Confidence = (double)iwordconf[1]; confidence[i] = wc; } alternative.WordConfidence = confidence; } alternatives.Add(alternative); } result.alternatives = alternatives.ToArray(); } IDictionary iKeywords = iresult["keywords_result"] as IDictionary; if (iKeywords != null) { result.keywords_result = new KeywordResults(); List <KeywordResult> keywordResults = new List <KeywordResult>(); foreach (string keyword in Keywords) { if (iKeywords[keyword] != null) { IList iKeywordList = iKeywords[keyword] as IList; if (iKeywordList == null) { continue; } foreach (var k in iKeywordList) { IDictionary iKeywordDictionary = k as IDictionary; KeywordResult keywordResult = new KeywordResult(); keywordResult.keyword = keyword; keywordResult.confidence = (double)iKeywordDictionary["confidence"]; keywordResult.end_time = (double)iKeywordDictionary["end_time"]; keywordResult.start_time = (double)iKeywordDictionary["start_time"]; keywordResult.normalized_text = (string)iKeywordDictionary["normalized_text"]; keywordResults.Add(keywordResult); } } } result.keywords_result.keyword = keywordResults.ToArray(); } results.Add(result); } return(new SpeechRecognitionEvent(results.ToArray())); } catch (Exception e) { Log.Error("SpeechToText.ParseRecognizeResponse()", "ParseJsonResponse exception: {0}", e.ToString()); return(null); } }