Exemple #1
0
        public void OverviewFor(string t, string p, ref bool b, ref SearchSet obj, List <Search> list)
        {
            SearchSet ss = null;

            if (string.IsNullOrWhiteSpace(p))
            {
                OverviewFor(t, PartsOfSpeech.Adjective.ToString(), ref b, ref obj, list);
                OverviewFor(t, PartsOfSpeech.Adverb.ToString(), ref b, ref obj, list);
                OverviewFor(t, PartsOfSpeech.Noun.ToString(), ref b, ref obj, list);
                OverviewFor(t, PartsOfSpeech.Verb.ToString(), ref b, ref obj, list);

                ss = obj;
            }
            else
            {
                PartOfSpeech pos = PartOfSpeech.Of(p);

                if (!string.IsNullOrWhiteSpace(pos?.Key))
                {
                    ss = netData.Is_defined(t, pos);
                    Morph ms = new Morph(t, pos, netData);
                    hasmatch = AddSearchFor(t, pos, list);

                    //TODO: if this isn't reset then morphs aren't checked on subsequent searches - check for side effects of resetting this manually
                    if (!hasmatch)
                    {
                        string m;
                        // loop through morphs (if there are any)
                        while ((m = ms.Next()) != null)
                        {
                            if (m != t)
                            {
                                ss += netData.Is_defined(m, pos);
                                AddSearchFor(m, pos, list);
                            }
                        }
                    }
                }
            }

            b   = ss?.NonEmpty ?? false;
            obj = ss;
        }
Exemple #2
0
        /// <summary>Returns a list of Synonyms for a given word</summary>
        /// <param name="word">the word</param>
        /// <param name="pos">The Part of speech of a word</param>
        /// <param name="includeMorphs">include morphology? (fuzzy matching)</param>
        /// <returns>An array of strings containing the synonyms found</returns>
        /// <remarks>
        /// Note that my usage of 'Synonyms' here is not the same as hypernyms as defined by
        /// WordNet. Synonyms in this sense are merely words in the same SynSet as the given
        /// word. Hypernyms are found by tracing the pointers in a given synset.
        /// </remarks>
        public string[] FindSynonyms(string word, PartsOfSpeech pos, bool includeMorphs)
        {
            // get an index to a synset collection
            word = word.ToLower();
            Index index = new Index(word, PartOfSpeech.Of(pos), netData);

            // none found?
            if (index == null)
            {
                if (!includeMorphs)
                {
                    return(null);
                }

                // check morphs
                Morph  morphs = new Morph(word, PartOfSpeech.Of(pos), netData);
                string morph;
                while ((morph = morphs.Next()) != null)
                {
                    index = new Index(morph, PartOfSpeech.Of(pos), netData);
                    if (index != null)
                    {
                        break;
                    }
                }
            }

            // still none found?
            if (index == null)
            {
                return(null);
            }

            // at this point we will always have a valid index
            return(LookupSynonyms(index));
        }
Exemple #3
0
        private WordInfo LookupWordMorphs(string word)
        {
            // OVERVIEW: This functions only gets called when the word was not found with
            //           an exact match. So, enumerate all the parts of speech, then enumerate
            //           all of the word's morphs in each category. Perform a lookup on each
            //           morph and save the morph/strength/part-of-speech data sets. Finally,
            //           loop over all the data sets and then pick the strongest one.

            ArrayList wordinfos = new ArrayList();

            // for each part of speech...
            for (int i = 0; i < enums.Length; i++)
            {
                // get a valid part of speech
                PartsOfSpeech pos = enums[i];
                if (pos == PartsOfSpeech.None)
                {
                    continue;
                }

                // generate morph list
                Morph  morphs = new Morph(word, PartOfSpeech.Of(pos), netData);
                string morph;
                while ((morph = morphs.Next()) != null)
                {
                    // get an index to a synset collection
                    Index index = new Index(morph, PartOfSpeech.Of(pos), netData);

                    // none found?
                    if (index == null)
                    {
                        continue;
                    }

                    // save the wordinfo
                    WordInfo wordinfo = GetMorphInfo(wordinfos, morph);
                    wordinfo.senseCounts[i] = index.sense_cnt;
                }
            }

            // search the wordinfo list for the best match
            WordInfo bestWordInfo = new WordInfo();
            int      maxStrength  = 0;

            foreach (WordInfo wordinfo in wordinfos)
            {
                // for each part of speech...
                int maxSenseCount = 0;
                int strength      = 0;
                for (int i = 0; i < enums.Length; i++)
                {
                    // get a valid part of speech
                    PartsOfSpeech pos = enums[i];
                    if (pos == PartsOfSpeech.None)
                    {
                        continue;
                    }

                    // determine part of speech and strength
                    strength += wordinfo.senseCounts[i];
                    if (wordinfo.senseCounts[i] > maxSenseCount)
                    {
                        maxSenseCount         = wordinfo.senseCounts[i];
                        wordinfo.partOfSpeech = pos;
                    }
                }

                // best match?
                if (strength > maxStrength)
                {
                    maxStrength  = strength;
                    bestWordInfo = wordinfo;
                }
            }

            return(bestWordInfo);
        }