protected override void Execute(CodeActivityContext context)
        {
            string text   = Text.Get(context);
            string apiKey = APIKey.Get(context);
            string seKey  = SecretKey.Get(context);

            try
            {
                var client = new Baidu.Aip.Nlp.Nlp(apiKey, seKey);
                //修改超时时间
                client.Timeout = 60000;
                //设置可选参数
                var options = new Dictionary <string, object>
                {
                    { "mode", Mode }
                };
                //带参数调用依存句法分析
                string result = client.DepParser(text, options).ToString();
                Result.Set(context, result);
            }
            catch (Exception e)
            {
                SharedObject.Instance.Output(SharedObject.enOutputType.Error, Localize.LocalizedResources.GetString("msgErrorOccurred"), e.Message);
            }
        }
Ejemplo n.º 2
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入内容");
                return;
            }

            var mode = comboBox1.Text ==
                       "web模型" ? new Dictionary <string, object> {
                { "mode", 1 }
            } : new Dictionary <string, object> {
                { "mode", 0 }
            };
            JObject depResult = client.DepParser(textBox1.Text, mode);

            JToken[]        itemArr      = depResult.GetValue("items").ToArray();
            int             head         = 0;
            List <TreeNode> treeNodeList = new List <TreeNode>();
            List <int>      idList       = new List <int>();
            int             index        = 0;

            foreach (JToken item in itemArr)
            {
                int headId = item.Value <int>("head");
                if (head == headId)
                {
                    int id = item.Value <int>("id");
                    idList.Add(id);
                    string word    = item.Value <string>("word");
                    string deprel  = item.Value <string>("deprel");
                    string content = word + "(" + DEPRELTable[deprel] + ")";
                    treeNodeList.Add(new TreeNode(content));
                }
            }
            while (index < treeNodeList.Count)
            {
                TreeNode pTreeNode = treeNodeList.ElementAt(index);
                head = idList.ElementAt(index++);
                foreach (JToken item in itemArr)
                {
                    int headId = item.Value <int>("head");
                    if (head == headId)
                    {
                        int id = item.Value <int>("id");
                        idList.Add(id);
                        string   word     = item.Value <string>("word");
                        string   deprel   = item.Value <string>("deprel");
                        string   content  = word + "(" + DEPRELTable[deprel] + ")";
                        TreeNode treeNode = new TreeNode(content);
                        pTreeNode.Nodes.Add(treeNode);
                        treeNodeList.Add(treeNode);
                    }
                }
            }
            treeView2.Nodes.Add(treeNodeList.ElementAt(0));
            treeView2.ExpandAll();
        }