Analysis.cs 3.1 KB

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