Skip to content

Commit

Permalink
Spelling
Browse files Browse the repository at this point in the history
  • Loading branch information
Paymon Khamooshi committed Nov 29, 2024
1 parent e4c37b2 commit 1a9ae3c
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 47 deletions.
76 changes: 76 additions & 0 deletions Olive.Globalization/Html/EnglishSpelling.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Collections.Generic;
using System.Linq;
using HtmlAgilityPack;

namespace Olive.Globalization
{
public class EnglishSpelling
{
string Original;
string[] ToReplace;
readonly Dictionary<string, string> Map;

public EnglishSpelling(string htmlOrText, Dictionary<string, string> map)
{
Original = htmlOrText.TrimOrEmpty();
Map = map;
if (Original.HasValue())
ToReplace = map.Where(v => Original.ContainsWholeWord(v.Key)).Select(v => v.Key).ToArray();
}

public static string ToBritish(string htmlOrText) => new EnglishSpelling(htmlOrText, SpellingMap.AmericanToBritish).Convert();
public static string ToAmerican(string htmlOrText) => new EnglishSpelling(htmlOrText, SpellingMap.BritishToAmerican).Convert();

public string Convert()
{
if (ToReplace.None() || Original.Lacks("<")) return Replace(Original);

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(Original);

Replace(htmlDoc.DocumentNode);

return htmlDoc.DocumentNode.InnerHtml;
}

void Replace(HtmlNode node)
{
node.ChildNodes.Do(Replace);

if (node.NodeType == HtmlNodeType.Text)
{
var to = node.InnerHtml;
if (to.ContainsAny(ToReplace))
node.InnerHtml = Replace(to);
}
}

string Replace(string text)
{
if (text.ContainsAny(ToReplace))
ToReplace.Do(v => text = text.ReplaceWholeWord(v, Map[v]));

return text;
}
}

static class SpellingMap
{
public readonly static Dictionary<string, string> AmericanToBritish, BritishToAmerican;

static SpellingMap()
{
AmericanToBritish = ConvertIze();
AmericanToBritish.Add(Others());
BritishToAmerican = AmericanToBritish.ToDictionary(v => v.Value, v => v.Key);
}

static Dictionary<string, string> ConvertIze() => "accessorize,acclimatize,acclimatized,acclimatizes,acclimatizing,actualize,agonize,agonized,agonizes,agonizing,alphabetize,americanize,amortize,anaesthetize,anglicize,antagonize,antagonized,antagonizes,antagonizing,apologize,apologized,apologizes,apologizing,apostrophize,atomize,authorize,authorized,authorizes,authorizing,baptize,baptized,baptizes,baptizing,bastardize,brutalize,brutalized,cannibalize,canonize,capitalize,capitalized,capitalizes,capitalizing,carbonize,carburize,categorize,categorized,categorizing,cauterize,cauterized,cauterizes,cauterizing,centralize,centralized,characterize,characterized,characterizes,characterizing,civilize,civilized,civilizes,civilizing,colonize,colonized,colonizes,colonizing,commercialize,commercialized,compartmentalize,compartmentalized,compartmentalizing,contextualize,contextualized,criminalize,criminalizing,criticize,criticized,criticizes,criticizing,crystallize,crystallized,crystallizes,crystallizing,customize,customized,decarbonize,decentralize,decentralized,decimalize,dehumanize,delegitimize,demobilize,democratize,democratized,democratizing,demonize,demonized,demonizing,demoralize,demoralized,denationalize,deputize,destabilize,digitize,digitized,diphthongize,dramatize,dramatized,dramatizes,dramatizing,economize,economized,economizes,economizing,empathize,energize,energized,energizes,energizing,epitomize,epitomized,epitomizes,epitomizing,equalize,equalizing,eulogize,eulogized,eulogizes,eulogizing,europeanize,evangelize,evangelized,evangelizes,evangelizing,extemporize,extemporized,extemporizes,extemporizing,externalize,factorize,familiarize,feminize,fertilize,fertilized,fertilizes,fertilizing,fetishize,fictionalize,fictionalized,focalize,formalize,formalized,fossilize,fraternize,fraternized,fraternizes,fraternizing,galvanize,galvanized,galvanizes,galvanizing,generalize,generalized,generalizes,generalizing,glamorize,globalize,globalized,harmonize,harmonized,harmonizes,harmonizing,hospitalize,humanize,humanizes,humanizing,hybridize,hypnotize,hypnotized,idealize,idealized,idealizes,idealizing,idolize,idolized,idolizing,immobilize,immortalize,immunize,incentivize,incentivized,individualize,individualized,industrialize,industrialized,institutionalize,internalize,internalized,internalizing,internationalize,italicize,itemize,jeopardize,jeopardized,jeopardizes,jeopardizing,legalize,legalized,legalizes,legalizing,legitimize,legitimized,legitimizes,legitimizing,liberalize,liberalized,liberalizes,liberalizing,lionize,liquidize,localize,localized,localizes,localizing,magnetize,magnetized,magnetizes,magnetizing,marginalize,marginalized,materialize,materialized,materializes,maximize,maximized,maximizes,maximizing,mechanize,memorialize,memorize,memorized,memorizes,memorizing,mesmerize,mesmerized,mesmerizes,mesmerizing,metabolize,minimize,minimized,minimizes,minimizing,mobilize,mobilized,mobilizes,mobilizing,modernize,modernized,modernizes,modernizing,moisturize,moisturized,moisturizing,monetize,monetized,monetizing,monopolize,monopolized,monopolizes,monopolizing,moralize,moralized,moralizes,moralizing,nasalize,nationalize,naturalize,naturalized,neutralize,neutralized,neutralizes,neutralizing,nominalize,normalize,normalized,normalizing,optimize,optimized,optimizing,organize,organized,organizes,organizing,ostracize,ostracized,ostracizes,ostracizing,oxidize,oxidized,oxidizes,oxidizing,palatalize,particularize,pasteurize,patronize,patronized,patronizes,patronizing,pedestrianize,penalize,penalized,penalizes,penalizing,personalize,personalized,personalizing,philosophize,picturize,plagiarize,plagiarized,plagiarizes,plagiarizing,pluralize,polarize,polarized,polarizing,politicize,politicized,polymerize,popularize,popularized,popularizes,popularizing,prioritize,prioritized,prioritizes,prioritizing,privatize,privatized,privatizing,proselytize,proselytized,proselytizes,proselytizing,publicize,publicized,publicizing,pulverize,pulverized,pulverizes,pulverizing,radicalize,radicalized,rationalize,rationalized,rationalizes,rationalizing,realize,realized,realizes,realizing,reauthorize,reauthorized,recognize,recognized,recognizes,recognizing,regularize,reorganize,reorganized,reorganizes,reorganizing,revitalize,revitalized,revitalizes,revitalizing,revolutionize,revolutionized,revolutionizes,revolutionizing,rhapsodize,romanticize,romanticized,romanticizing,satirize,satirized,satirizing,scandalize,scandalized,scandalizes,scandalizing,schematize,scrutinize,scrutinized,scrutinizes,scrutinizing,secularize,sensationalize,sensationalized,sensitize,sensitized,sentimentalize,sermonize,sexualize,socialize,socialized,socializing,solemnize,soliloquize,specialize,specialized,specializes,specializing,stabilize,stabilized,stabilizes,stabilizing,standardize,standardized,sterilize,sterilized,sterilizes,sterilizing,stigmatize,stigmatized,stigmatizes,stigmatizing,subsidize,subsidized,subsidizes,subsidizing,summarize,summarized,summarizes,summarizing,symbolize,symbolized,symbolizes,symbolizing,sympathize,sympathizing,synchronize,synchronized,synchronizes,synchronizing,systematize,tantalize,tantalized,tantalizes,tantalizing,temporize,temporized,temporizes,temporizing,terrorize,terrorized,terrorizes,terrorizing,theorize,theorized,theorizes,theorizing,tranquillize,tranquillized,tranquillizes,tranquillizing,trivialize,trivialized,tyrannize,tyrannized,tyrannizes,tyrannizing,unauthorized,uncivilized,utilize,utilized,utilizes,utilizing,vaporize,vaporized,vaporizes,vaporizing,verbalize,victimize,victimized,victimizes,victimizing,visualize,visualized,visualizes,visualizing,vocalize,vulgarize"
.Split(',').SelectMany(x => new[] { x, x[0].ToUpper() + x.Substring(1) })
.ToDictionary(x => x, x => x.Replace("z", "s"));

static Dictionary<string, string> Others() => "aging:ageing|aluminum:aluminium|catalog:catalogue|cataloged:catalogued|cataloging:cataloguing|catalogs:catalogues|center:centre|centered:centred|centering:centring|centers:centres|check:cheque|checked:chequed|checking:chequing|checks:cheques|color:colour|colored:coloured|coloring:colouring|colors:colours|curb:kerb|curbed:kerbed|curbing:kerbing|curbs:kerbs|defense:defence|defenses:defences|draft:draught|drafted:draughted|drafting:draughting|drafts:draughts|dreamed:dreamt|enroll:enrol|enrolls:enrols|favorite:favourite|favorites:favourites|fiber:fibre|fibers:fibres|fulfill:fulfil|fulfills:fulfils|gray:grey|grayed:greyed|graying:greying|grays:greys|jewelry:jewellery|labor:labour|labored:laboured|laboring:labouring|labors:labours|license:licence|licensed:licenced|licenses:licences|licensing:licencing|maneuver:manoeuvre|maneuvered:manoeuvred|maneuvering:manoeuvring|maneuvers:manoeuvres|meter:metre|metered:metred|metering:metring|meters:metres|mold:mould|molded:moulded|molding:moulding|molds:moulds|neighbor:neighbour|neighbored:neighboured|neighboring:neighbouring|neighbors:neighbours|offense:offence|offenses:offences|pajamas:pyjamas|plow:plough|plowed:ploughed|plowing:ploughing|plows:ploughs|practice:practise|practiced:practised|practices:practises|practicing:practising|program:programme|programs:programmes|skillful:skilful|spelled:spelt|sulfur:sulphur|sulfured:sulphured|sulfuring:sulphuring|sulfurs:sulphurs|theater:theatre|theaters:theatres|toward:towards|traveler:traveller|travelers:travellers"
.Split('|').Distinct().Select(c => c.Split(':')).ToDictionary(x => x[0], x => x[1]);
}

}
4 changes: 2 additions & 2 deletions Olive.Globalization/Olive.Globalization.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.114</Version>
<Version>2.1.116</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.46" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.71" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

Expand Down
24 changes: 24 additions & 0 deletions Olive/-Extensions/Async/Linq.SpecialCollectionTypes.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ public static Task<IEnumerable<TSource>> Concat<TSource, TOther>(this Task<TSour
public static Task<IEnumerable<TSource>> Concat<TSource, TOther>(this Task<TSource[]> @this, Task<TOther> other) where TOther : IEnumerable<TSource>
=> @this.Get(async x => x.Concat(await other));

public static Task<IEnumerable<TSource>> Prepend<TSource, TOther>(this Task<TSource[]> @this, TOther other) where TOther : IEnumerable<TSource>
=> @this.Get(x => x.Prepend(other));

public static Task<IEnumerable<TSource>> Prepend<TSource, TOther>(this Task<TSource[]> @this, Task<TOther> other) where TOther : IEnumerable<TSource>
=> @this.Get(async x => x.Prepend(await other));

public static Task<IEnumerable<TSource>> Where<TSource>(this Task<TSource[]> @this, Func<TSource, Task<bool>> predicate)
=> @this.Get(x => x.Where(predicate));

Expand Down Expand Up @@ -641,6 +647,12 @@ public static Task<IEnumerable<TSource>> Concat<TSource, TOther>(this Task<IOrde
public static Task<IEnumerable<TSource>> Concat<TSource, TOther>(this Task<IOrderedEnumerable<TSource>> @this, Task<TOther> other) where TOther : IEnumerable<TSource>
=> @this.Get(async x => x.Concat(await other));

public static Task<IEnumerable<TSource>> Prepend<TSource, TOther>(this Task<IOrderedEnumerable<TSource>> @this, TOther other) where TOther : IEnumerable<TSource>
=> @this.Get(x => x.Prepend(other));

public static Task<IEnumerable<TSource>> Prepend<TSource, TOther>(this Task<IOrderedEnumerable<TSource>> @this, Task<TOther> other) where TOther : IEnumerable<TSource>
=> @this.Get(async x => x.Prepend(await other));

public static Task<IEnumerable<TSource>> Where<TSource>(this Task<IOrderedEnumerable<TSource>> @this, Func<TSource, Task<bool>> predicate)
=> @this.Get(x => x.Where(predicate));

Expand Down Expand Up @@ -905,6 +917,12 @@ public static Task<IEnumerable<TSource>> Concat<TSource, TOther>(this Task<List<
public static Task<IEnumerable<TSource>> Concat<TSource, TOther>(this Task<List<TSource>> @this, Task<TOther> other) where TOther : IEnumerable<TSource>
=> @this.Get(async x => x.Concat(await other));

public static Task<IEnumerable<TSource>> Prepend<TSource, TOther>(this Task<List<TSource>> @this, TOther other) where TOther : IEnumerable<TSource>
=> @this.Get(x => x.Prepend(other));

public static Task<IEnumerable<TSource>> Prepend<TSource, TOther>(this Task<List<TSource>> @this, Task<TOther> other) where TOther : IEnumerable<TSource>
=> @this.Get(async x => x.Prepend(await other));

public static Task<IEnumerable<TSource>> Where<TSource>(this Task<List<TSource>> @this, Func<TSource, Task<bool>> predicate)
=> @this.Get(x => x.Where(predicate));

Expand Down Expand Up @@ -1169,6 +1187,12 @@ public static Task<IEnumerable<TSource>> Concat<TSource, TOther>(this Task<IList
public static Task<IEnumerable<TSource>> Concat<TSource, TOther>(this Task<IList<TSource>> @this, Task<TOther> other) where TOther : IEnumerable<TSource>
=> @this.Get(async x => x.Concat(await other));

public static Task<IEnumerable<TSource>> Prepend<TSource, TOther>(this Task<IList<TSource>> @this, TOther other) where TOther : IEnumerable<TSource>
=> @this.Get(x => x.Prepend(other));

public static Task<IEnumerable<TSource>> Prepend<TSource, TOther>(this Task<IList<TSource>> @this, Task<TOther> other) where TOther : IEnumerable<TSource>
=> @this.Get(async x => x.Prepend(await other));

public static Task<IEnumerable<TSource>> Where<TSource>(this Task<IList<TSource>> @this, Func<TSource, Task<bool>> predicate)
=> @this.Get(x => x.Where(predicate));

Expand Down
6 changes: 6 additions & 0 deletions Olive/-Extensions/Async/Linq.SpecialCollectionTypes.Async.tt
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ namespace Olive
public static Task<IEnumerable<TSource>> Concat<TSource, TOther>(this Task<<#=t#>> @this, Task<TOther> other) where TOther : IEnumerable<TSource>
=> @this.Get(async x => x.Concat(await other));

public static Task<IEnumerable<TSource>> Prepend<TSource, TOther>(this Task<<#=t#>> @this, TOther other) where TOther : IEnumerable<TSource>
=> @this.Get(x => x.Prepend(other));

public static Task<IEnumerable<TSource>> Prepend<TSource, TOther>(this Task<<#=t#>> @this, Task<TOther> other) where TOther : IEnumerable<TSource>
=> @this.Get(async x => x.Prepend(await other));

public static Task<IEnumerable<TSource>> Where<TSource>(this Task<<#=t#>> @this, Func<TSource, Task<bool>> predicate)
=> @this.Get(x => x.Where(predicate));

Expand Down
4 changes: 4 additions & 0 deletions Olive/-Extensions/Async/Linq.Task.Of.IEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public static Task<IEnumerable<TSource>> Concat<TSource>(
this Task<IEnumerable<TSource>> @this, TSource item)
=> @this.Get(x => x.OrEmpty().Concat(item));

public static Task<IEnumerable<TSource>> Prepend<TSource>(
this Task<IEnumerable<TSource>> @this, TSource item)
=> @this.Get(x => x.OrEmpty().Prepend(item));

public static Task<IEnumerable<TSource>> Distinct<TSource>(
this Task<IEnumerable<TSource>> @this) => @this.Get(x => x.OrEmpty().Distinct());

Expand Down
2 changes: 1 addition & 1 deletion Olive/-Extensions/Boolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ partial class OliveExtensions
/// <param name="yes">The output string if this value is True. the default value is Yes.</param>
/// <param name="no">The output string if this value is False.. the default value is No.</param>
public static string ToYesNoString(this bool? value, string yes = "Yes", string no = "No") =>
value.HasValue ? ToYesNoString(value.Value) : string.Empty;
value.HasValue ? ToYesNoString(value.Value, yes, no) : string.Empty;

/// <summary>
/// Compares two Boolean object and returns 0 if both are equal.
Expand Down
4 changes: 4 additions & 0 deletions Olive/-Extensions/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;

namespace Olive
{
Expand Down Expand Up @@ -75,6 +76,9 @@ public static DateTime GetLast(this DateTime @this, DayOfWeek dayOfWeek, bool sk
}
}

public static async Task< string> ToString(this Task<DateTime> @this, string format)
=> (await @this).ToString(format);

/// <summary>
/// Determines whether this value is before than TimeSpan parameter
/// </summary>
Expand Down
Loading

0 comments on commit 1a9ae3c

Please sign in to comment.