Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further improvements in the existing unit test library made #5

Merged
merged 12 commits into from
Jul 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![Build status](https://ci.appveyor.com/api/projects/status/bjwkwoqdlpcl1jir?svg=true)](https://ci.appveyor.com/project/Arash-Sabet/sharpsimplenlg-olve3)
# SharpSimpleNLG
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.

Expand All @@ -10,7 +11,6 @@ Install-Package SharpSimpleNLG
```

## Version News

Version 1.2.0:
* 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!)

Expand Down
2 changes: 2 additions & 0 deletions SharpSimpleNLG.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpSimpleNLGDotNet45", "S
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpSimpleNLG", "SharpSimpleNLG\SharpSimpleNLG.csproj", "{16F344A8-B688-4D95-8DA6-2E207D47AECB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpSimpleNLGConsoleNetCore", "SharpSimpleNLGConsoleNetCore\SharpSimpleNLGConsoleNetCore.csproj", "{3E619416-20EC-429F-A467-9F7BFE8BACB1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
5 changes: 5 additions & 0 deletions SharpSimpleNLG/SharpSimpleNLG.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<Version>1.2.0</Version>
<PackageLicenseUrl>https://github.com/nickhodge/SharpSimpleNLG/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl />
<RepositoryUrl>https://github.com/nickhodge/SharpSimpleNLG</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.6'">
<OutputPath>bin\$(Configuration)\$(Framework)</OutputPath>
Expand Down
9 changes: 6 additions & 3 deletions SharpSimpleNLGTests/SharpSimpleNLGTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
</Choose>
<ItemGroup>
<Compile Include="helpers\ExtensionHelpersTest.cs" />
<Compile Include="morphology\DeterminerAgrHelper.cs" />
<None Include="morphology\MorphologyProcessor.cs" />
<None Include="morphology\MorphologyRules.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -106,9 +109,9 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SharpSimpleNLGDotNet\SharpSimpleNLGDotNet45.csproj">
<Project>{fd418a41-a46e-4fd7-a6db-ff3ebb4afa29}</Project>
<Name>SharpSimpleNLGDotNet45</Name>
<ProjectReference Include="..\SharpSimpleNLG\SharpSimpleNLG.csproj">
<Project>{16f344a8-b688-4d95-8da6-2e207d47aecb}</Project>
<Name>SharpSimpleNLG</Name>
</ProjectReference>
</ItemGroup>
<Choose>
Expand Down
244 changes: 244 additions & 0 deletions SharpSimpleNLGTests/morphology/DeterminerAgrHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
/*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is "SharpSimpleNLG".
*
* The Initial Developer of the Original Code is Ehud Reiter, Albert Gatt and Dave Westwater.
* Portions created by Ehud Reiter, Albert Gatt and Dave Westwater are Copyright (C) 2010-11 The University of Aberdeen. All Rights Reserved.
*
* Contributor(s): Ehud Reiter, Albert Gatt, Dave Wewstwater, Roman Kutlak, Margaret Mitchell, Saad Mahamood, Nick Hodge
*/

/* Additional Notes:
* - Original Java source is SimpleNLG from 12-Jun-2016 https://github.com/simplenlg/simplenlg
* - This is a port of the Java version to C# with no additional features
* - I have left the "Initial Developer" section to reflect this fact
* - Any questions, comments, feedback on this port can be sent to Nick Hodge <[email protected]>
*/

using System.Text;
using SimpleNLG.Extensions;

namespace SharpNLG.Extensions
{
/**
* This class is used to parse numbers that are passed as figures, to determine
* whether they should take "a" or "an" as determiner.
*
* @author bertugatt
*
*/

public class DeterminerAgrHelper
{
/*
* An array of strings which are exceptions to the rule that "an" comes
* before vowels
*/
private static string[] AN_EXCEPTIONS = {"one", "180", "110"};

/*
* Start of string involving vowels, for use of "an"
*/
private static string AN_AGREEMENT = @"\A(a|e|i|o|u).*";

/*
* Start of string involving numbers, for use of "an" -- courtesy of Chris
* Howell, Agfa healthcare corporation
*/
// private static final string AN_NUMERAL_AGREEMENT =
// "^(((8((\\d+)|(\\d+(\\.|,)\\d+))?).*)|((11|18)(\\d{3,}|\\D)).*)$";

/**
* Check whether this string starts with a number that needs "an" (e.g.
* "an 18% increase")
*
* @param string
* the string
* @return <code>true</code> if this string starts with 11, 18, or 8,
* excluding strings that start with 180 or 110
*/

public static bool requiresAn(string stringa)
{
var req = false;

var lowercaseInput = stringa.toLowerCase();

if (lowercaseInput.matches(AN_AGREEMENT) && !isAnException(lowercaseInput))
{
req = true;

}
else
{
var numPref = getNumericPrefix(lowercaseInput);

if (numPref != null && numPref.length() > 0
&& numPref.matches(@"^(8|11|18).*$"))
{
var num = int.Parse(numPref);
req = checkNum(num);
}
}

return req;
}

/*
* check whether a string beginning with a vowel is an exception and doesn't
* take "an" (e.g. "a one percent change")
*
* @return
*/

private static bool isAnException(string stringa)
{
foreach (var ex in AN_EXCEPTIONS)
{
if (stringa.matches("^" + ex + ".*"))
{
// if (string.equalsIgnoreCase(ex)) {
return true;
}
}

return false;
}

/*
* Returns <code>true</code> if the number starts with 8, 11 or 18 and is
* either less than 100 or greater than 1000, but excluding 180,000 etc.
*/

private static bool checkNum(int num)
{
var needsAn = false;

// eight, eleven, eighty and eighteen
if (num == 11 || num == 18 || num == 8 || (num >= 80 && num < 90))
{
needsAn = true;

}
else if (num > 1000)
{
// num = Math.Round(num / 1000);
num = num/1000;
needsAn = checkNum(num);
}

return needsAn;
}

/*
* Retrieve the numeral prefix of a string.
*/

private static string getNumericPrefix(string stringa)
{
var numeric = new StringBuilder();

if (stringa != null)
{
stringa = stringa.Trim();

if (stringa.length() > 0)
{

var buffer = new StringBuilder(stringa);
var first = buffer.charAt(0);

if (first.isDigit())
{
numeric.append(first);

for (var i = 1; i < buffer.length(); i++)
{
var next = buffer.charAt(i);

if (next.isDigit())
{
numeric.append(next);

// skip commas within numbers
}
else if (next.Equals(','))
{
continue;

}
else
{
break;
}
}
}
}
}

return numeric.length() == 0 ? null : numeric.ToString();
}


/**
* Check to see if a string ends with the indefinite article "a" and it agrees with {@code np}.
* @param text
* @param np
* @return an altered version of {@code text} to use "an" if it agrees with {@code np}, the original string otherwise.
*/

public static string checkEndsWithIndefiniteArticle(string text, string np)
{

var tokens = text.Split(' ');

var lastToken = tokens[tokens.Length - 1];

if (lastToken.equalsIgnoreCase("a") && DeterminerAgrHelper.requiresAn(np))
{

tokens[tokens.Length - 1] = "an";

return stringArrayToString(tokens);

}

return text;

}

// Turns ["a","b","c"] into "a b c"
private static string stringArrayToString(string[] sArray)
{

var buf = new StringBuilder();

for (var i = 0; i < sArray.Length; i++)
{

buf.Append(sArray[i]);

if (i != sArray.Length - 1)
{

buf.Append(" ");

}

}

return buf.ToString();

}

}
}
Loading