protected override async Task When()
        {
            ExceptionMode = Testing.Abstractions.ExceptionMode.Record;
            var sb = new StringBuilder();

            for (int i = 0; i < 350; i++)
            {
                sb.Append(_fiveHundredCharacters);
            }
            var text = "";

            using (var textExtractor = TextExtractor.Open(@"C:\Users\matth\Desktop\test.txt"))
            {
                text = await textExtractor.ExtractAsync();
            }

            var textChunks = text.ChunkWithDelimeters(4800, '.', '!', '?', ')', '"', '}', ']');

            _request = new GoogleSpeechRequest(
                voiceSelection: new VoiceSelectionParams
            {
                Name = ""
            },
                audioConfig: new AudioConfig
            {
                AudioEncoding = AudioEncoding.Mp3
            },
                content: textChunks
                );
        }
示例#2
0
        static async Task Main(string[] args)
        {
            var filePath = $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\example.pdf";

            using (var extractor = TextExtractor.Open(filePath))
            {
                Console.WriteLine(await extractor.ExtractAsync());
            }

            Console.ReadLine();
        }
        public async Task StartAsync(TranslationRequest request)
        {
            int version = request.Version + 1;

            var fileResult = await _fileClient.DownloadAsync(
                fileName : $"{request.CorrelationId}{Path.GetExtension(request.FileName)}",
                userId : request.UserId
                );

            string text;

            using (var fileStream = await fileResult.OpenStreamAsync())
                using (var textExtractor = TextExtractor.Open(fileStream, fileResult.ContentType))
                {
                    text = await textExtractor.ExtractAsync();
                }

            var textChunks = text.ChunkWithDelimeters(5000, '.', '!', '?', ')', '"', '}', ']').ToArray();

            await _eventPublisher.PublishAsync(new TranslationStarted(
                                                   aggregateId : request.AggregateId,
                                                   version : version,
                                                   correlationId : request.CorrelationId,
                                                   causationId : request.CausationId,
                                                   characterCount : textChunks.Sum(tc => tc.Length),
                                                   userId : request.UserId
                                                   ));

            try
            {
                for (int i = 0; i < textChunks.Count(); i++)
                {
                    version++;
                    await _eventPublisher.PublishAsync(new TranslationCharactersProcessed(
                                                           aggregateId : request.AggregateId,
                                                           version : version,
                                                           correlationId : request.CorrelationId,
                                                           causationId : request.CausationId,
                                                           charactersProcessed : textChunks[i].Length,
                                                           userId : request.UserId
                                                           ));
                }

                using (var stream = new MemoryStream())
                    using (TextWriter tw = new StreamWriter(stream))
                    {
                        await tw.WriteAsync("test");

                        await tw.FlushAsync();

                        stream.Position = 0;
                        await _fileClient.UploadAsync($"{request.AggregateId}.mp3", request.UserId, stream);

                        await _eventPublisher.PublishAsync(new TranslationSucceded(
                                                               aggregateId : request.AggregateId,
                                                               version : ++version,
                                                               correlationId : request.CorrelationId,
                                                               causationId : request.CausationId,
                                                               userId : request.UserId
                                                               ));
                    }
            }
            catch (Exception e)
            {
                await _eventPublisher.PublishAsync(new TranslationFailed(
                                                       aggregateId : request.AggregateId,
                                                       version : ++version,
                                                       correlationId : request.CorrelationId,
                                                       causationId : request.CausationId,
                                                       error : e.Message,
                                                       userId : request.UserId
                                                       ));
            }
        }
        public async Task StartAsync(TranslationRequest request)
        {
            int version = request.Version + 1;

            var fileResult = await _fileClient.DownloadAsync(
                fileName : $"{request.CorrelationId}{Path.GetExtension(request.FileName)}",
                userId : request.UserId
                );

            string text;

            using (var fileStream = await fileResult.OpenStreamAsync())
                using (var textExtractor = TextExtractor.Open(fileStream, fileResult.ContentType))
                {
                    text = await textExtractor.ExtractAsync();
                }

            var textChunks = text.ChunkWithDelimeters(5000, '.', '!', '?', ')', '"', '}', ']');

            await _eventPublisher.PublishAsync(new TranslationStarted(
                                                   aggregateId : request.AggregateId,
                                                   version : version,
                                                   correlationId : request.CorrelationId,
                                                   causationId : request.CausationId,
                                                   characterCount : textChunks.Sum(tc => tc.Length),
                                                   userId : request.UserId
                                                   ));

            try
            {
                var result = await _speechSynthesizer.TranslateTextAsync(new GoogleSpeechRequest(
                                                                             voiceSelection : new VoiceSelectionParams
                {
                    Name = request.TranslationSubject
                },
                                                                             audioConfig : new AudioConfig
                {
                    AudioEncoding = AudioEncoding.Mp3
                },
                                                                             content : textChunks,
                                                                             callback : async length =>
                {
                    Interlocked.Increment(ref version);
                    await _semaphoreSlim.WaitAsync();

                    try
                    {
                        await _eventPublisher.PublishAsync(new TranslationCharactersProcessed(
                                                               aggregateId: request.AggregateId,
                                                               version: version,
                                                               correlationId: request.CorrelationId,
                                                               causationId: request.CausationId,
                                                               charactersProcessed: length,
                                                               userId: request.UserId
                                                               ));
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        _semaphoreSlim.Release();
                    }
                }
                                                                             ));


                using (var stream = await result.OpenStreamAsync())
                {
                    await _fileClient.UploadAsync($"{request.AggregateId}.mp3", request.UserId, stream);

                    await _eventPublisher.PublishAsync(new TranslationSucceded(
                                                           aggregateId : request.AggregateId,
                                                           version : ++version,
                                                           correlationId : request.CorrelationId,
                                                           causationId : request.CausationId,
                                                           userId : request.UserId
                                                           ));
                }
            }
            catch (Exception e)
            {
                await _eventPublisher.PublishAsync(new TranslationFailed(
                                                       aggregateId : request.AggregateId,
                                                       version : ++version,
                                                       correlationId : request.CorrelationId,
                                                       causationId : request.CausationId,
                                                       error : e.Message,
                                                       userId : request.UserId
                                                       ));
            }
        }
示例#5
0
        public async Task StartAsync(TranslationRequest request)
        {
            int version = request.Version + 1;

            var fileResult = await _fileClient.DownloadAsync(
                fileName : $"{request.CorrelationId.ToString()}{Path.GetExtension(request.FileName)}",
                userId : request.UserId
                );

            string text;

            using (var fileStream = await fileResult.OpenStreamAsync())
                using (var textExtractor = TextExtractor.Open(fileStream, fileResult.ContentType))
                {
                    text = await textExtractor.ExtractAsync();
                }

            var textChunks = text.ChunkWithDelimeters(100000, '.', '!', '?', ')', '"', '}', ']');

            await _eventPublisher.PublishAsync(new TranslationStarted(
                                                   aggregateId : request.AggregateId,
                                                   version : version,
                                                   correlationId : request.CorrelationId,
                                                   causationId : request.CausationId,
                                                   characterCount : text.Length,
                                                   userId : request.UserId
                                                   ));

            try
            {
                var result = await _speechSynthesizer.TranslateTextAsync(new AWSSpeechRequest {
                    OutputFormat = "",
                    Content      = textChunks,
                    VoiceId      = request.TranslationSubject,
                    CallBack     = async length =>
                    {
                        Interlocked.Increment(ref version);
                        await _semaphoreSlim.WaitAsync();

                        try
                        {
                            await _eventPublisher.PublishAsync(new TranslationCharactersProcessed(
                                                                   aggregateId: request.AggregateId,
                                                                   version: version,
                                                                   correlationId: request.CorrelationId,
                                                                   causationId: request.CausationId,
                                                                   charactersProcessed: length,
                                                                   userId: request.UserId
                                                                   ));
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                        finally
                        {
                            _semaphoreSlim.Release();
                        }
                    }
                });;


                using (var stream = await result.OpenStreamAsync())
                {
                    await _fileClient.UploadAsync($"{request.AggregateId}.mp3", request.UserId, stream);

                    await _eventPublisher.PublishAsync(new TranslationSucceded(
                                                           aggregateId : request.AggregateId,
                                                           version : version + 1,
                                                           correlationId : request.CorrelationId,
                                                           causationId : request.CausationId,
                                                           userId : request.UserId
                                                           ));
                }
            }
            catch (Exception e)
            {
                await _eventPublisher.PublishAsync(new TranslationFailed(
                                                       aggregateId : request.AggregateId,
                                                       version : version + 1,
                                                       correlationId : request.CorrelationId,
                                                       causationId : request.CausationId,
                                                       error : e.Message,
                                                       userId : request.UserId
                                                       ));
            }
        }