Answer the question
In order to leave comments, you need to log in
Wrong data goes to the file, why?
The file gets not what is needed, not at all what is needed "System.Collections.Generic.List`1[System.String]", namely this one. The file should display the text that I recorded from the microphone, recorded with the NAudio library.
Here is the speech recognizer code:
class ReadFromAFile : INotifyPropertyChanged
{
SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new CultureInfo("en-US"));
Grammar dictation = new DictationGrammar();
List<string> text = new List<string>();
public ReadFromAFile()
{
dictation.Name = "Dictation Grammar";
recognizer.LoadGrammar(dictation);
recognizer.SetInputToAudioStream(File.OpenRead(@"Ex_voice.wav"), new SpeechAudioFormatInfo(44100, AudioBitsPerSample.Sixteen, AudioChannel.Mono));
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognizedHandler);
recognizer.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(RecognizeCompletedHandler);
}
public void WritingToTextFile()
{
using (StreamWriter sw = new StreamWriter("Text.txt", false, System.Text.Encoding.Default))
{
sw.WriteLine(text);
sw.Close();
}
}
public List<string> ReadingFromTextFile()
{
List<string> temp = new List<string>();
using (StreamReader sr = new StreamReader("Text.txt"))
{
temp.Add(sr.ReadToEnd());
}
return temp;
}
private void SpeechRecognizedHandler(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result != null && e.Result.Text != null)
{
text.Add(e.Result.Text);
}
else
{
MessageBox.Show("File is empty");
}
}
private void RecognizeCompletedHandler(object sender, RecognizeCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(" Error encountered " +
e.Error.GetType().Name + " " + e.Error.Message);
}
if (e.Cancelled)
{
MessageBox.Show(" Operation cancelled.");
}
if (e.InputStreamEnded)
{
MessageBox.Show(" End of stream encountered.");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
Answer the question
In order to leave comments, you need to log in
// и вообще так себе идея называть список словом текст, лучше
List<string> text = new List<string>();
private readonly List<string> _translatedTextLines = new List<string>();
var output = string.Join("\n",text);
make changes in the code:
public void WritingToTextFile()
{
using (StreamWriter sw = new StreamWriter("Text.txt", false, System.Text.Encoding.Default))
{
sw.WriteLine(text);
sw.Close();
}
}
наFile.WriteAllText("Text.txt", text)
public List<string> ReadingFromTextFile()
{
List<string> temp = new List<string>();
using (StreamReader sr = new StreamReader("Text.txt"))
{
temp.Add(sr.ReadToEnd());
}
return temp;
}
а тут надо подумать! наFile.ReadAllText("Text.txt")
если одной строкой, илиFile.ReadAllLines("Text.txt")
если нужен массив строкTry replacing
sw.WriteLine(text);
with
sw.WriteLine(string.join(Environment.NewLine, text));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question