Esempio n. 1
0
        private static string TextFromConstituencyTree(ConstituencyTreeNode node, string tree)
        {
            ConstituencyTreeNode previous = null;

            foreach (ConstituencyTreeNode n in node.Children)
            {
                if (previous != n.previousNode)
                {
                    throw new FormatException();
                }
                if (previous != null && previous.nextNode != n)
                {
                    throw new FormatException();
                }
                previous = n;
                if (n.word == null)
                {
                    tree += " (" + n.PoS;
                    tree  = TextFromConstituencyTree(n, tree);
                    tree += ")";
                }
                else
                {
                    tree += " (" + n.PoS + " " + n.word + ")";
                }
            }
            return(tree);
        }
Esempio n. 2
0
 private static void FormatConstituencyTree(ConstituencyTreeNode node, int level, int col, string[] printLines, int debugLevel)
 {
     if (level != node.level)
     {
         throw new FormatException();
     }
     if (printLines[level] == null)
     {
         printLines[level] = "|";
     }
     if (col - printLines[level].Length - 1 > 0)
     {
         printLines[level] += string.Empty.PadRight(col - printLines[level].Length - 1, ' ') + "|";
     }
     foreach (ConstituencyTreeNode n in node.Children)
     {
         string debugInfo = debugLevel < 4 ? string.Empty : "(" + n.parentNode.PoS + " " + n.level + "/" + n.maxDepth + ")";
         if (n.word == null)
         {
             int previousLen = printLines[level].Length;
             FormatConstituencyTree(n, level + 1, previousLen, printLines, debugLevel);
             int len   = printLines[level + 1].Length - previousLen - n.PoS.Length - 1;
             int left  = len / 2;
             int right = len - left;
             printLines[level] += string.Empty.PadRight(left, '-') + n.PoS + debugInfo + string.Empty.PadRight(right, '-') + "|";
         }
         else
         {
             printLines[level] += " " + n.PoS + debugInfo + ":" + n.word + " |";
         }
     }
 }
Esempio n. 3
0
        private static string ConstituencyTreeFromText(string parse, ConstituencyTreeNode node, ConstituencyTreeNode root)
        {
            ConstituencyTreeNode previous = null;

            while (!string.IsNullOrWhiteSpace(parse) && parse[0] == '(')
            {
                int    lastIndex = parse.IndexOfAny(" ()".ToCharArray(), 1);
                char   lastChar  = parse[lastIndex];
                string gu        = parse.Substring(1, lastIndex - 1);
                parse = parse.Substring(lastIndex).TrimStart();
                ConstituencyTreeNode child;
                switch (lastChar)
                {
                case ' ':
                    lastIndex = parse.IndexOfAny("()".ToCharArray());
                    string wo = parse.Substring(0, lastIndex);
                    parse       = parse.Substring(lastIndex).TrimStart();
                    child       = node.AddChild(new ConstituencyTreeNode(gu, wo));
                    child.level = node.level + 1;
                    if (root.nextWord == null)
                    {
                        root.nextWord = child;
                    }
                    else
                    {
                        root.previousWord.nextWord = child;
                    }
                    child.previousWord = root.previousWord;
                    root.previousWord  = child;
                    break;

                case '(':
                    child       = node.AddChild(new ConstituencyTreeNode(gu));
                    child.level = node.level + 1;
                    parse       = ConstituencyTreeFromText(parse, child, root);
                    break;

                default:
                    throw new FormatException();
                }
                if (parse[0] != ')')
                {
                    throw new FormatException();
                }
                parse              = parse.Substring(1).TrimStart();
                child.parentNode   = node;
                child.previousNode = previous;
                if (previous != null)
                {
                    previous.nextNode = child;
                }
                previous = child;
                if (child.maxDepth + 1 > node.maxDepth)
                {
                    node.maxDepth = child.maxDepth + 1;
                }
            }
            return(parse);
        }
Esempio n. 4
0
        public static ConstituencyTreeNode ConstituencyTreeFromText(string parse)
        {
            parse = parse.Replace(" (", "(");
            ConstituencyTreeNode root = new ConstituencyTreeNode("root");

            parse = ConstituencyTreeFromText(parse, root, root);
            return(root);
        }
Esempio n. 5
0
 private static string WordsFromConstituencyTree(ConstituencyTreeNode node, string words)
 {
     node = node.nextWord;
     if (node != null)
     {
         words += node.word + " ";
         words  = WordsFromConstituencyTree(node, words);
     }
     return(words);
 }
        public override async System.Threading.Tasks.Task <CallServiceResponse <IGenericServiceResponse> > CallServiceAsync(string text, System.Collections.Generic.Dictionary <string, string> apiArgs)
        {
            Log.WriteLine("MicrosoftCognitiveParseServices: Text:" + text);
            if (MicrosoftCognitiveParseAnalyzersServices.ParseAnalysers == null) // calling ParseAnalyzersService. It's a dependency of ParseService.
            {
                CallServiceResponse <IGenericServiceResponse> responseAnalyzers    = new CallServiceResponse <IGenericServiceResponse>(service);
                MicrosoftCognitiveParseAnalyzersServices      parseAnalyersService = new MicrosoftCognitiveParseAnalyzersServices(Options.services["MicrosoftCognitiveParseAnalyzersService"].service);
                responseAnalyzers = await parseAnalyersService.CallServiceAsync(text, apiArgs);

                if (MicrosoftCognitiveParseAnalyzersServices.ParseAnalysers == null || MicrosoftCognitiveParseAnalyzersServices.ParseAnalysers.Length == 0 || string.IsNullOrWhiteSpace(MicrosoftCognitiveParseAnalyzersServices.ParseAnalyzerStringized))
                {
                    throw new InvalidOperationException(); // can't continue without at least one Analyzer
                }
            }
            CallServiceResponse <IGenericServiceResponse> response = new CallServiceResponse <IGenericServiceResponse>(service);

            response.Request = Array.Find(service.requests, p => p.argType == "text");
            System.Collections.Generic.List <Tuple <string, string> > jsonSubstitutes = new System.Collections.Generic.List <Tuple <string, string> >()
            {
                new Tuple <string, string>("{AnalyzerStringized}", MicrosoftCognitiveParseAnalyzersServices.ParseAnalyzerStringized),
            };
            await HttpMethods.CallApiAsync(response, null, null, jsonSubstitutes, text, apiArgs); // example of using jsonSubstitutes

            await HttpMethods.ExtractResultAsync(response);

            Newtonsoft.Json.Linq.JArray arrayOfResults = Newtonsoft.Json.Linq.JArray.Parse(response.ResponseResult);
            foreach (Newtonsoft.Json.Linq.JToken s in arrayOfResults)
            {
                ConstituencyTreeNode root = ParseHelpers.ConstituencyTreeFromText(s.ToString());
                string textTree           = ParseHelpers.TextFromConstituencyTree(root);
                if (textTree != s.ToString())
                {
                    throw new FormatException();
                }
                string   words      = ParseHelpers.WordsFromConstituencyTree(root);
                string[] printLines = ParseHelpers.FormatConstituencyTree(root);
                foreach (string p in printLines)
                {
                    Console.WriteLine(p);
                }
            }
            return(response);
        }
Esempio n. 7
0
 public static string WordsFromConstituencyTree(ConstituencyTreeNode node)
 {
     return(WordsFromConstituencyTree(node, ""));
 }
Esempio n. 8
0
 public static string TextFromConstituencyTree(ConstituencyTreeNode node)
 {
     return(TextFromConstituencyTree(node, "").Trim());
 }
Esempio n. 9
0
 public ConstituencyTreeNode AddChild(ConstituencyTreeNode node)
 {
     Children.Add(node);
     return(node);
 }
Esempio n. 10
0
 public static string[] FormatConstituencyTree(ConstituencyTreeNode node)
 {
     string[] printLines = new string[node.maxDepth];
     FormatConstituencyTree(node, 0, 0, printLines, 0);
     return(printLines);
 }