Ejemplo n.º 1
0
        public static void GradeLatestMemoryString(int gradeValue, out float prevGradeValue, out float currentGradeValue, string applicationPrefix)
        {
            string latestPath = Path.Combine(Path.GetTempPath(), applicationPrefix + latestFilename);

            using (TextReader textReader = new StreamReader(latestPath))
            {
                string line = textReader.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    throw new MemoryException("Unable to read infomation about latest memory string displayed");
                }
                string[] brokenLine = line.Split(' ');
                if (brokenLine.Length != 2)
                {
                    throw new MemoryException("Unable to read infomation about latest memory string displayed");
                }
                string memoryFilename       = brokenLine[0];
                int    numberOfMemoryString = 0;
                if (!int.TryParse(brokenLine[1], out numberOfMemoryString))
                {
                    throw new MemoryException("Unable to read infomation about latest memory string displayed");
                }
                float newAvgGrade = MemoryFile.GradeMemoryString(memoryFilename, numberOfMemoryString, gradeValue, out prevGradeValue, out currentGradeValue);
                ConfigurationFile.SaveNewAvgGrade(memoryFilename, newAvgGrade);
            }
        }
Ejemplo n.º 2
0
        public static string GetMemoryString(string applicationPrefix)
        {
            //Access configuration file
            ConfigurationFile confFile = ConfigurationFile.Open();

            //Pick memoryFile from which we will pick memory string to learn
            Engine memoryEngine       = new Engine();
            string selectedMemoryFile = memoryEngine.PickMemoryFile(confFile.MemoryFiles);

            //Read all strings and grades for selected file
            MemoryFile memoryFile = MemoryFile.Open(selectedMemoryFile);

            //Calculate memory string to learn
            string selectedMemoryString = memoryEngine.PickMemoryString(memoryFile);

            //Write down this memory string, so it can be graded
            int selectedMemoryStringNumber = MemoryFile.GetNumberOutOfMemoryString(selectedMemoryString);

            ConfigurationFile.WriteDownLatestMemoryString(selectedMemoryFile, selectedMemoryStringNumber, applicationPrefix);

            //Display selected string with selected color in conf file
            //Display display = new Display(confFile.SelectedColor);

            return(MemoryFile.GetMemoryStringWithoutNumber(selectedMemoryString));
        }
Ejemplo n.º 3
0
        //public static global::Memory.MemoryFile Open(string selectedMemoryFile)
        public static MemoryFile Open(string selectedMemoryFile)
        {
            MemoryFile memoryFile = new MemoryFile();

            memoryFile.ReadMemoryFile(selectedMemoryFile);
            memoryFile.ReadMemoryFileGrades(Path.GetFileNameWithoutExtension(selectedMemoryFile) + ".grades");

            return(memoryFile);
        }
Ejemplo n.º 4
0
        public static float CalcAvgMemoryFileGrade(string path)
        {
            float avgGrade;

            try
            {
                avgGrade = MemoryFile.CalcAvgGradeFromMemoryFile(path);
            }
            catch (FileNotFoundException) { avgGrade = GenerateMemoryFileGrades(path); }
            catch (MemoryException e) { Console.WriteLine(e.Message); avgGrade = GenerateMemoryFileGrades(path); }

            return(avgGrade);
        }
Ejemplo n.º 5
0
        public string PickMemoryString(MemoryFile memoryFile)
        {
            string selectedMemoryString = string.Empty;

            //Multiplay by 10 to move comma - increase learning process acuracy
            int highestGrade = 0;

            for (int i = 0; i < memoryFile.MemoryStringsGrades.Count; i++)
            {
                memoryFile.MemoryStringsGrades[i] *= 10;
                if (memoryFile.MemoryStringsGrades[i] > highestGrade)
                {
                    highestGrade = (int)Math.Ceiling(memoryFile.MemoryStringsGrades[i]);
                }
            }
            highestGrade++;

            //Revert grades and calc total sum
            int totalSum = 0;

            for (int i = 0; i < memoryFile.MemoryStringsGrades.Count; i++)
            {
                int revertedGrade = (int)(highestGrade - memoryFile.MemoryStringsGrades[i]);
                memoryFile.MemoryStringsGrades[i] = revertedGrade;
                totalSum += revertedGrade;
            }

            int currentSum = 0;
            int randValue  = rand.Next(1, totalSum + 1);

            for (int i = 0; i < memoryFile.MemoryStringsGrades.Count; i++)
            {
                currentSum += (int)memoryFile.MemoryStringsGrades[i];

                if (currentSum >= randValue)
                {
                    selectedMemoryString = memoryFile.MemoryStrings[i];
                    break;
                }
            }

            if (string.Empty == selectedMemoryString)
            {
                throw new MemoryException("Engine selection error");
            }

            return(selectedMemoryString);
        }
Ejemplo n.º 6
0
        private static float GenerateMemoryFileGrades(string path)
        {
            int totalMemoryStrings = MemoryFile.CountMemoryStrings(path);

            string bareFilename   = Path.GetFileNameWithoutExtension(Path.GetFileName(path));
            string gradesfilepath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), bareFilename + ".grades");

            using (TextWriter textWriter = new StreamWriter(gradesfilepath))
            {
                for (int i = 1; i <= totalMemoryStrings; i++)
                {
                    textWriter.WriteLine(String.Format("{0} {1}", i, 0.0f));
                }

                //Close handle to MemoryString Grades file
                textWriter.Close();
            }

            return(0.0f);
        }
Ejemplo n.º 7
0
        public static float GradeMemoryString(string memoryFilename, int memoryFileToGrade, int gradeValue, out float prevGradeValue, out float currentGradeValue)
        {
            float  newAvgGrade  = 0.0f;
            string bareFilename = Path.GetFileNameWithoutExtension(Path.GetFileName(memoryFilename));

            MemoryFile memoryFile = new MemoryFile();

            memoryFile.ReadMemoryFileGrades(bareFilename + ".grades");
            prevGradeValue = memoryFile.MemoryStringsGrades[memoryFileToGrade - 1];
            if (0 == prevGradeValue)
            {
                memoryFile.MemoryStringsGrades[memoryFileToGrade - 1] = gradeValue;
            }
            else
            {
                memoryFile.MemoryStringsGrades[memoryFileToGrade - 1] = (gradeValue + prevGradeValue) / 2.0f;
            }
            currentGradeValue = memoryFile.MemoryStringsGrades[memoryFileToGrade - 1];
            newAvgGrade       = memoryFile.SaveGradesFile(bareFilename + ".grades");

            return(newAvgGrade);
        }
Ejemplo n.º 8
0
        private void GenerateConfigurationCacheFile(string filename)
        {
            string gradesfilepath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), filename);

            using (TextWriter textWriter = new StreamWriter(gradesfilepath))
            {
                string[] keys = new string[this.MemoryFiles.Keys.Count];
                this.MemoryFiles.Keys.CopyTo(keys, 0);
                for (int i = 0; i < keys.Length; i++)
                {
                    float avgGrade = MemoryFile.CalcAvgMemoryFileGrade(keys[i]);
                    this.MemoryFiles[keys[i]].GradeValue = avgGrade;

                    //write to cache file
                    DateTime currentModificationDate = System.IO.File.GetLastWriteTime(keys[i]);
                    textWriter.WriteLine(String.Format("{0} {1} {2}", keys[i], currentModificationDate.ToString("s"), avgGrade));
                }

                //Close handle to configuration cache file
                textWriter.Close();
            }
        }
Ejemplo n.º 9
0
        private static float CalcAvgGradeFromMemoryFile(string path)
        {
            float avgGrade = 0.0f;

            int totalMemoryStrings = MemoryFile.CountMemoryStrings(path);

            //Read grades if counts!= then append grades with 0.0
            int    totalGrades    = 0;
            string bareFilename   = Path.GetFileNameWithoutExtension(Path.GetFileName(path));
            string gradesfilepath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), bareFilename + ".grades");

            using (TextReader textReader = new StreamReader(gradesfilepath))
            {
                string line = null;
                while (null != (line = textReader.ReadLine()))
                {
                    switch (line)
                    {
                    case "":
                        //Eat empty lines
                        break;

                    default:
                        string[] brokenLine = line.Split(' ');
                        if (2 != brokenLine.Length)
                        {
                            throw new MemoryException(String.Format("[{0}] MemoryStrings Grade File paring error", gradesfilepath));
                        }

                        int memoryStringNo;
                        if (false == int.TryParse(brokenLine[0], out memoryStringNo))
                        {
                            throw new MemoryException(String.Format("[{0}] MemoryStrings Grade File paring error", gradesfilepath));
                        }
                        if (memoryStringNo != totalGrades + 1)
                        {
                            //There must be a strinct sequence in grades
                            throw new MemoryException(String.Format("[{0}] MemoryStrings Grade File paring error", gradesfilepath));
                        }

                        float memoryStringGrade;
                        if (false == float.TryParse(brokenLine[1], out memoryStringGrade))
                        {
                            throw new MemoryException(String.Format("[{0}] MemoryStrings Grade File paring error", gradesfilepath));
                        }

                        avgGrade += memoryStringGrade;
                        totalGrades++;

                        break;
                    }
                }

                //Close handle to MemoryString Grades file
                textReader.Close();
            }

            if (totalGrades != totalMemoryStrings)
            {
                //Append 0.0 grades for new MemoryStrings
                using (TextWriter textWriter = new StreamWriter(gradesfilepath, true))
                {
                    for (int i = totalGrades + 1; i <= totalMemoryStrings; i++)
                    {
                        textWriter.WriteLine(String.Format("{0} {1}", i, 0.0f));
                    }

                    //Close handle to MemoryString Grades file
                    textWriter.Close();
                }
            }

            return(avgGrade / totalMemoryStrings);
        }