コード例 #1
0
        private void btnSimilarity_Click(object sender, EventArgs e)
        {
            var output = new StringBuilder();

            string[] sentences = SplitSentences(_txtIn.Text);

            foreach (string sentence in sentences)
            {
                string[] tokens = TokenizeSentence(sentence);
                string[] tags   = PosTagTokens(tokens);

                string posTaggedSentence = string.Empty;

                for (int currentTag = 0; currentTag < tags.Length; currentTag++)
                {
                    posTaggedSentence += tokens[currentTag] + @"/" + tags[currentTag] + " ";
                }

                output.Append(posTaggedSentence);
                output.Append("\r\n");
                output.Append(SimilarityModel.SimilarityMain(_modelPath + "coref\\sim", posTaggedSentence));
                output.Append("\r\n\r\n");
            }

            _txtOut.Text = output.ToString();
        }
コード例 #2
0
 /// <summary>
 /// Initializes the resolvers used by this linker.
 /// </summary>
 /// <param name="mode">
 /// The mode in which this linker is being used.
 /// </param>
 /// <param name="fixedNonReferentialProbability">
 /// </param>
 protected internal virtual void InitializeResolvers(LinkerMode mode, double fixedNonReferentialProbability)
 {
     if (mode == LinkerMode.Train)
     {
         MentionFinder.PrenominalNamedEntitiesCollection = false;
         MentionFinder.CoordinatedNounPhrasesCollection  = false;
     }
     SingularPronounIndex = 0;
     if (LinkerMode.Test == mode || LinkerMode.Eval == mode)
     {
         if (fixedNonReferentialProbability < 0)
         {
             Resolvers = new MaximumEntropyResolver[] { new SingularPronounResolver(CoreferenceProjectName, ResolverMode.Test), new ProperNounResolver(CoreferenceProjectName, ResolverMode.Test), new DefiniteNounResolver(CoreferenceProjectName, ResolverMode.Test), new IsAResolver(CoreferenceProjectName, ResolverMode.Test), new PluralPronounResolver(CoreferenceProjectName, ResolverMode.Test), new PluralNounResolver(CoreferenceProjectName, ResolverMode.Test), new CommonNounResolver(CoreferenceProjectName, ResolverMode.Test), new SpeechPronounResolver(CoreferenceProjectName, ResolverMode.Test) };
         }
         else
         {
             INonReferentialResolver nrr = new FixedNonReferentialResolver(fixedNonReferentialProbability);
             Resolvers = new MaximumEntropyResolver[] { new SingularPronounResolver(CoreferenceProjectName, ResolverMode.Test, nrr), new ProperNounResolver(CoreferenceProjectName, ResolverMode.Test, nrr), new DefiniteNounResolver(CoreferenceProjectName, ResolverMode.Test, nrr), new IsAResolver(CoreferenceProjectName, ResolverMode.Test, nrr), new PluralPronounResolver(CoreferenceProjectName, ResolverMode.Test, nrr), new PluralNounResolver(CoreferenceProjectName, ResolverMode.Test, nrr), new CommonNounResolver(CoreferenceProjectName, ResolverMode.Test, nrr), new SpeechPronounResolver(CoreferenceProjectName, ResolverMode.Test, nrr) };
         }
         if (LinkerMode.Eval == mode)
         {
             //String[] names = {"Pronoun", "Proper", "Def-NP", "Is-a", "Plural Pronoun"};
             //eval = new Evaluation(names);
         }
         MaximumEntropyResolver.SimilarityModel = SimilarityModel.TestModel(CoreferenceProjectName + "/sim");
     }
     else if (LinkerMode.Train == mode)
     {
         Resolvers    = new AbstractResolver[9];
         Resolvers[0] = new SingularPronounResolver(CoreferenceProjectName, ResolverMode.Train);
         Resolvers[1] = new ProperNounResolver(CoreferenceProjectName, ResolverMode.Train);
         Resolvers[2] = new DefiniteNounResolver(CoreferenceProjectName, ResolverMode.Train);
         Resolvers[3] = new IsAResolver(CoreferenceProjectName, ResolverMode.Train);
         Resolvers[4] = new PluralPronounResolver(CoreferenceProjectName, ResolverMode.Train);
         Resolvers[5] = new PluralNounResolver(CoreferenceProjectName, ResolverMode.Train);
         Resolvers[6] = new CommonNounResolver(CoreferenceProjectName, ResolverMode.Train);
         Resolvers[7] = new SpeechPronounResolver(CoreferenceProjectName, ResolverMode.Train);
         Resolvers[8] = new PerfectResolver();
     }
     else
     {
         System.Console.Error.WriteLine("DefaultLinker: Invalid Mode");
     }
 }
コード例 #3
0
 private string GetSemanticCompatibilityFeature(Mention.MentionContext entityContext, DiscourseEntity discourseEntity)
 {
     if (SimilarityModel != null)
     {
         double best = 0;
         foreach (Mention.MentionContext checkEntityContext in discourseEntity.Mentions)
         {
             double sim = SimilarityModel.AreCompatible(entityContext, checkEntityContext);
             if (DebugOn)
             {
                 Console.Error.WriteLine("MaxentResolver.GetSemanticCompatibilityFeature: sem-compat " + sim + " " + entityContext.ToText() + " " + checkEntityContext.ToText());
             }
             if (sim > best)
             {
                 best = sim;
             }
         }
         if (best > MinimumSimilarityProbability)
         {
             return(SimilarityCompatible);
         }
         else if (best > (1 - MinimumSimilarityProbability))
         {
             return(SimilarityUnknown);
         }
         else
         {
             return(SimilarityIncompatible);
         }
     }
     else
     {
         Console.Error.WriteLine("MaxentResolver: Uninitialized Semantic Model");
         return(SimilarityUnknown);
     }
 }
コード例 #4
0
        public static List <Dictionary <string, double> > CalcSimilarity(string[] targets, string[] candidates, SimilarityModel model = SimilarityModel.Spacy, DistanceMetric metric = DistanceMetric.Euclidean)
        {
            List <Dictionary <string, double> > results = new List <Dictionary <string, double> >();

            string uri;

            switch (model)
            {
            case SimilarityModel.Spacy:
                for (int h = 0; h < targets.Length; h++)
                {
                    results[h] = new Dictionary <string, double>();
                    for (int i = 0; i < candidates.Length; i++)
                    {
                        uri = baseURL + @"/callspacy?string1=""" + targets[h] + "\"&string2=\"" + candidates[i] + "\"";
                        results[h].Add(candidates[i], Double.Parse(GetRequest(uri)));
                    }
                }
                break;

            case SimilarityModel.USEDAN:
                uri = baseURL + @"/callUSEDAN?data=""" + targets[0];
                for (int i = 1; i < targets.Length; i++)
                {
                    uri += "," + targets[i];
                }
                for (int i = 0; i < candidates.Length; i++)
                {
                    uri += "," + candidates[i];
                }
                uri += "\"";

                string requestResult = GetRequest(uri);

                for (int i = 0; i < targets.Length; i++)
                {
                    List <double> scores = CalcScore(requestResult, i, targets.Length, metric);
                    for (int j = 0; j < scores.Count; j++)
                    {
                        results[i].Add(candidates[j], scores[j]);
                    }
                }

                break;

            case SimilarityModel.USETrans:
                uri = baseURL + @"/callUSETrans?data=""" + targets[0];
                for (int i = 1; i < targets.Length; i++)
                {
                    uri += "," + targets[i];
                }
                for (int i = 0; i < candidates.Length; i++)
                {
                    uri += "," + candidates[i];
                }
                uri += "\"";

                requestResult = GetRequest(uri);

                for (int i = 0; i < targets.Length; i++)
                {
                    List <double> scores = CalcScore(requestResult, i, targets.Length, metric);
                    results.Add(new Dictionary <string, double>());
                    for (int j = 0; j < scores.Count; j++)
                    {
                        results[i].Add(candidates[j], scores[j]);
                    }
                }

                break;
            }

            return(results);
        }
コード例 #5
0
        private static string baseURL = "http://*****:*****@"/callspacy?string1=""" + target + "\"&string2=\"" + candidates[i] + "\"";
                    results.Add(candidates[i], Double.Parse(GetRequest(uri)));
                }
                break;

            case SimilarityModel.USEDAN:
                uri = baseURL + @"/callUSEDAN?data=""" + target;
                for (int i = 0; i < candidates.Length; i++)
                {
                    uri += "," + candidates[i];
                }
                uri += "\"";
                List <double> scores = CalcScore(GetRequest(uri), 0, 1, metric);
                for (int i = 0; i < scores.Count; i++)
                {
                    results.Add(candidates[i], scores[i]);
                }
                break;

            case SimilarityModel.USETrans:
                uri = baseURL + @"/callUSETrans?data=""" + target;
                for (int i = 0; i < candidates.Length; i++)
                {
                    uri += "," + candidates[i];
                }
                uri   += "\"";
                scores = CalcScore(GetRequest(uri), 0, 1, metric);
                for (int i = 0; i < scores.Count; i++)
                {
                    results.Add(candidates[i], scores[i]);
                }
                break;
            }

            return(results);
        }