Analysis.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using CsvHelper;
  7. using CsvHelper.Configuration;
  8. namespace memoQAnalysis
  9. {
  10. public class Analysis
  11. {
  12. public List<MemoQAnalysis> Data { get; }
  13. private readonly string _delimiter;
  14. public Analysis(string path, string delimiter = ",")
  15. {
  16. _delimiter = delimiter;
  17. Data = ReadFromFile(path);
  18. }
  19. public Analysis(byte[] data, string delimiter = ";")
  20. {
  21. _delimiter = delimiter;
  22. Data = ReadData(data);
  23. }
  24. private List<MemoQAnalysis> ReadFromFile(string path)
  25. {
  26. using (var sr = new StreamReader(path))
  27. {
  28. return ReadData(sr);
  29. }
  30. }
  31. private List<MemoQAnalysis> ReadData(StreamReader streamReader)
  32. {
  33. var configuration = new CsvConfiguration(CultureInfo.InvariantCulture)
  34. {
  35. Delimiter = _delimiter
  36. };
  37. using (var csv = new CsvReader(streamReader, configuration))
  38. {
  39. var firstLine = streamReader.ReadLine();
  40. if (!string.IsNullOrWhiteSpace(firstLine) && firstLine.StartsWith("file", StringComparison.InvariantCultureIgnoreCase))
  41. {
  42. streamReader.BaseStream.Position = 0;
  43. streamReader.DiscardBufferedData();
  44. }
  45. var result = csv.GetRecords<MemoQAnalysis>().ToList();
  46. return result;
  47. }
  48. }
  49. private List<MemoQAnalysis> ReadData(byte[] data)
  50. {
  51. using (var ms = new MemoryStream(data))
  52. using (var sr = new StreamReader(ms))
  53. {
  54. return ReadData(sr);
  55. }
  56. }
  57. public int WordsToTranslateWithoutRepetitions()
  58. {
  59. var total = 0;
  60. foreach (var file in Data)
  61. {
  62. total += file.NinentyFiveNineWords;
  63. total += file.EightyFiveNinentyFourWords;
  64. total += file.SeventyFiveEightyFourWords;
  65. total += file.FiftySeventyFourWords;
  66. total += file.NoMatchWords;
  67. }
  68. return total;
  69. }
  70. public int WordsToTranslateWithRepetitions()
  71. {
  72. var total = WordsToTranslateWithoutRepetitions();
  73. foreach (var file in Data)
  74. {
  75. total += file.RepetitionsWords;
  76. }
  77. return total;
  78. }
  79. }
  80. }