Zėŋōfōbìå

5 agosto 2009

ItalianInflector

Filed under: C#, informatica — Tag: — Zeno @ 9:44

Ho esteso l’ottima libreria uNhAddIns per avere l’inflector in italiano.
In questo modo è possibile, dato un sostantivo singolare, ottenerne il plurale.

Segue la patch per la versione 696 di unhaddins:

Index: uNhAddIns/uNhAddIns.Test/Inflector/ItalianInflectorFixture.cs
===================================================================
--- uNhAddIns/uNhAddIns.Test/Inflector/ItalianInflectorFixture.cs	(revision 0)
+++ uNhAddIns/uNhAddIns.Test/Inflector/ItalianInflectorFixture.cs	(revision 0)
@@ -0,0 +1,69 @@
+using NUnit.Framework;
+using uNhAddIns.Inflector;
+
+namespace uNhAddIns.Test.Inflector
+{
+
+    [TestFixture]
+    public class ItalianInflectorFixture : BaseInflectorFixture
+    {
+        public class CustomInflector : ItalianInflector
+        {
+            public CustomInflector()
+            {
+                AddDataDictionary("OrdineCliente", "OrdiniClienti");
+            }
+        }
+
+        public ItalianInflectorFixture()
+        {
+            SingularToPlural.Add("inglese", "inglesi");
+            SingularToPlural.Add("prova", "prove");
+            SingularToPlural.Add("veicolo", "veicoli");
+            SingularToPlural.Add("dispositivo", "dispositivi");
+            SingularToPlural.Add("posizione", "posizioni");            
+            SingularToPlural.Add("attività", "attività");
+            SingularToPlural.Add("intervento", "interventi");
+            SingularToPlural.Add("personale", "personale");
+            SingularToPlural.Add("referente", "referenti");
+            SingularToPlural.Add("città", "città");
+            SingularToPlural.Add("camicia", "camicie");
+            SingularToPlural.Add("uomo", "uomini");
+            SingularToPlural.Add("sacco", "sacchi");
+            SingularToPlural.Add("lago", "laghi");
+            SingularToPlural.Add("amico", "amici");
+
+            SingularToPlural.Add("decreto", "decreti");
+
+            SingularToPlural.Add("ricerca", "ricerche");
+            SingularToPlural.Add("interruttore", "interruttori");
+            SingularToPlural.Add("processo", "processi");
+            SingularToPlural.Add("indirizzo", "indirizzi");
+
+            SingularToPlural.Add("categoria", "categorie");
+            SingularToPlural.Add("domanda", "domande");
+            SingularToPlural.Add("abilità", "abilità");
+            SingularToPlural.Add("agenzia", "agenzie");
+            SingularToPlural.Add("film", "film");
+            SingularToPlural.Add("archivio", "archivi");
+            SingularToPlural.Add("indice", "indici");
+
+            SingularToPlural.Add("automobile", "automobili");
+            
+            ClassNameToTableName.Add("Origini", "Origine");
+            ClassNameToTableName.Add("OrdiniClienti", "OrdineCliente");
+            ClassNameToTableName.Add("Fatture", "Fattura");
+
+            AddClassNameToTableName("Attività", "Attività");
+            AddClassNameToTableName("Telefono", "Telefoni");
+
+            TestInflector = new CustomInflector();
+
+        }
+
+        private void AddClassNameToTableName(string singular, string plural)
+        {
+            ClassNameToTableName.Add(plural, singular);
+        }
+    }
+}
Index: uNhAddIns/uNhAddIns.Test/uNhAddIns.Test.csproj
===================================================================
--- uNhAddIns/uNhAddIns.Test/uNhAddIns.Test.csproj	(revision 696)
+++ uNhAddIns/uNhAddIns.Test/uNhAddIns.Test.csproj	(working copy)
@@ -133,6 +133,7 @@
     
     
     
+    
     
     
     
Index: uNhAddIns/uNhAddIns/Inflector/ItalianInflector.cs
===================================================================
--- uNhAddIns/uNhAddIns/Inflector/ItalianInflector.cs	(revision 0)
+++ uNhAddIns/uNhAddIns/Inflector/ItalianInflector.cs	(revision 0)
@@ -0,0 +1,144 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace uNhAddIns.Inflector
+{
+    /// 
+    /// Inflector for pluralize and singularize Italian nouns. 
+    /// 
+    /// by Fabrizio Cornelli .
+    /// 
+    /// 
+    public class ItalianInflector : AbstractInflector
+	{
+		private class RuleReplacement
+		{
+			public string Rule { get; set; }
+			public string Replacement { get; set; }
+		}
+
+		public ItalianInflector()
+        {
+            #region Fixture Data
+
+            AddPlural("$", "i");
+            AddPlural("a$", "e");
+            AddPlural("o$", "i");
+            AddPlural("e$", "i");
+            AddPlural("i$", "i");
+            AddPlural("um$", "a");
+            AddPlural("io$", "i");
+
+            AddPlural("co$", "chi");
+            AddPlural("ca$", "che");
+            AddPlural("go$", "ghi");
+            AddPlural("ga$", "ghe");
+
+            AddPlural("cio$", "ci");
+            AddPlural("gio$", "gi");
+            AddPlural("ico$", "ici");
+            AddPlural("igo$", "igi");
+
+            AddPlural("cia$", "ce");
+            AddPlural("gia$", "ge");
+
+            AddPlural("icia$", "icie");
+            AddPlural("à$", "à");
+
+            AddSingular("$", "");
+            AddSingular("e$", "a");
+            AddSingular("i$", "o");
+            AddSingular("chi$", "co");
+            AddSingular("che$", "ca");
+            AddSingular("ghi$", "go");
+            AddSingular("ghe$", "ga");
+
+            AddIrregular("problema", "problemi");
+            AddIrregular("dilemma", "dilemmi");
+            AddIrregular("medium", "media");
+            AddIrregular("uomo", "uomini");
+
+            // these are not irregular, but there's no rule
+            // to make singular. It depends by the gender of the word.
+            AddIrregular("inglese", "inglesi");
+            AddIrregular("posizione", "posizioni");
+            AddIrregular("referente", "referenti");
+            AddIrregular("interruttore", "interruttori");
+            AddIrregular("archivio", "archivi");
+            AddIrregular("indice", "indici");
+            AddIrregular("automobile", "automobili");
+
+            AddUncountable("film");
+            AddUncountable("computer");
+            AddUncountable("murales");
+            AddUncountable("attività");
+            AddUncountable("bar");
+            AddUncountable("radio");
+            AddUncountable("moto");
+            AddUncountable("personale");
+
+            #endregion
+        }
+
+		protected override void AddIrregular(string singular, string plural)
+		{
+			base.AddIrregular(singular, plural);
+			base.AddIrregular(Unaccent(singular), Unaccent(plural));
+		}
+
+		protected override void AddUncountable(string word)
+		{
+			base.AddUncountable(word);
+			base.AddUncountable(Unaccent(word));
+		}
+
+		private void AddPluralForEach(string charectersToMatch, string charectersToReplace, char wildChar, string ruleTemplate,
+		                              string replacementTemplate)
+		{
+			IEnumerable e = RulesReplacements(charectersToMatch, charectersToReplace, wildChar, ruleTemplate,
+			                                                   replacementTemplate);
+			foreach (var element in e)
+			{
+				AddPlural(element.Rule, element.Replacement);
+			}
+		}
+
+		private void AddSingularForEach(string charectersToMatch, string charectersToReplace, char wildChar,
+		                                string ruleTemplate, string replacementTemplate)
+		{
+			IEnumerable e = RulesReplacements(charectersToMatch, charectersToReplace, wildChar, ruleTemplate,
+			                                                   replacementTemplate);
+			foreach (var element in e)
+			{
+				AddSingular(element.Rule, element.Replacement);
+			}
+		}
+
+		private static IEnumerable RulesReplacements(string charectersToMatch, string charectersToReplace,
+		                                                              char wildChar, string ruleTemplate,
+		                                                              string replacementTemplate)
+		{
+			if (charectersToMatch.Length != charectersToReplace.Length)
+			{
+				throw new ArgumentException("charectersToMatch and charectersToReplace must have same length", charectersToReplace);
+			}
+			for (int i = 0; i < charectersToMatch.Length; i++)
+			{
+				string rule = ruleTemplate.Replace(wildChar, charectersToMatch[i]);
+				string replacement = replacementTemplate.Replace(wildChar, charectersToReplace[i]);
+				yield return new RuleReplacement {Rule = rule, Replacement = replacement};
+			}
+		}
+
+		#region Overrides of AbstractInflector
+
+		public override string Ordinalize(string number)
+		{
+			return number;
+		}
+
+		#endregion
+	}
+}
Index: uNhAddIns/uNhAddIns/Inflector/ItalianNamingStrategy.cs
===================================================================
--- uNhAddIns/uNhAddIns/Inflector/ItalianNamingStrategy.cs	(revision 0)
+++ uNhAddIns/uNhAddIns/Inflector/ItalianNamingStrategy.cs	(revision 0)
@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace uNhAddIns.Inflector
+{
+	public class ItalianNamingStrategy : InflectorNamingStrategy
+	{
+		public ItalianNamingStrategy() : base(new ItalianInflector()) {}
+	}
+}
Index: uNhAddIns/uNhAddIns/uNhAddIns.csproj
===================================================================
--- uNhAddIns/uNhAddIns/uNhAddIns.csproj	(revision 696)
+++ uNhAddIns/uNhAddIns/uNhAddIns.csproj	(working copy)
@@ -107,6 +107,8 @@
     
     
     
+    
+    
     
     
     

Annunci

Lascia un commento »

Non c'è ancora nessun commento.

RSS feed for comments on this post. TrackBack URI

Rispondi

Effettua il login con uno di questi metodi per inviare il tuo commento:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

Crea un sito o un blog gratuitamente presso WordPress.com.

%d blogger hanno fatto clic su Mi Piace per questo: