2 Replies - 304 Views - Last Post: 05 November 2010 - 08:23 PM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

hard-coding an indexable database that can be referenced in code

Posted 05 November 2010 - 04:27 PM

Uh, hey. I'm trying to program a bit of a grammar machine. Right now I'm stuck on pronouns. I want to be able to do a

string[] allPronouns = Pronouns.GetAll();

string[] allSingularPronouns = Pronouns.Singular.GetAll();

string SingularSubjectPronoun = Pronouns.Singular.Subject



It looks like using "tags" might be the way to go with this, rather than tables... so maybe

Pronouns.GetMatching("Singular", "subject");

I can't imagine what the best way is of doing this though...

Here was my first trial (with out doing anything 'fancy')

        // singular pronouns
        string[] pronounsSubjS = { "I", "you", "he", "she", "it", "we", "they" };
        string[] pronounsObjS = { "me", "you", "him", "her", "it" };
        string[] pronounsSubjOrObjS = { "you", "it" };
        string[] pronounsReflexiveS = { "myself", "yourself", "himself", "herself", "itself" };

        // plural pronouns
        string[] pronounsSubjP = { "we", "you", "they" };
        string[] pronounsObjP = { "us", "you", "them" };

        string[] pronounsSecond = { "you" };  // this could be objective or subjective, and could be sigular or plural? 
        string[] pronounsSecondRefS = { "yourself" };  // this could be objective or subjective, and could be sigular or plural? 
        string[] pronounsSecondRefP = { "yourselves" };  // this could be objective or subjective, and could be sigular or plural? 

        string[] pronounsReflexiveP = { "ourselves", "yourselves", "themselves" };



Then I was like, "hey, why don't I put these guys into a class?" But now I see why, it's a bit odd...


                // this is how I reference it in my code
                Pronoun myPronouns = new Pronoun();
                string[] tehString = myPronouns.singular.GetAll();
                MessageBox.Show(tehString[8]);          // this is just proof that tehString is an array filled with all the pronouns of the singular form (and there are repeats so, meh...)





public class Pronoun
        {
            public Pronoun.Singular singular = new Pronoun.Singular();

            


            public class Singular
            {
                public Pronoun.Singular.Subject subject = new Pronoun.Singular.Subject();
                public Pronoun.Singular.Object obj = new Pronoun.Singular.Object();
                public Pronoun.Singular.Reflexive reflexive = new Pronoun.Singular.Reflexive();



                public string[] GetAll()
                {
                    ArrayList allEntities = new ArrayList();
                    allEntities.Add(subject.GetAll());
                    allEntities.Add(obj.GetAll());
                    allEntities.Add(reflexive.GetAll());

                    int newSize = 0;                    // find size to make the return array be
                    foreach (string[] entity in allEntities)
                    {
                        newSize += entity.GetUpperBound(0) + 1;
                    }
                    string[] all = new string[newSize];


                    int counter = 0;                          // combine the arrays
                    foreach (string[] entity in allEntities)
                    {
                        entity.CopyTo(all, counter);
                        counter += entity.GetUpperBound(0) + 1;
                    }
                    return all;
                }



                public class Subject
                {
                    public string[] First = { "I" };
                    public string[] Second = { "you" };
                    public string[] Third = { "he", "she", "it" };

                    public string[] GetAll()
                    {
                        ArrayList allEntities = new ArrayList();
                        allEntities.Add(First);
                        allEntities.Add(Second);
                        allEntities.Add(Third);

                        int newSize = 0;                    // find size to make the return array be
                        foreach (string[] entity in allEntities)
                        {
                            newSize += entity.GetUpperBound(0) + 1;
                        }

                        string[] all = new string[newSize];


                        int counter = 0;                          // combine the arrays
                        foreach (string[] entity in allEntities)
                        {
                            entity.CopyTo(all, counter);
                            counter += entity.GetUpperBound(0) + 1;
                        }

                        return all;
                    }

                }

                public class Object
                {
                    string[] First = { "me" };
                    string[] Second = { "you" };
                    string[] Third = { "him", "her", "it" };


                    public string[] GetAll()
                    {
                        ArrayList allEntities = new ArrayList();
                        allEntities.Add(First);
                        allEntities.Add(Second);
                        allEntities.Add(Third);

                        int newSize = 0;                    // find size to make the return array be
                        foreach (string[] entity in allEntities)
                        {
                            newSize += entity.GetUpperBound(0) + 1;
                        }
                        string[] all = new string[newSize];


                        int counter = 0;                          // combine the arrays
                        foreach (string[] entity in allEntities)
                        {
                            entity.CopyTo(all, counter);
                            counter += entity.GetUpperBound(0) + 1;
                        }
                        return all;
                    }
                }

                public class Reflexive
                {
                    string[] First = { "myself" };
                    string[] Second = { "yourself" };
                    string[] Third = {"himself", "herself", "itself"};

                    public string[] GetAll()
                    {
                        ArrayList allEntities = new ArrayList();
                        allEntities.Add(First);
                        allEntities.Add(Second);
                        allEntities.Add(Third);

                        int newSize = 0;                    // find size to make the return array be
                        foreach (string[] entity in allEntities)
                        {
                            newSize += entity.GetUpperBound(0) + 1;
                        }
                        string[] all = new string[newSize];


                        int counter = 0;                          // combine the arrays
                        foreach (string[] entity in allEntities)
                        {
                            entity.CopyTo(all, counter);
                            counter += entity.GetUpperBound(0) + 1;
                        }
                        return all;
                    }
                }
            }
        }




Yeah, it gets pretty long, lol. And I haven't even done the plural form pronouns (and this took a while...). So I gotta assume that there's a better, cleaner way to do this, so plz comment on my code and maybe throw some alternatives my way.

Oh, here's a visual reference I've been using about pronouns http://en.wikipedia...._modern_English

Is This A Good Question/Topic? 0
  • +

Replies To: hard-coding an indexable database that can be referenced in code

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: hard-coding an indexable database that can be referenced in code

Posted 05 November 2010 - 05:32 PM

I'd make a class to hold the word and all it's attributes. For your example, an enum with all the categories it belongs to.

e.g.
class Pronoun {
	[Flags]
	public enum PronounGroup {
		None,
		SubjS, ObjS, SubjOrObjS, ReflexiveS,
		SubjP, ObjP, 
		Second, SecondRefS, SecondRefP, ReflexiveP
	};
	
	public PronounGroup group;
	public readonly string word;
	
	public Pronoun(string word) {
		this.word = word;
		this.group = PronounGroup.None;
	}
	public Pronoun(string word, PronounGroup group) {
		this.word = word;
		this.group = group;
	}
}



Now, you want to hold all those values.

class Pronouns {
	public static readonly Singluar = Pronoun.PronounGroup.SubjS | Pronoun.PronounGroup.ObjS;
	private List<Pronoun> words = new List<Pronoun>();
	public Pronouns() {
		Add(Pronoun.PronounGroup.SubjS, new string[] { "I", "you", "he", "she", "it", "we", "they" } );
		Add(Pronoun.PronounGroup.ObjS, new string[] {  "me", "you", "him", "her", "it" } );
		Add(Pronoun.PronounGroup.SubjOrObjS, new string[] { "you", "it" } );
		Add(Pronoun.PronounGroup.ReflexiveS, new string[] { "myself", "yourself", "himself", "herself", "itself" } );
		// ... more 
	}

	private void Add(Pronoun.PronounGroup group, string[] words) {
		foreach(string word in words) { Add(word, group); }
	}
	
	private void Add(string word, Pronoun.PronounGroup group) {
		Pronoun found = words.Find(delegate(Pronoun item) { return word.Equals(item.word); });
		if (found==null) {
			words.Add(new Pronoun(word, group));
		} else {
			found.group = found.group | group;
		}
	}
	
	private string[] GetWordList(List<Pronoun> list) {
		List<string> list = new List<string>();
		foreach(Pronoun item in words) {
			list.Add(item.word);
		}
		return list.ToArray();
	}
	
	public string[] GetAll() { return GetWordList(words); }
	public string[] GetAll(Pronoun.PronounGroup group) { 
		return GetWordList(
			words.FindAll(delegate(Pronoun item) { return (item.group & group) != 0; });
		);
	}
}



You could use the above like so:
Pronouns myPronouns = new Pronouns();
string[] tehString = myPronouns.GetAll(Pronouns.Singular);



I don't currently have access to a Windows box, so can't test this at the moment. Hopefully you get the idea.
Was This Post Helpful? 2
  • +
  • -

#3 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: hard-coding an indexable database that can be referenced in code

Posted 05 November 2010 - 08:23 PM

Thanks =) I'm a bit basic with C# still and I don't totally 'grasp' delegates (I have a doc on my bathroom floor on the subject, so it's just a matter of days =P). But I think I see the gist of it, and I'll probably implement this when my sentence interpretation algorithm matures.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1