1
1
1SLY22020-04-15 18:08:52
Speech recognition
1SLY2, 2020-04-15 18:08:52

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));
        }
    }

Tell me what the problem is, there are thoughts, but very scary.
Ps I use MVVM, maybe there are still some spokes in the wheels.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Korotenko, 2020-04-15
@firedragon

// и вообще так себе идея называть список словом текст, лучше 
List<string> text = new List<string>();
private readonly List<string> _translatedTextLines = new List<string>();
var output = string.Join("\n",text);

#
#, 2020-04-15
@mindtester

make changes in the code:

WritingToTextFile

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)
ReadingFromTextFile

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")если нужен массив строк

but this is so, the introduction, in order to remove the garbage code
, you do not provide the code that actually performs voice recording / recognition, and something else. all that you have shown is foreplay preparation .. where is the process then? ( upd the one that returns empty text, or you lose it on the save path)

E
edward_freedom, 2020-04-15
@edward_freedom

Try replacing
sw.WriteLine(text);
with

sw.WriteLine(string.join(Environment.NewLine, text));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question