Second Sample for C# Programmers

Post general questions about using the WordNet.Net library in your own project

Second Sample for C# Programmers

Postby KawaGeo » Sat Aug 16, 2008 7:50 pm

Again, I had to convert VB sample to C# for Get Coordinate Terms for POS. That was a tough cookie, mate. In spite of the help from SharpDevelop, I had to make many changes in order to make the app work. I had to use false for doMorph parameter instead of true. With doMorph set to true, the app bombs. Also, I had to use generic collection for some lists to remove the casting problems. Here is the modified code below for "dog as noun" as an example. The morph part was removed for now.
Code: Select all
using System;
using System.Collections.Generic;

using Wnlib;

namespace GetCoordinateTermsCon
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello from Get Coordinate Terms for POS\n");

            string dictPath = @"C:\Program Files\WordNet\3.0\dict\";
            WordNetClasses.WN wnc = new WordNetClasses.WN(dictPath);

            List<Search> list = new List<Search>();
                // used to store an array of searches populated by GetCoords
            bool hascoords = false;                 
                // used to determine whether there are any coordinate terms for the word and part of speech
            Wnlib.SearchSet ss = null;             
                // used to retrieve the SearchSet from GetCoords
            List<String> outcoords = new List<string>();
                // used to build a list of words for the output

            string word = "dog";
            string poss = "noun";

            // Get the coordinate terms
            GetCoords(word, poss, ref hascoords, ref ss, ref list);

            // iterate the searches in the list.
            // because we are only collecting coordinate terms
            // we do not take this to any depth beyond the parent
            // and its morphs.
            foreach (Search se in list)
            {
                foreach (SynSet sense in se.senses)
                {
                    ProcessSense(sense, ref outcoords);
                }
            }

            // output the list of coordinate terms
            for (int i = 0; i <= outcoords.Count - 1; i++)
            {
                // txtResult.AppendText(outcoords(i).ToString + Constants.vbCrLf);
                Console.WriteLine(outcoords[i]);
            }

            Console.Read();
        }


        private static void ProcessSense(SynSet sense, ref List<string> outarr)
        {
            foreach (Lexeme lx in sense.words)
            {
                string tmpstr = lx.word.Replace("_", " ");
                if (outarr.IndexOf(tmpstr.ToLower()) == -1)  // not in the list?
                {
                    outarr.Add(tmpstr.ToLower());
                }
            }
        }
       
        static void GetCoords(string wrd, string p, ref bool b, ref SearchSet outss, ref List<Search> list)
        {
            Wnlib.PartOfSpeech pos = PartOfSpeech.of(p);
            SearchSet ss = Wnlib.WNDB.is_defined(wrd, pos);

            AddSearchFor(wrd, pos, ref list, "COORDS");
            b = ss.NonEmpty;
            outss = ss;
        }

        private static void AddSearchFor(string wrd, PartOfSpeech pos, ref List<Search> list, string schtype)
        {
            Wnlib.SearchType st = new SearchType(false, schtype);
            Wnlib.Search se = new Search(wrd, false, pos, st, 0);  // was: true for doMorphs (gm)
            list.Add(se);
        }

    }
}

It runs just like the VB version but the words listed are NOT coordinate terms (sisters) at all. You'll see the difference if you run original WordNet 2.1 console. Use "wn dog -coorn" at command prompt. For more info, use "wn dog -h -coorn".

Obviously, the sample needs to be changed in order to list sisters for each sense. I don't know how to do this yet. Anyone?
Geo Massar
Retired Software Engineer/Teacher
KawaGeo
 
Posts: 12
Joined: Sun Aug 10, 2008 9:52 pm
Location: So. Calif. Mountains, USA

Re: Second Sample for C# Programmers

Postby Faffe » Mon Feb 02, 2009 11:12 pm

Hi
I am developing a windows application in C#.I want to use the word.net dictionary only for synonyms and definations only. Can you please guide me how can I add the database, search for meaning and display the output in a label.
I will be very thankful to you if you reply me before 6th Feb. I am a beginner.
Farheena JawedFaffe
Faffe
 
Posts: 7
Joined: Mon Feb 02, 2009 10:57 pm

Re: Second Sample for C# Programmers

Postby KawaGeo » Sat Feb 07, 2009 2:10 am

I have recompiled my own database for WordNet 3.0 and developed an application in C# to derive some definitions as requested. The program is called WordNet Lemma Finder. The database contains only synset, pos, glosses and sometimes examples. Here is one class I can share with you:
Code: Select all
    class Definition
    {
        private string dataPath = @"D:\Databases\WordNet\wn_database.lst";

        private string _info = String.Empty;
        private string _name = String.Empty;
        private string _pos = String.Empty;
        private string _gloss = String.Empty;

        private List<string> _examples = new List<string>();
        private List<string> _syns = new List<string>(); 

        public Definition(string lemma, long offset)
        {
            _info = getDefinition(offset);

            string[] parts = _info.Split('|');

            string[] synset = parts[0].Split(',');
            foreach (string syn in synset)
            {
                string syn2 = removeNotation(syn);
                if (syn2.ToLower().Equals(lemma))
                    _name = syn2;
                else
                    _syns.Add(syn2);
            }

            _pos = extend(parts[1]);
            _gloss = parts[2];

            if (parts[3].Length == 0) return;

            string[] examples = parts[3].Split(';');
            foreach (string ex in examples)
            {
                _examples.Add(ex);
            }
        }

        private string removeNotation(string syn)
        {
            string retVal = syn;
            int idx = syn.IndexOf('(');
            if (idx > -1)
                retVal = syn.Substring(0, idx);
            return retVal;
        }

        public string Info
        {
            get { return _info; }
        }

        public string Name
        {
            get { return _name; }
        }

        public string Pos
        {
            get { return _pos; }
        }

        public string Gloss
        {
            get { return _gloss; }
        }

        public List<string> Examples
        {
            get { return _examples; }
        }

        public List<string> Syns
        {
            get { return _syns; }
        }

        private string getDefinition(long offset)
        {
            string retDef = String.Empty;
            using (FileStream fs = new FileStream(dataPath, FileMode.Open))
            {
                fs.Seek(offset, SeekOrigin.Begin);
                retDef = getLine(fs);
                fs.Close();
            }
            return retDef;
        }

        private string getLine(FileStream fs)
        {
            int ch;
            StringBuilder sb = new StringBuilder();
            while ((ch = fs.ReadByte()) != '\r' && ch != -1)
            {
                sb.Append((char)ch);
            }
            fs.ReadByte();    // pass the terminator
            string line = sb.ToString();
            return (line.Length > 0 || ch != -1) ? line : null;
        }

        private string extend(string p)
        {
            switch (p)
            {
                case "n": return "noun";
                case "v": return "verb";
                case "a": return "adjective";
                case "r": return "adverb";
            }
            return "unknown";
        }
  }

Is it what you want to have? Just let me know. I could upload two files, wn_lemmaIndexes.lst and wn_database.lst

Best regards,
Geo
Last edited by KawaGeo on Sat Feb 07, 2009 11:24 pm, edited 1 time in total.
Geo Massar
Retired Software Engineer/Teacher
KawaGeo
 
Posts: 12
Joined: Sun Aug 10, 2008 9:52 pm
Location: So. Calif. Mountains, USA

Re: Second Sample for C# Programmers

Postby Faffe » Sat Feb 07, 2009 9:46 pm

hi there
Thanks for your response. Please let me know the pre-reqs to run this program. I have downloaded Wordnet dictionary , what else do I need to do. I hope this sample provided will help in extraction on synonyms of words(nouns) too.
Please upload the the two files "wn_lemmaIndexes.lst and wn_database.lst" that you have mentioned.
Hoping to hear from you soon.
Regards
Farheena Jawed
Faffe
 
Posts: 7
Joined: Mon Feb 02, 2009 10:57 pm

Re: Second Sample for C# Programmers

Postby KawaGeo » Sun Feb 08, 2009 10:43 pm

Here is a zipped package of those mentioned files at:
http://rcpt.yousendit.com/650567861/0bd ... 5ed20e99a3
The package will expire on Feb 15th.

You would need another class to read those indexes in wn_lemmaIndexes.lst. Here is a code:
Code: Select all
    class Indexer
    {
        private static string lemmaIndexesPath = @"D:\Databases\WordNet\wn_lemmaIndexes.lst";
        private static Dictionary<string, List<long>> _lemmas = null;

        public static Dictionary<string, List<long>> Lemmas
        {
            get { return _lemmas; }
        }

        public static bool IsDefined(string lemma)
        {
            return _lemmas.ContainsKey(lemma);
        }

   public static void LoadLemmas()
        {
            _lemmas = new Dictionary<string, List<long>>();

            using (StreamReader sr = new StreamReader(lemmaIndexesPath))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] parts = line.Split(' ');
                    string lemma = parts[0].Replace('_', ' ');
                    _lemmas.Add(lemma, new List<long>());
                    for (int i = 1; i < parts.Length; i++)
                    {
                        long index = Convert.ToInt64(parts[i]);
                        _lemmas[lemma].Add(index);
                    }
                }
                sr.Close();
            }
       }

    }


Those two classes (one posted earlier) are sufficient for any application you want to develop. If you need a sample, just holler and I'll send you a console app. Give me a couple of days to write and verified.
Geo Massar
Retired Software Engineer/Teacher
KawaGeo
 
Posts: 12
Joined: Sun Aug 10, 2008 9:52 pm
Location: So. Calif. Mountains, USA

Re: Second Sample for C# Programmers

Postby Faffe » Thu Feb 12, 2009 8:00 am

Hello Mr.KawaGeo
Thanks a lot for providing me such useful codes. It helped me a lot. You will be glad to know I am successful in incorporating the code into my Windows application. Finding meanings and definition is a part of my Bachelors Final year project.
Thanks a lot again
Regards
Faffe
 
Posts: 7
Joined: Mon Feb 02, 2009 10:57 pm

Re: Second Sample for C# Programmers

Postby Faffe » Thu Mar 05, 2009 9:39 am

hi again!
Can your please let me know how can we use WordNet for natural language processing?
Waiting for your useful help again :D
Thanks
Faffe
 
Posts: 7
Joined: Mon Feb 02, 2009 10:57 pm

Re: Second Sample for C# Programmers

Postby Faffe » Thu Mar 05, 2009 10:11 am

My problem statment is "Develop a Query answering system . It takes query string as input. Search for the revelent answer from the document which is the user is reading. Its a desktop application the document is already saved. Desired output is to display the material.
Regards.
Faffe
 
Posts: 7
Joined: Mon Feb 02, 2009 10:57 pm

Re: Second Sample for C# Programmers

Postby KawaGeo » Fri Mar 06, 2009 8:30 pm

Faffe,

I have made a new thread "New Simplified WordNet Database Available" in the same topic, "Help Using WordNet.Net." Let's start posting there. I have a couple of questions for you.

See you there.
Geo Massar
Retired Software Engineer/Teacher
KawaGeo
 
Posts: 12
Joined: Sun Aug 10, 2008 9:52 pm
Location: So. Calif. Mountains, USA


Return to Help Using WordNet.Net

Who is online

Users browsing this forum: No registered users and 0 guests

cron