public void SetupNetwork()
 {
     _vectors = Substitute.For<WordVectors>();
     _sentences = Substitute.For<Sentences>();
     SentenceLearner.NeuralNetworkFactory = _neuralNetworkFactory = new NeuralNetworkFactory();
     _contextMaps = new ContextMaps();
     _target = new SentenceLearner(_vectors, _sentences,_contextMaps);
 }
Beispiel #2
0
 public void LoadAndLearn()
 {
     var wvl =new WordVectorLoader("wikipedia_vectors.txt");
     var sw = Stopwatch.StartNew();
     _wordVectors = wvl.LoadVectors();
     sw.Stop();
     Debug.WriteLine("Loaded word vectors in {0} ms", sw.ElapsedMilliseconds);
     var s = new StoryReader("WikiJunior_Biology.txt");
     var sentences = s.ReadStory();
     _contextMaps = new ContextMaps();
     _sentenceLearner = new SentenceLearner(_wordVectors, sentences, _contextMaps);
     var plan = _sentenceLearner.PreparePlan(12);
     _sentenceLearner.ExecutePlan(plan, MorphoSyntacticContext.InitialState());
 }
Beispiel #3
0
        public ContextBase()
        {
            ContextMap contextMap = null;

            if (!ContextMaps.ContainsKey(this.GetType()))
            {
                contextMap = SetupMaps(this);
            }
            else
            {
                contextMap = ContextMaps[this.GetType()];
            }

            ContextMap = contextMap;
            SetupDbSets();

            ObjectTracker      = new ObjectTracker(this);
            ObjectMaterializer = new Tracking.ObjectMaterializer(ObjectTracker);
            contextMap.InitializeContext(this);
        }
        public void TestTrain()
        {
            var stream = new StringReader(@"Hey! Look here!");
            var storyReader = new StoryReader(stream);
            var sentences = storyReader.ReadStory();
            var wordVectors = new WordVectors(10);
            wordVectors.TryAdd("Hey", new WordVector { Name = "Hey" });
            wordVectors.TryAdd("Look", new WordVector { Name = "Look" });
            wordVectors.TryAdd("here", new WordVector { Name = "here" });
            wordVectors.TryAdd("Test", new WordVector { Name = "Test" });
            wordVectors.TryAdd("!", new WordVector { Name = "!" });

            var contextMaps = new ContextMaps();
            var target = new SentenceLearner(wordVectors, sentences, contextMaps);

            var trainingPlan = target.PreparePlan(4);
            //should train Hey,ctx0,1; Look,ctx0,0; here,ctx0,-1, here,ctx1,1
            trainingPlan.ContainsPlanStep(new WrittenWord("Hey"), TrainingOutput.Complete).Should().BeTrue();
            trainingPlan.ContainsPlanStep(new WrittenWord("Look"), TrainingOutput.Incomplete).Should().BeTrue();
            trainingPlan.ContainsPlanStep(new WrittenWord("Test"), TrainingOutput.Incomplete).Should().BeFalse();
        }
        public void TestTrain2()
        {
            var stream = new StringReader(@"Hey! Look here!");
            var storyReader = new StoryReader(stream);
            var sentences = storyReader.ReadStory();
            var wordVectors = new WordVectors(10);
            var contextMaps = new ContextMaps();
            wordVectors.TryAdd("Hey", new WordVector { Name = "Hey" });
            wordVectors.TryAdd("Look", new WordVector { Name = "Look" });
            wordVectors.TryAdd("here", new WordVector { Name = "here" });
            wordVectors.TryAdd("Test", new WordVector { Name = "Test" });
            wordVectors.TryAdd("!", new WordVector { Name = "!" });

            var target = new SentenceLearner(wordVectors, sentences, contextMaps);

            var plan = target.PreparePlan(4);

            target.ExecutePlan(plan, MorphoSyntacticContext.InitialState());

            var weights = target.GetWeightsForWord(new WrittenWord("Hey"));
        }
Beispiel #6
0
        protected static ContextMap SetupMaps(ContextBase thisItem)
        {
            if (ContextMaps == null)
            {
                ContextMaps = new ConcurrentDictionary <Type, ContextMap>();
            }

            ContextMap contextMap = new ContextMap();

            foreach (var prop in thisItem.GetType().GetProperties())
            {
                if (prop.PropertyType.Name == "KVSDbSet`1" || prop.PropertyType.Name == "ICollection`1")
                {
                    Type entityType = prop.PropertyType.GetGenericArguments().Last();

                    var entityProps = entityType.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
                    var keyProp     = entityProps.SingleOrDefault(q => q.Name == "Key" || q.Name == "Id");

                    var entityMap = new EntityMap(entityType, prop.GetGetMethod(), prop.GetSetMethod(), prop.PropertyType.GetConstructor(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic, null, new Type[] { typeof(EntityMap), typeof(ContextBase) }, new System.Reflection.ParameterModifier[] { }), keyProp.Name, keyProp.GetGetMethod());
                    contextMap.EntityMaps.Add(entityMap);
                }
            }

            foreach (var entityMap in contextMap.EntityMaps)
            {
                foreach (var prop in entityMap.EntityType.GetProperties())
                {
                    if (prop.PropertyType.Name == "KVSCollection`1" || prop.PropertyType.Name == "ICollection`1")
                    {
                        Type targetEntityType = prop.PropertyType.GetGenericArguments().Last();
                        var  targetEntityMap  = contextMap.EntityMaps.SingleOrDefault(q => q.EntityType == targetEntityType);

                        if (targetEntityMap != null)
                        {
                            var relationshipMap = new RelationshipMap()
                            {
                                LocalObjectMap  = entityMap,
                                TargetObjectMap = targetEntityMap,
                                PropertyName    = prop.Name,
                                IsManyToTarget  = true
                            };

                            contextMap.RelationshipMaps.Add(relationshipMap);
                            entityMap.RelationshipMaps.Add(relationshipMap);
                        }
                        //many relationship
                    }
                    else if (contextMap.EntityMaps.Any(q => q.EntityType == prop.PropertyType))
                    {
                        //foreign key ref (single relationship)

                        Type targetEntityType = prop.PropertyType;
                        var  targetEntityMap  = contextMap.EntityMaps.SingleOrDefault(q => q.EntityType == targetEntityType);

                        if (targetEntityMap != null)
                        {
                            var relationshipMap = new RelationshipMap()
                            {
                                LocalObjectMap = entityMap, TargetObjectMap = targetEntityMap, PropertyName = prop.Name
                            };
                            contextMap.RelationshipMaps.Add(relationshipMap);
                            entityMap.RelationshipMaps.Add(relationshipMap);
                        }
                    }
                }

                //find relationships
            }

            ContextMaps.TryAdd(thisItem.GetType(), contextMap);

            return(contextMap);
        }