Skip to content

Commit 895d023

Browse files
authored
Merge pull request #5 from Umplify/master
Further improvements in the existing unit test library made by leveraging .net standard version of SharpSimpleNLG instead of the full .net version.
2 parents 6d0bbf5 + 8f90ee6 commit 895d023

13 files changed

+1729
-11
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[![Build status](https://ci.appveyor.com/api/projects/status/bjwkwoqdlpcl1jir?svg=true)](https://ci.appveyor.com/project/Arash-Sabet/sharpsimplenlg-olve3)
12
# SharpSimpleNLG
23
SharpSimpleNLG is a C# port of the [University of Aberdeen *simplenlg* project](https://github.com/simplenlg/simplenlg). All thanks and acknowledgement for hard work goes to this team.
34

@@ -10,7 +11,6 @@ Install-Package SharpSimpleNLG
1011
```
1112

1213
## Version News
13-
1414
Version 1.2.0:
1515
* Thanks to [PR from Arash-Sabet](https://github.com/nickhodge/SharpSimpleNLG/pull/3) now supports .NET Standard 1.6 and .NET 4.6.2; along with VS2017 support. Added to NuGet package (I still hope my packaging is OK!)
1616

SharpSimpleNLG.sln

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpSimpleNLGDotNet45", "S
1818
EndProject
1919
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpSimpleNLG", "SharpSimpleNLG\SharpSimpleNLG.csproj", "{16F344A8-B688-4D95-8DA6-2E207D47AECB}"
2020
EndProject
21+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpSimpleNLGConsoleNetCore", "SharpSimpleNLGConsoleNetCore\SharpSimpleNLGConsoleNetCore.csproj", "{3E619416-20EC-429F-A467-9F7BFE8BACB1}"
22+
EndProject
2123
Global
2224
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2325
Debug|Any CPU = Debug|Any CPU

SharpSimpleNLG/SharpSimpleNLG.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
1414
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
1515
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
16+
<Version>1.2.0</Version>
17+
<PackageLicenseUrl>https://github.com/nickhodge/SharpSimpleNLG/blob/master/LICENSE.md</PackageLicenseUrl>
18+
<PackageProjectUrl />
19+
<RepositoryUrl>https://github.com/nickhodge/SharpSimpleNLG</RepositoryUrl>
20+
<RepositoryType>git</RepositoryType>
1621
</PropertyGroup>
1722
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.6'">
1823
<OutputPath>bin\$(Configuration)\$(Framework)</OutputPath>

SharpSimpleNLGTests/SharpSimpleNLGTests.csproj

+6-3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
</Choose>
6767
<ItemGroup>
6868
<Compile Include="helpers\ExtensionHelpersTest.cs" />
69+
<Compile Include="morphology\DeterminerAgrHelper.cs" />
70+
<None Include="morphology\MorphologyProcessor.cs" />
71+
<None Include="morphology\MorphologyRules.cs" />
6972
<Compile Include="Properties\AssemblyInfo.cs" />
7073
</ItemGroup>
7174
<ItemGroup>
@@ -106,9 +109,9 @@
106109
<None Include="packages.config" />
107110
</ItemGroup>
108111
<ItemGroup>
109-
<ProjectReference Include="..\SharpSimpleNLGDotNet\SharpSimpleNLGDotNet45.csproj">
110-
<Project>{fd418a41-a46e-4fd7-a6db-ff3ebb4afa29}</Project>
111-
<Name>SharpSimpleNLGDotNet45</Name>
112+
<ProjectReference Include="..\SharpSimpleNLG\SharpSimpleNLG.csproj">
113+
<Project>{16f344a8-b688-4d95-8da6-2e207d47aecb}</Project>
114+
<Name>SharpSimpleNLG</Name>
112115
</ProjectReference>
113116
</ItemGroup>
114117
<Choose>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/*
2+
* The contents of this file are subject to the Mozilla Public License
3+
* Version 1.1 (the "License"); you may not use this file except in
4+
* compliance with the License. You may obtain a copy of the License at
5+
* http://www.mozilla.org/MPL/
6+
*
7+
* Software distributed under the License is distributed on an "AS IS"
8+
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9+
* License for the specific language governing rights and limitations
10+
* under the License.
11+
*
12+
* The Original Code is "SharpSimpleNLG".
13+
*
14+
* The Initial Developer of the Original Code is Ehud Reiter, Albert Gatt and Dave Westwater.
15+
* Portions created by Ehud Reiter, Albert Gatt and Dave Westwater are Copyright (C) 2010-11 The University of Aberdeen. All Rights Reserved.
16+
*
17+
* Contributor(s): Ehud Reiter, Albert Gatt, Dave Wewstwater, Roman Kutlak, Margaret Mitchell, Saad Mahamood, Nick Hodge
18+
*/
19+
20+
/* Additional Notes:
21+
* - Original Java source is SimpleNLG from 12-Jun-2016 https://github.com/simplenlg/simplenlg
22+
* - This is a port of the Java version to C# with no additional features
23+
* - I have left the "Initial Developer" section to reflect this fact
24+
* - Any questions, comments, feedback on this port can be sent to Nick Hodge <[email protected]>
25+
*/
26+
27+
using System.Text;
28+
using SimpleNLG.Extensions;
29+
30+
namespace SharpNLG.Extensions
31+
{
32+
/**
33+
* This class is used to parse numbers that are passed as figures, to determine
34+
* whether they should take "a" or "an" as determiner.
35+
*
36+
* @author bertugatt
37+
*
38+
*/
39+
40+
public class DeterminerAgrHelper
41+
{
42+
/*
43+
* An array of strings which are exceptions to the rule that "an" comes
44+
* before vowels
45+
*/
46+
private static string[] AN_EXCEPTIONS = {"one", "180", "110"};
47+
48+
/*
49+
* Start of string involving vowels, for use of "an"
50+
*/
51+
private static string AN_AGREEMENT = @"\A(a|e|i|o|u).*";
52+
53+
/*
54+
* Start of string involving numbers, for use of "an" -- courtesy of Chris
55+
* Howell, Agfa healthcare corporation
56+
*/
57+
// private static final string AN_NUMERAL_AGREEMENT =
58+
// "^(((8((\\d+)|(\\d+(\\.|,)\\d+))?).*)|((11|18)(\\d{3,}|\\D)).*)$";
59+
60+
/**
61+
* Check whether this string starts with a number that needs "an" (e.g.
62+
* "an 18% increase")
63+
*
64+
* @param string
65+
* the string
66+
* @return <code>true</code> if this string starts with 11, 18, or 8,
67+
* excluding strings that start with 180 or 110
68+
*/
69+
70+
public static bool requiresAn(string stringa)
71+
{
72+
var req = false;
73+
74+
var lowercaseInput = stringa.toLowerCase();
75+
76+
if (lowercaseInput.matches(AN_AGREEMENT) && !isAnException(lowercaseInput))
77+
{
78+
req = true;
79+
80+
}
81+
else
82+
{
83+
var numPref = getNumericPrefix(lowercaseInput);
84+
85+
if (numPref != null && numPref.length() > 0
86+
&& numPref.matches(@"^(8|11|18).*$"))
87+
{
88+
var num = int.Parse(numPref);
89+
req = checkNum(num);
90+
}
91+
}
92+
93+
return req;
94+
}
95+
96+
/*
97+
* check whether a string beginning with a vowel is an exception and doesn't
98+
* take "an" (e.g. "a one percent change")
99+
*
100+
* @return
101+
*/
102+
103+
private static bool isAnException(string stringa)
104+
{
105+
foreach (var ex in AN_EXCEPTIONS)
106+
{
107+
if (stringa.matches("^" + ex + ".*"))
108+
{
109+
// if (string.equalsIgnoreCase(ex)) {
110+
return true;
111+
}
112+
}
113+
114+
return false;
115+
}
116+
117+
/*
118+
* Returns <code>true</code> if the number starts with 8, 11 or 18 and is
119+
* either less than 100 or greater than 1000, but excluding 180,000 etc.
120+
*/
121+
122+
private static bool checkNum(int num)
123+
{
124+
var needsAn = false;
125+
126+
// eight, eleven, eighty and eighteen
127+
if (num == 11 || num == 18 || num == 8 || (num >= 80 && num < 90))
128+
{
129+
needsAn = true;
130+
131+
}
132+
else if (num > 1000)
133+
{
134+
// num = Math.Round(num / 1000);
135+
num = num/1000;
136+
needsAn = checkNum(num);
137+
}
138+
139+
return needsAn;
140+
}
141+
142+
/*
143+
* Retrieve the numeral prefix of a string.
144+
*/
145+
146+
private static string getNumericPrefix(string stringa)
147+
{
148+
var numeric = new StringBuilder();
149+
150+
if (stringa != null)
151+
{
152+
stringa = stringa.Trim();
153+
154+
if (stringa.length() > 0)
155+
{
156+
157+
var buffer = new StringBuilder(stringa);
158+
var first = buffer.charAt(0);
159+
160+
if (first.isDigit())
161+
{
162+
numeric.append(first);
163+
164+
for (var i = 1; i < buffer.length(); i++)
165+
{
166+
var next = buffer.charAt(i);
167+
168+
if (next.isDigit())
169+
{
170+
numeric.append(next);
171+
172+
// skip commas within numbers
173+
}
174+
else if (next.Equals(','))
175+
{
176+
continue;
177+
178+
}
179+
else
180+
{
181+
break;
182+
}
183+
}
184+
}
185+
}
186+
}
187+
188+
return numeric.length() == 0 ? null : numeric.ToString();
189+
}
190+
191+
192+
/**
193+
* Check to see if a string ends with the indefinite article "a" and it agrees with {@code np}.
194+
* @param text
195+
* @param np
196+
* @return an altered version of {@code text} to use "an" if it agrees with {@code np}, the original string otherwise.
197+
*/
198+
199+
public static string checkEndsWithIndefiniteArticle(string text, string np)
200+
{
201+
202+
var tokens = text.Split(' ');
203+
204+
var lastToken = tokens[tokens.Length - 1];
205+
206+
if (lastToken.equalsIgnoreCase("a") && DeterminerAgrHelper.requiresAn(np))
207+
{
208+
209+
tokens[tokens.Length - 1] = "an";
210+
211+
return stringArrayToString(tokens);
212+
213+
}
214+
215+
return text;
216+
217+
}
218+
219+
// Turns ["a","b","c"] into "a b c"
220+
private static string stringArrayToString(string[] sArray)
221+
{
222+
223+
var buf = new StringBuilder();
224+
225+
for (var i = 0; i < sArray.Length; i++)
226+
{
227+
228+
buf.Append(sArray[i]);
229+
230+
if (i != sArray.Length - 1)
231+
{
232+
233+
buf.Append(" ");
234+
235+
}
236+
237+
}
238+
239+
return buf.ToString();
240+
241+
}
242+
243+
}
244+
}

0 commit comments

Comments
 (0)