예제 #1
0
파일: Program.cs 프로젝트: ilexp/bmj2017-03
        public static void Main2(string[] args)
        {
            Thread.CurrentThread.CurrentCulture   = CultureInfo.InvariantCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

            using (VectorDataStore vectorData = new VectorDataStore())
            {
                Console.WriteLine("Opening word vector database...");
                vectorData.Open(
                    "..\\..\\..\\WordVectors\\wiki.en.vec.bin",
                    "..\\..\\..\\WordVectors\\wiki.en.vec.idx");

                Console.WriteLine("  {0} vectors available", vectorData.VectorCount);
                Console.WriteLine("  {0} dimensions per vector", vectorData.VectorDimensions);

                Console.WriteLine();
                Console.WriteLine("Enter two words to check semantic similarity");
                Console.WriteLine();
                while (true)
                {
                    Console.Write("First: ");
                    string first = Console.ReadLine();
                    Console.Write("Second: ");
                    string second = Console.ReadLine();
                    Console.WriteLine("Similarity: {0:F}", vectorData.GetSimilarity(first, second));
                    Console.WriteLine();
                }
            }
        }
예제 #2
0
        public Message(DialogContext requiredContext, string text, VectorDataStore vectorData)
        {
            this.context = requiredContext;
            this.rawText = text;

            // Trim and add ending punctuation of not present
            text = text.Trim();
            if (!char.IsPunctuation(text[text.Length - 1]))
            {
                text += ".";
            }

            // Make sure punctuation counts as individual words
            text = this.WordifyPunctuation(text);

            // Split the input text into words
            this.words = RegexWordBounds.Split(text)
                         .Where(token => !string.IsNullOrWhiteSpace(token))
                         .ToArray();

            // Retrieve a vector for each word
            this.vectors = new LargeVector[this.words.Length];
            for (int i = 0; i < this.words.Length; i++)
            {
                this.vectors[i] = vectorData.Get(this.words[i].ToLower());
            }
        }
예제 #3
0
        private static DialogNode ParseNode(JObject jsonNode, VectorDataStore vectorData, Dictionary <string, DialogContext> contextMap)
        {
            DialogContext context    = ParseContext(jsonNode.Value <string>("context"), contextMap);
            DialogContext inContext  = ParseContext(jsonNode.Value <string>("inContext"), contextMap);
            DialogContext outContext = ParseContext(jsonNode.Value <string>("outContext"), contextMap);

            Statement input = new Statement(inContext ?? context);

            foreach (JObject jsonMessage in jsonNode.Value <JArray>("input"))
            {
                Message message = ParseMessage(jsonMessage, vectorData, contextMap);
                input.Add(message);
            }

            Statement output = new Statement(outContext ?? context);

            foreach (JObject jsonMessage in jsonNode.Value <JArray>("output"))
            {
                Message message = ParseMessage(jsonMessage, vectorData, contextMap);
                output.Add(message);
            }

            DialogNode node = new DialogNode(input, output);

            return(node);
        }
예제 #4
0
        private static Message ParseMessage(JObject jsonMessage, VectorDataStore vectorData, Dictionary <string, DialogContext> contextMap)
        {
            string        contextId = jsonMessage.Value <string>("context");
            string        text      = jsonMessage.Value <string>("text");
            DialogContext context   = ParseContext(contextId, contextMap);

            return(new Message(context, text, vectorData));
        }
예제 #5
0
파일: Program.cs 프로젝트: ilexp/bmj2017-03
        public static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture   = CultureInfo.InvariantCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

            AsyncConsole.UserPrefix      = "You: ";
            AsyncConsole.UserPrefixColor = ConsoleColor.Red;

            using (VectorDataStore vectorData = new VectorDataStore())
            {
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.WriteLine("Opening word vector database...");
                vectorData.Open(
                    "..\\..\\..\\WordVectors\\wiki.en.vec.bin",
                    "..\\..\\..\\WordVectors\\wiki.en.vec.idx");

                Console.WriteLine("  {0} vectors available", vectorData.VectorCount);
                Console.WriteLine("  {0} dimensions per vector", vectorData.VectorDimensions);
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("This is an experiment for a dialog system. Imagine you are");
                Console.WriteLine("actually playing some open world RPG game. Right now, you are");
                Console.WriteLine("standing in front of the castle gate and need to get in to");
                Console.WriteLine("fulfill some epic quest. However, castle guard Bob is in your way.");
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Talk your way in.");
                Console.ResetColor();
                Console.WriteLine();

                DialogTree sampleDialog = CreateSampleTree(vectorData);
                Eliza      eliza        = new Eliza(vectorData, sampleDialog);

                while (true)
                {
                    // Receive user input
                    bool   userTyping = AsyncConsole.IsReceivingLine;
                    string userInput  = AsyncConsole.ReadLine();
                    if (!string.IsNullOrEmpty(userInput))
                    {
                        // Quit program
                        if (string.Equals(userInput, "bye", StringComparison.InvariantCultureIgnoreCase))
                        {
                            break;
                        }

                        // Say something: hand it over to Eliza
                        eliza.Listen(userInput);
                    }

                    // Let Eliza think
                    eliza.Update(userTyping);

                    // Don't run at CPU max speed
                    Thread.Sleep(1);
                }
            }
        }
예제 #6
0
        public static DialogTree Load(string filePath, VectorDataStore vectorData)
        {
            DialogTree tree = new DialogPrototype.DialogTree();
            Dictionary <string, DialogContext> contextMap = new Dictionary <string, DialogContext>();

            using (Stream stream = File.OpenRead(filePath))
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, true))
                    using (JsonTextReader jsonReader = new JsonTextReader(reader))
                    {
                        JObject jsonRoot = JObject.Load(jsonReader);

                        JObject jsonDontGetItNode = jsonRoot.Value <JObject>("dontGetItNode");
                        tree.dontGetItNode = ParseNode(jsonDontGetItNode, vectorData, contextMap);

                        JArray jsonNodes = jsonRoot.Value <JArray>("nodes");
                        foreach (JObject jsonNode in jsonNodes)
                        {
                            DialogNode node = ParseNode(jsonNode, vectorData, contextMap);
                            tree.Add(node);
                        }
                    }

            return(tree);
        }
예제 #7
0
 public Message(string text, VectorDataStore vectorData) : this(null, text, vectorData)
 {
 }
예제 #8
0
파일: Program.cs 프로젝트: ilexp/bmj2017-03
 private static DialogTree CreateSampleTree(VectorDataStore vectorData)
 {
     return(DialogTree.Load("SampleDialog.json", vectorData));
 }
예제 #9
0
파일: Eliza.cs 프로젝트: ilexp/bmj2017-03
 public Eliza(VectorDataStore vectorData, DialogTree dialogTree)
 {
     this.vectorData = vectorData;
     this.dialogTree = dialogTree;
     this.watch.Start();
 }