<commit_before>﻿//
// Copyright (c) 2013 <NAME> <<EMAIL>>.
// Alle Rechte vorbehalten.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.IO;
using System.Speech.Recognition;
using System.Text;
using System.Threading;

namespace de.fhb.oll.transcripter
{
    class Program
    {
        private static readonly CultureInfo INPUT_LANGUAGE_CULTURE = CultureInfo.GetCultureInfo("de-DE");

        private static readonly CultureInfo CSV_OUTPUT_CULTURE = CultureInfo.InvariantCulture;
        private static readonly Encoding CSV_OUTPUT_ENCODING = new UTF8Encoding(false);

        private static readonly CultureInfo CLJ_OUTPUT_CULTURE = CultureInfo.InvariantCulture;
        private static readonly Encoding CLJ_OUTPUT_ENCODING = new UTF8Encoding(false);

        private const int GLOBAL_LIST_LENGTH = 10;
        private const int LOCAL_LIST_LENGTH = 5;

        private static string sourceFile;
        private static string inputName;
        private static string targetFile;

        private static AutoResetEvent exitEvent;
        private static double confidenceSum;
        private static long phraseCount;
        private static Dictionary<string, WordStats> hitlist;

        private static TextWriter outPhrases;
        private static TextWriter outWords;
        private static TextWriter outClojure;

        private static long resultNo;

        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("You need to specify a filename.");
                return;
            }

            sourceFile = args[0];
            inputName = Path.GetFileNameWithoutExtension(sourceFile) ?? "unknown";
            hitlist = new Dictionary<string, WordStats>();
            confidenceSum = 0.0;
            phraseCount = 0;
            resultNo = 0;
            exitEvent = new AutoResetEvent(false);

            var outputPath = Path.Combine(Path.GetDirectoryName(sourceFile) ?? "", "transcript");
            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }
            targetFile = Path.Combine(outputPath, inputName);

            Console.WriteLine(inputName);
            Console.WriteLine();
            Console.WriteLine("Starting transcription...");

            using (outPhrases = new StreamWriter(targetFile + ".phrases.csv", false, CSV_OUTPUT_ENCODING))
            using (outWords = new StreamWriter(targetFile + ".words.csv", false, CSV_OUTPUT_ENCODING))
            using (outClojure = new StreamWriter(targetFile + ".clj", false, CLJ_OUTPUT_ENCODING))
            using (var engine = new SpeechRecognitionEngine(INPUT_LANGUAGE_CULTURE))
            {
                BeginWriterOutput();

                engine.SpeechRecognized += RecognizerSpeechRecognizedHandler;
                engine.RecognizeCompleted += RecognizerRecognizeCompletedHandler;

                Grammar dictation = new DictationGrammar();
                dictation.Name = "Dictation Grammar";
                engine.LoadGrammar(dictation);

                engine.SetInputToWaveFile(sourceFile);
                engine.RecognizeAsync(RecognizeMode.Multiple);
                exitEvent.WaitOne();

                EndWriterOutput();
            }
        }

        private static void BeginWriterOutput()
        {
            outClojure.WriteLine("[");
        }

        private static void EndWriterOutput()
        {
            outClojure.WriteLine("]");
            outClojure.Flush();
        }

        private static void WriteWordStats()
        {
            using (var outWordStats = new StreamWriter(targetFile + ".wordstats.csv", false, Encoding.UTF8))
            {<commit_msg>added commented code for audio streaming from ffmpeg (not working because no random access to stdout of ffmpeg)<commit_after>﻿//
// Copyright (c) 2013 <NAME> <<EMAIL>>.
// Alle Rechte vorbehalten.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.IO;
using System.Speech.Recognition;
using System.Text;
using System.Threading;

namespace de.fhb.oll.transcripter
{
    class Program
    {
        private static readonly CultureInfo INPUT_LANGUAGE_CULTURE = CultureInfo.GetCultureInfo("de-DE");

        private static readonly CultureInfo CSV_OUTPUT_CULTURE = CultureInfo.InvariantCulture;
        private static readonly Encoding CSV_OUTPUT_ENCODING = new UTF8Encoding(false);

        private static readonly CultureInfo CLJ_OUTPUT_CULTURE = CultureInfo.InvariantCulture;
        private static readonly Encoding CLJ_OUTPUT_ENCODING = new UTF8Encoding(false);

        private const int GLOBAL_LIST_LENGTH = 10;
        private const int LOCAL_LIST_LENGTH = 5;

        private static string sourceFile;
        private static string inputName;
        private static string targetFile;

        private static AutoResetEvent exitEvent;
        private static double confidenceSum;
        private static long phraseCount;
        private static Dictionary<string, WordStats> hitlist;

        private static TextWriter outPhrases;
        private static TextWriter outWords;
        private static TextWriter outClojure;

        private static long resultNo;

        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("You need to specify a filename.");
                return;
            }

            sourceFile = args[0];
            inputName = Path.GetFileNameWithoutExtension(sourceFile) ?? "unknown";
            hitlist = new Dictionary<string, WordStats>();
            confidenceSum = 0.0;
            phraseCount = 0;
            resultNo = 0;
            exitEvent = new AutoResetEvent(false);

            var outputPath = Path.Combine(Path.GetDirectoryName(sourceFile) ?? "", "transcript");
            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }
            targetFile = Path.Combine(outputPath, inputName);

            Console.WriteLine(inputName);
            Console.WriteLine();
            Console.WriteLine("Starting transcription...");

            using (outPhrases = new StreamWriter(targetFile + ".phrases.csv", false, CSV_OUTPUT_ENCODING))
            using (outWords = new StreamWriter(targetFile + ".words.csv", false, CSV_OUTPUT_ENCODING))
            using (outClojure = new StreamWriter(targetFile + ".clj", false, CLJ_OUTPUT_ENCODING))
            using (var engine = new SpeechRecognitionEngine(INPUT_LANGUAGE_CULTURE))
            {
                BeginWriterOutput();

                engine.SpeechRecognized += RecognizerSpeechRecognizedHandler;
                engine.RecognizeCompleted += RecognizerRecognizeCompletedHandler;

                Grammar dictation = new DictationGrammar();
                dictation.Name = "Dictation Grammar";
                engine.LoadGrammar(dictation);

                engine.SetInputToWaveFile(sourceFile);
                engine.RecognizeAsync(RecognizeMode.Multiple);
                exitEvent.WaitOne();

                EndWriterOutput();
            }
        }

        //private static Stream ExtractAudio(string file)
        //{
        //    var ffmpeg =
        //        Path.Combine(
        //            Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath) ?? "",
        //            "ffmpeg.exe");
        //    if (!File.Exists(ffmpeg))
        //    {
        //        throw new FileNotFoundException("Could not find the ffmpeg executable.", ffmpeg);
        //    }

        //    var psi = new ProcessStartInfo(ffmpeg,
        //        string.Format("-i \"{0}\" -n -vn -ac 1 -ar 16000 -acodec pcm_s16le pipe:1", file));
        //    psi.RedirectStandardOutput = true;
        //    psi.CreateNoWindow = false;
        //    psi.UseShellExecute = false;
            
        //    var p = Process.Start(psi);
        //    return p.StandardOutput.BaseStream;
        //}

        private static void BeginWriterOutput()
        {
            outClojure.WriteLine("[");
        }

        private static void EndWriterOutput()
        {
            outClojure.WriteLine("]");
            outClojure.Flush();
        }

        private static void WriteWordStats()
        {
            using (var outWordStats = new StreamWriter(targetFile + ".wordstats.csv", false, Encoding.UTF8))
            {
{'fraction_non_alphanumeric': 0.08696161048689138, 'fraction_numerical': 0.0049157303370786515, 'mean_word_length': 2.1636430951499444, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1936045', 'n_tokens_mistral': 2305, 'n_tokens_neox': 2138, 'n_words': 522}