-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
TextFileAlignmentCollection.cs
75 lines (66 loc) · 2.17 KB
/
TextFileAlignmentCollection.cs
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
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace SIL.Machine.Corpora
{
public class TextFileAlignmentCollection : IAlignmentCollection
{
private readonly string _fileName;
public TextFileAlignmentCollection(string id, string fileName)
{
Id = id;
_fileName = fileName;
}
public string Id { get; }
public string SortKey => Id;
public bool MissingRowsAllowed => false;
public int Count(bool includeEmpty = true)
{
using (var reader = new StreamReader(_fileName))
{
int count = 0;
string line;
while ((line = reader.ReadLine()) != null)
{
if (includeEmpty || line.Trim().Length > 0)
count++;
}
return count;
}
}
public IEnumerator<AlignmentRow> GetEnumerator()
{
return GetRows().GetEnumerator();
}
public IEnumerable<AlignmentRow> GetRows()
{
using (var reader = new StreamReader(_fileName))
{
int lineNum = 1;
string line;
while ((line = reader.ReadLine()) != null)
{
int index = line.IndexOf("\t");
object rowRef;
if (index >= 0)
{
string[] keys = line.Substring(0, index).Trim().Split('-', '_');
rowRef = new MultiKeyRef(Id, keys.Select(k => int.TryParse(k, out int ki) ? (object)ki : k));
line = line.Substring(index + 1);
}
else
{
rowRef = new MultiKeyRef(Id, lineNum);
}
yield return new AlignmentRow(Id, rowRef) { AlignedWordPairs = AlignedWordPair.Parse(line) };
lineNum++;
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}