Stian Saether

Stian Saether

  • NA
  • 65
  • 1.4k

Populate list from file in a method and use it in another?

Nov 9 2017 8:27 AM
I have a console app which generates random sentences based on a List-object containing words and phrases.
 
The list object can be populated either from a dictionary file if it exists, or by dummy words and phrases defined in the GetDictionary()-method, which are then written to a new dictionary file for later use. The idea is that the user could edit the dictionary file to make personalized sentences.
 
After the dictionary has been loaded into the List-object, the Generate()-method picks one subject, one verb and one object, making a full sentence in the style of "[The boy] (subject) [is talking to] (verb) [the girl] (object).
 
The problem is that I have to load the dictionary in the Generate()-method.
 
Can it be done in such a way that the GetDictionary()-method populates the list object either from file or from the dummy values within the method, and then the list object would be accessible from within the Generate()-method?
Source code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. namespace TSG
  7. {
  8. class Program
  9. {
  10. class Word
  11. {
  12. public string Str { get; set; }
  13. public bool IsSorO { get; set; }
  14. public bool IsV { get; set; }
  15. public bool IsPerson { get; set; }
  16. public static Word FromCsv(string csvLine)
  17. {
  18. string[] values = csvLine.Split(';');
  19. Word words = new Word();
  20. words.Str = Convert.ToString(values[0]);
  21. words.IsSorO = Convert.ToBoolean(values[1]);
  22. words.IsV = Convert.ToBoolean(values[2]);
  23. words.IsPerson = Convert.ToBoolean(values[3]);
  24. return words;
  25. }
  26. }
  27. class CSVHeader
  28. {
  29. public string Str { get; set; }
  30. public string IsSorO { get; set; }
  31. public string IsV { get; set; }
  32. public string IsPerson { get; set; }
  33. }
  34. public static int NumOfSentences = 0;
  35. static void Main(string[] args)
  36. {
  37. Intro();
  38. GetDictionary("dictionary.txt");
  39. while (AwaitKey()) { }
  40. }
  41. static void GetDictionary(string fileName)
  42. {
  43. if (File.Exists(fileName))
  44. {
  45. // Read from file
  46. List words = File.ReadAllLines(fileName)
  47. .Skip(1) // skip header row
  48. .Select(v => Word.FromCsv(v))
  49. .ToList();
  50. }
  51. else
  52. {
  53. // Create new dictionary file based on default list data
  54. List csvHeader = new List
  55. {
  56. new CSVHeader { Str = "WORD", IsSorO = "IS_SUB_OR_OBJ", IsV = "IS_VERB", IsPerson = "IS_NAME" }
  57. };
  58. List words = new List
  59. {
  60. new Word { Str = "Janet Jackson", IsSorO = true, IsV = false, IsPerson = true },
  61. new Word { Str = "A dog", IsSorO = true, IsV = false, IsPerson = false },
  62. new Word { Str = "The man", IsSorO = true, IsV = false, IsPerson = false },
  63. new Word { Str = "The boy", IsSorO = true, IsV = false, IsPerson = false },
  64. new Word { Str = "Bill Gates", IsSorO = true, IsV = false, IsPerson = true },
  65. new Word { Str = "is running after", IsSorO = false, IsV = true, IsPerson = false },
  66. new Word { Str = "is talking to", IsSorO = false, IsV = true, IsPerson = false },
  67. new Word { Str = "is smiling at",IsSorO = false, IsV = true, IsPerson = false },
  68. new Word { Str = "is singing a duet with", IsSorO = false, IsV = true, IsPerson = false }
  69. };
  70. string ToWrite = CreateCSVTextFile(csvHeader, ";")
  71. + CreateCSVTextFile(words, ";");
  72. File.WriteAllText(fileName, ToWrite);
  73. }
  74. }
  75. private static string CreateCSVTextFile(List data, string seperator = ",")
  76. {
  77. var properties = typeof(T).GetProperties();
  78. var result = new StringBuilder();
  79. foreach (var row in data)
  80. {
  81. var values = properties.Select(p => p.GetValue(row, null));
  82. var line = string.Join(seperator, values);
  83. result.AppendLine(line);
  84. }
  85. return result.ToString();
  86. }
  87. static void Generate()
  88. {
  89. // If didn't have to load the dictionary file here, but could use the one
  90. // loaded in GetDictionary(), either from text file or from the default list
  91. // set in GetDictionary() (in case of file not existing), I would be happy!
  92. string FileName = "dictionary.txt";
  93. List words = File.ReadAllLines(FileName)
  94. .Skip(1)
  95. .Select(v => Word.FromCsv(v))
  96. .ToList();
  97. var rnd = new Random();
  98. Word wordS = new Word();
  99. Word wordV = new Word();
  100. Word wordO = new Word();
  101. wordS = words.Where(w => w.IsV == false).OrderBy(x => rnd.Next()).FirstOrDefault();
  102. wordV = words.Where(w => w.IsV == true).OrderBy(x => rnd.Next()).FirstOrDefault();
  103. do
  104. {
  105. wordO = words.Where(w => w.IsV == false).OrderBy(x => rnd.Next()).FirstOrDefault();
  106. } while (wordS.Str == wordO.Str);
  107. var Sentence = wordS.Str + " " + wordV.Str + " ";
  108. var Obj = (wordO.IsPerson == true) ? wordO.Str : wordO.Str.ToLower();
  109. Sentence = Sentence + Obj + ".";
  110. Console.WriteLine(Sentence);
  111. NumOfSentences++;
  112. if (NumOfSentences > 9)
  113. {
  114. Console.WriteLine("[ENTER] = New sentence | [ESC] = Quit");
  115. NumOfSentences = 0;
  116. }
  117. }
  118. static bool AwaitKey()
  119. {
  120. bool acceptedKey = false;
  121. do
  122. {
  123. ConsoleKeyInfo keyRead = Console.ReadKey();
  124. switch (keyRead.Key)
  125. {
  126. case ConsoleKey.Enter: // Make new sentence
  127. acceptedKey = true;
  128. Generate();
  129. return true;
  130. case ConsoleKey.Escape: // Quit the app
  131. acceptedKey = true;
  132. Console.Write("\b \b");
  133. return false;
  134. default: // Not accepted key pressed
  135. Console.Write("\b \b");
  136. acceptedKey = false;
  137. return true;
  138. }
  139. } while (!acceptedKey);
  140. }
  141. static void Intro()
  142. {
  143. Console.Clear();
  144. Console.WriteLine("Random Sentence Generator");
  145. Console.WriteLine("[ENTER] = New sentence | [ESC] = Quit");
  146. }
  147. }
  148. }