Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
0
Reply
Populate list from file in a method and use it in another?
Stian Saether
Nov 9 2017 8:27 AM
202
Reply
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:
using
System;
using
System.Collections.Generic;
using
System.IO;
using
System.Linq;
using
System.Text;
namespace
TSG
{
class
Program
{
class
Word
{
public
string
Str {
get
;
set
; }
public
bool
IsSorO {
get
;
set
; }
public
bool
IsV {
get
;
set
; }
public
bool
IsPerson {
get
;
set
; }
public
static
Word FromCsv(
string
csvLine)
{
string
[] values = csvLine.Split(
';'
);
Word words =
new
Word();
words.Str = Convert.ToString(values[0]);
words.IsSorO = Convert.ToBoolean(values[1]);
words.IsV = Convert.ToBoolean(values[2]);
words.IsPerson = Convert.ToBoolean(values[3]);
return
words;
}
}
class
CSVHeader
{
public
string
Str {
get
;
set
; }
public
string
IsSorO {
get
;
set
; }
public
string
IsV {
get
;
set
; }
public
string
IsPerson {
get
;
set
; }
}
public
static
int
NumOfSentences = 0;
static
void
Main(
string
[] args)
{
Intro();
GetDictionary(
"dictionary.txt"
);
while
(AwaitKey()) { }
}
static
void
GetDictionary(
string
fileName)
{
if
(File.Exists(fileName))
{
// Read from file
List
words = File.ReadAllLines(fileName)
.Skip(1)
// skip header row
.Select(v => Word.FromCsv(v))
.ToList();
}
else
{
// Create new dictionary file based on default list data
List
csvHeader =
new
List
{
new
CSVHeader { Str =
"WORD"
, IsSorO =
"IS_SUB_OR_OBJ"
, IsV =
"IS_VERB"
, IsPerson =
"IS_NAME"
}
};
List
words =
new
List
{
new
Word { Str =
"Janet Jackson"
, IsSorO =
true
, IsV =
false
, IsPerson =
true
},
new
Word { Str =
"A dog"
, IsSorO =
true
, IsV =
false
, IsPerson =
false
},
new
Word { Str =
"The man"
, IsSorO =
true
, IsV =
false
, IsPerson =
false
},
new
Word { Str =
"The boy"
, IsSorO =
true
, IsV =
false
, IsPerson =
false
},
new
Word { Str =
"Bill Gates"
, IsSorO =
true
, IsV =
false
, IsPerson =
true
},
new
Word { Str =
"is running after"
, IsSorO =
false
, IsV =
true
, IsPerson =
false
},
new
Word { Str =
"is talking to"
, IsSorO =
false
, IsV =
true
, IsPerson =
false
},
new
Word { Str =
"is smiling at"
,IsSorO =
false
, IsV =
true
, IsPerson =
false
},
new
Word { Str =
"is singing a duet with"
, IsSorO =
false
, IsV =
true
, IsPerson =
false
}
};
string
ToWrite = CreateCSVTextFile(csvHeader,
";"
)
+ CreateCSVTextFile(words,
";"
);
File.WriteAllText(fileName, ToWrite);
}
}
private
static
string
CreateCSVTextFile
(List
data,
string
seperator =
","
)
{
var properties =
typeof
(T).GetProperties();
var result =
new
StringBuilder();
foreach
(var row
in
data)
{
var values = properties.Select(p => p.GetValue(row,
null
));
var line =
string
.Join(seperator, values);
result.AppendLine(line);
}
return
result.ToString();
}
static
void
Generate()
{
// If didn't have to load the dictionary file here, but could use the one
// loaded in GetDictionary(), either from text file or from the default list
// set in GetDictionary() (in case of file not existing), I would be happy!
string
FileName =
"dictionary.txt"
;
List
words = File.ReadAllLines(FileName)
.Skip(1)
.Select(v => Word.FromCsv(v))
.ToList();
var rnd =
new
Random();
Word wordS =
new
Word();
Word wordV =
new
Word();
Word wordO =
new
Word();
wordS = words.Where(w => w.IsV ==
false
).OrderBy(x => rnd.Next()).FirstOrDefault();
wordV = words.Where(w => w.IsV ==
true
).OrderBy(x => rnd.Next()).FirstOrDefault();
do
{
wordO = words.Where(w => w.IsV ==
false
).OrderBy(x => rnd.Next()).FirstOrDefault();
}
while
(wordS.Str == wordO.Str);
var Sentence = wordS.Str +
" "
+ wordV.Str +
" "
;
var Obj = (wordO.IsPerson ==
true
) ? wordO.Str : wordO.Str.ToLower();
Sentence = Sentence + Obj +
"."
;
Console.WriteLine(Sentence);
NumOfSentences++;
if
(NumOfSentences > 9)
{
Console.WriteLine(
"[ENTER] = New sentence | [ESC] = Quit"
);
NumOfSentences = 0;
}
}
static
bool
AwaitKey()
{
bool
acceptedKey =
false
;
do
{
ConsoleKeyInfo keyRead = Console.ReadKey();
switch
(keyRead.Key)
{
case
ConsoleKey.Enter:
// Make new sentence
acceptedKey =
true
;
Generate();
return
true
;
case
ConsoleKey.Escape:
// Quit the app
acceptedKey =
true
;
Console.Write(
"\b \b"
);
return
false
;
default
:
// Not accepted key pressed
Console.Write(
"\b \b"
);
acceptedKey =
false
;
return
true
;
}
}
while
(!acceptedKey);
}
static
void
Intro()
{
Console.Clear();
Console.WriteLine(
"Random Sentence Generator"
);
Console.WriteLine(
"[ENTER] = New sentence | [ESC] = Quit"
);
}
}
}
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
0
)
Next Recommended Forum
A problem caused the program to stop working correctly. C#
Basic regex question