123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using CsvHelper;
- using CsvHelper.Configuration;
- namespace memoQAnalysis
- {
- public class Analysis
- {
- public List<MemoQAnalysis> Data { get; }
- private readonly string _delimiter;
- public Analysis(string path, string delimiter = ",")
- {
- _delimiter = delimiter;
- Data = ReadFromFile(path);
- }
- public Analysis(byte[] data, string delimiter = ";")
- {
- _delimiter = delimiter;
- Data = ReadData(data);
- }
- private List<MemoQAnalysis> ReadFromFile(string path)
- {
- using (var sr = new StreamReader(path))
- {
- return ReadData(sr);
- }
- }
- private List<MemoQAnalysis> ReadData(StreamReader streamReader)
- {
- var configuration = new CsvConfiguration(CultureInfo.InvariantCulture)
- {
- Delimiter = _delimiter
- };
- using (var csv = new CsvReader(streamReader, configuration))
- {
- var firstLine = streamReader.ReadLine();
- if (!string.IsNullOrWhiteSpace(firstLine) && firstLine.StartsWith("file", StringComparison.InvariantCultureIgnoreCase))
- {
- streamReader.BaseStream.Position = 0;
- streamReader.DiscardBufferedData();
- }
- var result = csv.GetRecords<MemoQAnalysis>().ToList();
- return result;
- }
- }
- private List<MemoQAnalysis> ReadData(byte[] data)
- {
- using (var ms = new MemoryStream(data))
- using (var sr = new StreamReader(ms))
- {
- return ReadData(sr);
- }
- }
- public int WordsToTranslateWithoutRepetitions()
- {
- var total = 0;
- foreach (var file in Data)
- {
- total += file.NinentyFiveNineWords;
- total += file.EightyFiveNinentyFourWords;
- total += file.SeventyFiveEightyFourWords;
- total += file.FiftySeventyFourWords;
- total += file.NoMatchWords;
- }
- return total;
- }
- public int WordsToTranslateWithRepetitions()
- {
- var total = WordsToTranslateWithoutRepetitions();
- foreach (var file in Data)
- {
- total += file.RepetitionsWords;
- }
- return total;
- }
- }
- }
|