Ejemplo n.º 1
0
        public static Search GetSearch(string w, bool doMorphs, PartOfSpeech p, WordNetContext context)
        {
            var search = new Search {
                word = w, pos = p, _context = context
            };

            //if (p != null)
            search.do_search(doMorphs);

            return(search);
        }
Ejemplo n.º 2
0
        public static IEnumerable<string> GetMorphs(string str, PartOfSpeech pos, WordNetContext context)
        {
            if (pos == null)
            {
                var poses = (new string[] { "n", "v", "a" }).Select(s => PartOfSpeech.of(s));
                foreach(var p in poses)
                {
                    foreach (var s in GetMorphs(str, p, context))
                    {
                        yield return s;
                    }
                }
                yield break;
            }

            if (pos.clss == "SATELLITE")
                pos = PartOfSpeech.of("adj");

            var parts = str.Split(' ');
            int cnt = parts.Length;
            string tmp = null;

            /* first try exception list */
            var e = GetExceptions(str, pos, context);
            if (e.Any())
            {
                foreach(var s in e)
                    yield return s;
            }
            else if (pos.name == "verb" && cnt > 1 && HasPreposition(parts))
            {
                yield return MorphPreposition(str, parts, pos, context);
                yield break;
            }
            else
            /* then try simply morph on original string */
            if (pos.name != "verb" && MorphWord(str, pos, morph => tmp = morph, context))
            {
                yield return tmp;
            }
            else
            {
                bool isChanged = false;
                for (int i = 0; i < parts.Length; i++)
                {
                    string word = parts[i];
                    if (word.Contains('-'))
                    {
                        bool isSubChanged = false;
                        var subs = word.Split('-');
                        for (int j = 0; j < subs.Length; j++)
                        {
                            isSubChanged |= MorphWord(subs[j], pos, morph => subs[j] = morph, context);
                        }

                        if (isSubChanged)
                        {
                            parts[i] = string.Join("-", subs);
                            isChanged = true;
                        }
                    }
                    else
                    {
                        isChanged |= MorphWord(word, pos, morph => parts[i] = morph, context);
                    }
                }

                if (isChanged)
                {
                    var s = string.Join(" ", parts);
                    if (IsDefinded(s, pos, context))
                        yield return s;
                }
            }
        }
Ejemplo n.º 3
0
        // extracted from Exceptions class 
        private static IEnumerable<string> GetExceptions(string word, PartOfSpeech pos, WordNetContext context)
        {
            if (_exceptCache == null)
            {
                var watch = new System.Diagnostics.Stopwatch();
                watch.Start();

                _exceptCache = context.Excepts
                    .Include(e => e.Lemma)
                    .GroupBy(e => e.Value, e => e.Lemma)
                    .ToDictionary(grp => grp.Key, grp => grp.Select(l => new DictException { Exception = l.Value, Poses = l.Poses }).ToList());

                watch.Stop();
                Console.WriteLine($"GetExceptions time: {watch.ElapsedMilliseconds} ms");
            }

            List<DictException> excepts;
            if (_exceptCache.TryGetValue(word, out excepts))
            { 
                return excepts.Where(e => e.Poses.Contains(pos.symbol))
                    .Select(e => e.Exception);
            }
            
            return Enumerable.Empty<string>();
        }
Ejemplo n.º 4
0
 private static bool IsDefinded(string word, PartOfSpeech pos, WordNetContext context)
 {
     return context.IsDefinded(word, pos);
 }
Ejemplo n.º 5
0
        private static string MorphPreposition(string str, string[] parts, PartOfSpeech pos, WordNetContext context)
        {
            string retval;

            /* Assume that the verb is the first word in the phrase.  Strip it
			   off, check for validity, then try various morphs with the
			   rest of the phrase tacked on, trying to find a match. */

            string rest = str.Substring(parts.First().Length);
            string end = null;
            if (parts.Length > 2)
            {   // more than 2 words
                MorphWord(parts.Last(), pos, morph => end = rest.Substring(0, parts.Last().Length) + morph, context);
            }

            string word = parts.First();
            if (!word.All(c => char.IsLetterOrDigit(c)))
                return null;

            /* First try to find the verb in the exception list */
            var e = GetExceptions(word, PartOfSpeech.of("verb"), context);
            foreach (string excWord in e)
            {
                retval = excWord + rest;
                if (IsDefinded(retval, PartOfSpeech.of("verb"), context))
                    return retval;

                if (end != null)
                {
                    retval = excWord + end;
                    if (IsDefinded(retval, PartOfSpeech.of("verb"), context))
                        return retval;
                }
            }

            var psufx = sufx[PartOfSpeech.of("verb").ident];
            for (int i = 0; i < psufx.Length; i++)
            {
                string suffix = psufx[i];
                if (word.EndsWith(suffix)) // ending is different
                {
                    string excWord = word.Substring(0, word.Length - suffix.Length) + addr[PartOfSpeech.of("verb").ident][i];

                    retval = excWord + rest;
                    if (IsDefinded(retval, PartOfSpeech.of("verb"), context))
                        return retval;

                    if (end != null)
                    {
                        retval = excWord + end;
                        if (IsDefinded(retval, PartOfSpeech.of("verb"), context))
                            return retval;
                    }
                }
            }

            if (end != null)
            {
                return word + end;
            }

            return null;
        }
Ejemplo n.º 6
0
        private static bool MorphWord(string word, PartOfSpeech pos, Action<string> action, WordNetContext context)
        {
            if (word == null)
                return false;

            var morph = GetExceptions(word, pos, context).FirstOrDefault();
            if (morph != null)
            {
                action(morph);
                return true;
            }

            if (pos.name == "adverb")
                return false;

            string tmpbuf = "";
            string end = "";
            if (pos.name == "noun")
            {
                if (word.EndsWith("ful"))
                {
                    tmpbuf = word.Substring(0, word.Length - 3);
                    end = "ful";
                }
                else if (word.EndsWith("ss") || word.Length <= 2)
                    return false;
            }

            if (tmpbuf == "")
                tmpbuf = word;

            var psufx = sufx[pos.ident];
            for (int i = 0; i < psufx.Length; i++)
            {
                string suffix = psufx[i];
                if (tmpbuf.EndsWith(suffix))
                {
                    string retval = tmpbuf.Substring(0, tmpbuf.Length - suffix.Length) + addr[pos.ident][i];
                    if (IsDefinded(retval, pos, context))
                    {
                        action(retval + end);
                        return true;
                    }
                }
            }

            return false;
        }