-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
83 lines (79 loc) · 2.6 KB
/
Program.cs
File metadata and controls
83 lines (79 loc) · 2.6 KB
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
using System;
using System.IO; // Mono compatibility
using System.Text.RegularExpressions;
namespace SentSplit
// Mono compatibility
{
public class Program
{
public static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Usage: sentsplit --join <file> or sentsplit --split <file>");
return;
}
switch (args[0])
{
case "--join":
Join(args[1]);
break;
case "--split":
Split(args[1]);
break;
default:
Console.WriteLine("Usage: sentsplit --join <file> or sentsplit --split <file>");
break;
}
}
public static void Join(string source)
{
string[] data = File.ReadAllLines(source);
int i = 0;
List<string> paras = new();
while (i < data.Length)
{
string firstLine = data[i];
if (string.IsNullOrWhiteSpace(firstLine))
{
i++;
continue;
}
int iLocal = i;
var paraLines = new List<string>();
string localLine = data[iLocal];
while (!string.IsNullOrWhiteSpace(localLine) && iLocal < data.Length)
{
localLine = data[iLocal];
paraLines.Add(localLine);
iLocal++;
}
i = iLocal;
paras.Add(Regex.Replace((String.Join(" ", paraLines)), " +$", ""));
}
paras.ForEach(p => Console.WriteLine(p));
}
public static void Split(string source)
{
string[] data = File.ReadAllLines(source);
var output = new List<string>();
foreach (string line in data)
{
if (string.IsNullOrWhiteSpace(line))
continue;
string sentence = Regex.Replace(
line,
@"([\.\?!])\s+((--\s+)|(—\s+)?[A-ZА-Я])",
match =>
{
var m = match.Groups;
return string.Format("{0}\n{1}", m[1].ToString(), m[2].ToString());
}
);
string result = string.Format("{0}\n", sentence);
output.Add(result);
}
output.ForEach(p => Console.WriteLine(p));
}
}
}