File size: 3,156 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) Dr. Dirk Lellinger. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SlobViewer.Text
{
  /// <summary>
  /// Reads a text file as can be found at the TU Chemnitz (see <see href="http://dict.tu-chemnitz.de/"/>)

  /// </summary>
  public class TextReader
  {
    /// <summary>
    /// The full file name of the text file.
    /// </summary>
    string _fileName;

    static readonly char[] _partSeparator = new char[] { '|' };
    static readonly char[] _subPartSeparator = new char[] { ';' };

    /// <summary>Initializes a new instance of the <see cref="TextReader"/> class.</summary>
    /// <param name="fileName">Full file name of the text file.</param>
    public TextReader(string fileName)
    {
      _fileName = fileName;
    }

    /// <summary>
    /// Reads from the text file given in the constructor of this class and generates a dictionary.
    /// </summary>
    /// <returns>A dictionary of key-value pairs.</returns>
    public Dictionary<string, string> Read()
    {
      var dictionary = new Dictionary<string, string>();

      var keyList = new List<string>();

      using (var stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
      {

        string line;
        using (var xr = new StreamReader(stream))
        {
          while (null != (line = xr.ReadLine()))
          {
            if (line.Length == 0 || line[0] == '#')
              continue;

            // a line is separated by two colons in the part of the one language and the part with the other language
            // inside every part there may be additional separations by |   (hopefully the same number in both language parts)

            var idx = line.IndexOf("::");

            if (idx < 0)
              continue;

            var part1 = line.Substring(0, idx);
            var part2 = line.Substring(idx + 2);

            var parts1 = part1.Split(_partSeparator, StringSplitOptions.None);
            var parts2 = part2.Split(_partSeparator, StringSplitOptions.None);



            for (int i = 0; i < parts2.Length; ++i)
            {
              var keyt = parts2[i].Trim();
              var value = parts1[i].Trim();

              var keyParts = keyt.Split(_subPartSeparator, StringSplitOptions.RemoveEmptyEntries);

              foreach (var keya in keyParts)
              {
                var key = keya.Trim();

                if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
                {
                  if (dictionary.TryGetValue(key, out var existingValue))
                  {
                    existingValue += "\r\n" + value;
                    dictionary[key] = existingValue;
                  }
                  else
                  {
                    dictionary.Add(key, value);
                  }
                }
              }


            }
          }
        }

      }
      return dictionary;
    }


  }
}