Beispiel #1
0
        public SpeechEntity(SpeechEntity entity1, SpeechEntity entity2)
        {
            Left = entity1;
            Right = entity2;

            entity = "";// entity1.ToString() + " + " + entity2.ToString();
        }
Beispiel #2
0
        static void DrawNode(Graphics g, SpeechEntity node, double x, double y)
        {
            Pen blackPen = new Pen(Color.Black, 3);
            Font drawFont = new Font("Arial", 16);
            SolidBrush drawBrush = new SolidBrush(Color.Black);

            SizeF size = g.MeasureString(node.Entity, drawFont);

            float left = (float)x + (node.Width - 100) / 2;
            float top = (float)y;

               // g.DrawEllipse(blackPen, left, top, (float)100, (float)32);
            g.DrawString(node.Entity, drawFont, drawBrush, left + 50 - size.Width / 2, top + 8);

            if (node.Left != null)
            {
                float leafRight = (float)x + (node.Left.Width - 100) / 2 + 80;

                g.DrawLine(blackPen, left + 20, top + 36, leafRight, top + 80);

                DrawNode(g, node.Left, x, y + 80);
            }
            if (node.Right != null)
            {
                float leafLeft = (float)x + node.Width - node.Right.Width + (node.Right.Width - 100) / 2 + 20;

                g.DrawLine(blackPen, left + 80, top + 36, leafLeft, top + 80);

                DrawNode(g, node.Right, x + node.Width - node.Right.Width, y + 80);
            }
        }
Beispiel #3
0
        static void Draw(SpeechEntity root)
        {
            Bitmap myBitmap = new Bitmap(CalculateWidth(root) + 20, 600);

            Graphics myGraphics = Graphics.FromImage(myBitmap);

            DrawNode(myGraphics, root, 0, 0);

            myBitmap.Save("test.bmp");

            myGraphics.Dispose();
        }
Beispiel #4
0
        public SpeechEntity(SpeechEntity entity1, SpeechEntity entity2, double[] arr)
        {
            Left = entity1;
            Right = entity2;

            entity = "";

            // 0 is the bias
            for (int i = 1; i < 11; ++i)
            {
                if (arr[i] > 0.9)
                {
                    Add((SpeechClass)(i - 1));
                    entity += (SpeechClass)(i - 1) + " ";
                }
            }

            //entity = ".";// entity1.ToString() + " + " + entity2.ToString();
        }
Beispiel #5
0
        static int CalculateWidth(SpeechEntity node)
        {
            Font drawFont = new Font("Arial", 16);
            using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
            {
                node.Width = (int)g.MeasureString(node.Entity, drawFont).Width;
            }

            if (node.Left != null)
            {
                CalculateWidth(node.Left);
                node.Width += node.Left.Width;
            }
            if (node.Right != null)
            {
                CalculateWidth(node.Right);
                node.Width += node.Right.Width;
            }

            return node.Width;
        }
Beispiel #6
0
        public static double[] Group(SpeechEntity firstEntity, SpeechEntity secondEntity)
        {
            double[] arr = new double[20];

            for (int i = 0; i < 20; ++i)
            {
                arr[i] = -1.0;
            }

            if(firstEntity.speechClasses != null)
                foreach (var e in firstEntity.speechClasses)
                {
                    arr[(int)e] = 1.0;
                }

            if (secondEntity.speechClasses != null)
                foreach (var e in secondEntity.speechClasses)
                {
                    arr[(int)e + 10] = 1.0;
                }

            return arr;
        }
Beispiel #7
0
        public static SpeechEntity Merge(SpeechEntity firstEntity, SpeechEntity secondEntity)
        {
            SpeechEntity ent = new SpeechEntity(firstEntity, secondEntity);

            foreach (var e in firstEntity.speechClasses)
            {
                ent.Add(e);
            }

            foreach (var e in secondEntity.speechClasses)
            {
                ent.Add(e);
            }

            return ent;
        }
Beispiel #8
0
 static void AddSample(WordMatrix mat, string word1, string word2, SpeechEntity result, double expected)
 {
     ex.Add(SpeechEntity.Group(mat[word1], mat[word2]));
     res.Add(result.ToArray().Concat(new double[] { expected }).ToArray());
 }
        public SpeechEntity Parse(string sentence)
        {
            string[] words = sentence.Split(" ".ToCharArray());

            SpeechEntity[] ents = new SpeechEntity[words.Length];

            for (int i = 0; i < words.Length; ++i)
            {
                ents[i] = _matrix[words[i]];
            }

            return Parse(ents);
        }
        SpeechEntity Parse(SpeechEntity[] entities)
        {
            double bestScore = -1.0;
            int idx = -1;

            double[] result = null;

            for (int i = 0; i < entities.Length - 1; ++i)
            {
                var resu = _network.Evaluate(SpeechEntity.Group(entities[i], entities[i + 1]));
                if (resu[11] > bestScore)
                {
                    bestScore = resu[11];
                    idx = i;

                    result = (double[])resu.Clone();
                }
            }

            var resEntity = new SpeechEntity(entities[idx], entities[idx + 1], result);

            if (entities.Length > 2)
            {
                SpeechEntity[] ents = new SpeechEntity[entities.Length - 1];
                for (int i = 0, j = 0; i < entities.Length; ++i, ++j)
                {
                    if (i == idx)
                    {
                        ents[j] = resEntity;
                        i += 1;
                        continue;
                    }
                    else
                    {
                        ents[j] = entities[i];
                    }
                }

                return Parse(ents);
            }

            resEntity.Entity = "_ROOT_";
            return resEntity;
        }