This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
NounUtil.cs
96 lines (76 loc) · 2.84 KB
/
NounUtil.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.IO;
namespace JetBrains.Util
{
public static class NounUtil
{
private static readonly IDictionary<string, string> ourPlurals = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private static readonly IDictionary<string, string> ourSingulars = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
static NounUtil()
{
using (var streamReader = new StreamReader(typeof(NounUtil).Assembly.GetManifestResourceStream("Nitra.Grammar.resources.plural.txt")))
{
while (true)
{
string line = streamReader.ReadLine();
if (line == null)
return;
string singular = line.Substring(0, line.IndexOf(" ", StringComparison.Ordinal));
string plural = line.Substring(line.IndexOf(" ", StringComparison.Ordinal) + 1);
if (!ourPlurals.ContainsKey(singular))
ourPlurals.Add(singular, plural);
if (!ourSingulars.ContainsKey(plural))
ourSingulars.Add(plural, singular);
}
}
}
public static string GetPlural(string singular)
{
string plural;
if (ourPlurals.TryGetValue(singular, out plural))
return plural;
if (singular.EndsWith("s") || singular.EndsWith("x") || singular.EndsWith("sh")|| singular.EndsWith("ch"))
return singular + "es";
if (singular.EndsWith("y"))
return singular.Substring(0, singular.Length - 1) + "ies";
return singular + "s";
}
public static string GetSingular(string plural)
{
string singular;
if (ourSingulars.TryGetValue(plural, out singular))
return singular;
if (!plural.EndsWith("s") || plural.EndsWith("ss") || plural.EndsWith("xs") || plural.EndsWith("shs"))
return plural;
if (plural.EndsWith("ies"))
return plural.Substring(0, plural.Length - 3) + "y";
if ( plural.EndsWith("sses") || plural.EndsWith("zes") || plural.EndsWith("xes") || plural.EndsWith("shes")|| plural.EndsWith("ches"))
return plural.Substring(0, plural.Length - 2);
return plural.Substring(0, plural.Length - 1);
}
public static string ToPluralOrSingular(string singular, int count)
{
if(singular == null)
throw new ArgumentNullException("singular");
return count == 1 || count == -1 ? singular : GetPlural(singular);
}
public static string GetCountString(int argumentIndex)
{
string suffix = "th";
switch (argumentIndex + 1)
{
case 1:
suffix = "st";
break;
case 2:
suffix = "nd";
break;
case 3:
suffix = "d";
break;
}
return argumentIndex + 1 + suffix;
}
}
}