Пример #1
0
        /// <summary>
        /// Gets the web lemma.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="node">The node.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static webLemmaTerm GetWebLemma(webLemmaTerm parent, freeGraphNodeBase node, lemmaExpansionOptions options)
        {
            webLemmaTerm term = new webLemmaTerm();

            term.name        = node.name;
            term.nominalForm = node.name;
            term.weight      = node.weight;

            if (options.HasFlag(lemmaExpansionOptions.initialWeightToOne))
            {
                term.weight = 1;
            }

            if (options.HasFlag(lemmaExpansionOptions.initialWeightFromParent))
            {
                term.weight = parent.weight;
            }

            if (options.HasFlag(lemmaExpansionOptions.weightAsSemanticDistanceFromParent))
            {
                Double distanceFactor = node.distance.GetRatio(1);
                term.weight = term.weight * distanceFactor;
            }

            return(term);
        }
Пример #2
0
        internal void AddOrUpdateNode(freeGraphNodeBase node, freeGraphLink link, freeGraphNodeAndLinks links, Boolean typeToMin, lemmaExpansionOptions options)
        {
            if (ContainsNode(node.name, true))
            {
                var nd = GetNode(node.name, true);
                if (options.HasFlag(lemmaExpansionOptions.divideWeightByNumberOfSynonims))
                {
                    nd.weight = nd.weight + (node.weight.GetRatio(links.linkedNodeClones.Count));
                }
                else if (options.HasFlag(lemmaExpansionOptions.weightAsSemanticDistanceThatIsSumOfLinkWeights))
                {
                    nd.weight = nd.weight + node.weight.GetRatio(node.distance);
                    if (nd.distance > 1)
                    {
                        nd.distance = Math.Min(nd.distance, node.distance + link.linkBase.weight);
                    }
                    else
                    {
                        nd.distance = node.distance + link.linkBase.weight;
                    }
                }
                else
                {
                    nd.weight = nd.weight + node.weight;
                }

                if (typeToMin)
                {
                    nd.type = Math.Min(nd.type, node.type);
                }
                else
                {
                    nd.type = Math.Max(nd.type, node.type);
                }
            }
            else
            {
                if (options.HasFlag(lemmaExpansionOptions.divideWeightByNumberOfSynonims))
                {
                    AddNode(node.name, node.weight.GetRatio(links.linkedNodeClones.Count), node.type);
                }
                else if (options.HasFlag(lemmaExpansionOptions.weightAsSemanticDistanceThatIsSumOfLinkWeights))
                {
                    AddNode(node.name, node.weight.GetRatio(node.distance), node.type).distance = node.distance + link.linkBase.weight;
                }
                else
                {
                    AddNode(node.name, node.weight, node.type);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Expands the terms.
        /// </summary>
        /// <param name="lemmas">The lemmas.</param>
        /// <param name="expansionSteps">The expansion steps.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public List <webLemmaTerm> ExpandTerms(IEnumerable <webLemmaTerm> lemmas, Int32 expansionSteps, lemmaExpansionOptions options = lemmaExpansionOptions.initialWeightFromParent | lemmaExpansionOptions.weightAsSemanticDistanceFromParent)
        {
            Dictionary <String, webLemmaTerm> lemmaDictionary  = lemmas.GetLemmaDictionary();
            Dictionary <String, webLemmaTerm> outputDictionary = new Dictionary <string, webLemmaTerm>();

            Dictionary <String, List <webLemmaTerm> > lemmaSet = new Dictionary <string, List <webLemmaTerm> >();

            //aceDictionarySet<String, webLemmaTerm> lemmaSet = new aceDictionarySet<string, webLemmaTerm>();

            List <String> terms = lemmaDictionary.Keys.ToList();

            List <webLemmaTerm> output = new List <webLemmaTerm>();

            foreach (String term in terms)
            {
                lemmaSet.Add(term, new List <webLemmaTerm>());
                lemmaSet[term].Add(lemmaDictionary[term]);
                //lemmaSet[term].Add(lemmaDictionary[term]);
            }

            foreach (String term in terms)
            {
                webLemmaTerm         parent     = lemmaDictionary[term];
                freeGraphQueryResult termResult = GetLinkedNodes(new String[] { term }, expansionSteps, true, false);
                foreach (freeGraphNodeBase node in termResult)
                {
                    webLemmaTerm lem = GetWebLemma(parent, node, options);

                    if (options.HasFlag(lemmaExpansionOptions.divideWeightByNumberOfSynonims))
                    {
                        lem.weight = lem.weight.GetRatio(termResult.Count);
                    }

                    if (!lemmaSet.ContainsKey(node.name))
                    {
                        lemmaSet.Add(node.name, new List <webLemmaTerm>());
                    }
                    lemmaSet[node.name].Add(lem);
                }
            }

            foreach (String key in lemmaSet.Keys)
            {
                Double       weight     = 0;
                webLemmaTerm firstLemma = null;

                foreach (webLemmaTerm lem in lemmaSet[key])
                {
                    weight    += lem.weight;
                    firstLemma = lem;
                }
                if (!outputDictionary.ContainsKey(key))
                {
                    outputDictionary.Add(key, firstLemma);
                }
                outputDictionary[key].weight = weight;
            }
            return(outputDictionary.Values.ToList());
        }
Пример #4
0
        /// <summary>
        /// Returns expanded cloud from given lemma list - only for matched lemmas
        /// </summary>
        /// <param name="lemmas">The lemmas.</param>
        /// <param name="expansionSteps">The expansion steps.</param>
        /// <param name="options">The options.</param>
        /// <param name="typeToMin">todo: describe typeToMin parameter on ExpandTermsToCloud</param>
        /// <returns></returns>
        public lemmaSemanticCloud ExpandTermsToCloud(IEnumerable <String> lemmas, Int32 expansionSteps, Boolean typeToMin = true, lemmaExpansionOptions options = lemmaExpansionOptions.initialWeightFromParent | lemmaExpansionOptions.weightAsSemanticDistanceFromParent)
        {
            lemmaSemanticCloud output = new lemmaSemanticCloud();

            output.name         = name + "_subset_exp" + expansionSteps;
            output.DisableCheck = true;
            StringBuilder sb = new StringBuilder();

            sb.Append("Subset expanded from matched query lemmas [");


            List <String> nextTerms = new List <string>();
            List <String> allTerms  = new List <string>();

            foreach (String t in lemmas)
            {
                if (ContainsNode(t))
                {
                    sb.Append(t + " ");

                    var l = GetNode(t);


                    output.AddNode(l.name, l.weight, 0).distance = 1;
                    nextTerms.Add(t);
                    allTerms.Add(t);
                }
            }

            sb.Append("] using cloud [" + name + "]");
            output.description = sb.ToString();


            Int32 exp_i = 1;

            while (nextTerms.Any())
            {
                List <String> newNextTerms = new List <string>();
                foreach (String t in nextTerms)
                {
                    freeGraphNodeAndLinks links = new freeGraphNodeAndLinks();

                    if (options.HasFlag(lemmaExpansionOptions.weightAsSemanticDistanceFromParent))
                    {
                        links = GetLinks(t, true, false, 1.GetRatio(exp_i), exp_i, true, options.HasFlag(lemmaExpansionOptions.initialWeightFromParent));
                    }
                    else if (options.HasFlag(lemmaExpansionOptions.weightAsSemanticDistanceThatIsSumOfLinkWeights))
                    {
                        var nd = output.GetNode(t, true);
                        links = GetLinks(t, true, false, 1, exp_i, true, options.HasFlag(lemmaExpansionOptions.initialWeightFromParent));
                    }
                    else
                    {
                        links = GetLinks(t, true, false, 1, exp_i, true, options.HasFlag(lemmaExpansionOptions.initialWeightFromParent));
                    }



                    foreach (freeGraphLink link in links)
                    {
                        if (!allTerms.Contains(link.nodeA.name))
                        {
                            newNextTerms.Add(link.nodeA.name);
                            allTerms.Add(link.nodeA.name);
                        }

                        if (link.nodeA.name != t)
                        {
                            output.AddOrUpdateNode(link.nodeA, link, links, typeToMin, options);
                        }

                        if (!allTerms.Contains(link.nodeB.name))
                        {
                            newNextTerms.Add(link.nodeB.name);
                            allTerms.Add(link.nodeB.name);
                        }

                        if (link.nodeB.name != t)
                        {
                            output.AddOrUpdateNode(link.nodeB, link, links, typeToMin, options);
                        }
                    }

                    foreach (freeGraphLink link in links)
                    {
                        if (!output.ContainsLink(link.linkBase.nodeNameA, link.linkBase.nodeNameB))
                        {
                            output.AddLink(link.linkBase.nodeNameA, link.linkBase.nodeNameB, Math.Max(link.linkBase.weight, 1), link.linkBase.type);
                            //var nd = output.GetNode(link.nodeB.name);
                            //nd.weight = nd.weight + (link.nodeB.weight.GetRatio(links.linkedNodeClones.Count));
                            //nd.type = Math.Max(nd.type, link.nodeB.type);
                        }
                        else
                        {
                            var lnk = output.GetLink(link.linkBase.nodeNameA, link.linkBase.nodeNameB);
                            lnk.weight += link.linkBase.weight;
                            if (typeToMin)
                            {
                                lnk.type = Math.Min(link.linkBase.type, lnk.type);
                            }
                            else
                            {
                                lnk.type = Math.Max(link.linkBase.type, lnk.type);
                            }
                        }
                    }
                }
                nextTerms = newNextTerms;
                exp_i++;
                if (exp_i > expansionSteps)
                {
                    break;
                }
            }

            output.DisableCheck = false;
            output.RebuildIndex();


            return(output);
        }