Exemple #1
0
        static void Main(string[] args)
        {
            Word2VecBinaryReader binfile = new Word2VecBinaryReader();
            Vocabulary           v       = binfile.Read("f:\\googlenews.bin");

            Console.WriteLine($"Length {v.Words.Length}");
            Console.WriteLine($"Vector Dimensions: {v.VectorDimensionsCount}");

            //var closest = v.Distance("Trump", 10);
            //foreach(var w in closest)
            //{
            //    Console.WriteLine($"{w.Representation.WordOrNull}\t{w.DistanceValue}");
            //}

            //var ro = v.GetRepresentationFor("Obama");
            //var rt = v.GetRepresentationFor("Trump");
            //foreach (var ca in v.Distance(v["Obama"].Substract(v["President"]).Add(v["Money"]), 10))
            //{
            //    Console.WriteLine($"{ca.Representation.WordOrNull}");
            //}


            string[] teststr = new string[2];
            teststr[0] = @"Democrats scrambled to regroup on Wednesday after a disappointing special election defeat in Georgia, with lawmakers, activists and labor leaders speaking out in public and private to demand a more forceful economic message heading into the 2018 elections. Among Democrats in Washington, the setback in Georgia revived or deepened a host of existing grievances about the party, accentuating tensions between moderate lawmakers and liberal activists and prompting some Democrats to question the leadership and political strategy of Nancy Pelosi, the House minority leader. A small group of Democrats who have been critical of Ms. Pelosi in the past again pressed her to step down on Wednesday. And in a private meeting of Democratic lawmakers, Representative Tony Cárdenas of California, Ms. Pelosi’s home state, suggested the party should have a more open conversation about her effect on its political fortunes.";
            teststr[1] = @"democrats scrambled to regroup on wednesday after a disappointing special election defeat in georgia with lawmakers activists and labor leaders speaking out in public and private to demand a more forceful economic message heading into the 2018 elections among democrats in washington the setback in georgia revived or deepened a host of existing grievances about the party accentuating tensions between moderate lawmakers and liberal activists and prompting some democrats to question the leadership and political strategy of nancy pelosi the house minority leader a small group of democrats who have been critical of ms pelosi in the past again pressed her to step down on wednesday and in a private meeting of democratic lawmakers representative tony cárdenas of california ms pelosis home state suggested the party should have a more open conversation about her effect on its political fortunes";
            //teststr[0] = "Obama's proposes 40 million in spending";
            //teststr[1] = "Republicans cut global warming from Obama budget";
            //var teststr = "Obama's new budget proposes 40 million in spending for swag";
            //var teststr2 = "Republicans cut global warming from Obama budget";

            foreach (string str in teststr)
            {
                Console.WriteLine(str);
                Representation sumk = v.GetSummRepresentationOrNullForPhrase(str);
                foreach (var ca in v.Distance(sumk, 10))
                {
                    Console.WriteLine($"{ca.Representation.WordOrNull}");
                }
            }



            //var analogy = v.Analogy("Obama", "budget", "Bush", 10);
            //foreach(var a in analogy)
            //{
            //    Console.WriteLine($"{a.Representation.WordOrNull}");
            //}

            //Word2vec.Tools.Representation r = new Representation()

            //Word2vec.Tools.Vocabulary v = new Vocabulary()
        }
Exemple #2
0
        public static Vocabulary CreateVocabulary()
        {
            Console.WriteLine("Reading the model...");

            //Set your w2v bin file path here:
            var path1 = @"C:\Users\wainw\Downloads\GoogleNews-vectors-negative300.bin";
            //var path2 = @"C:\Users\wainw\Downloads\wiki-news-300d-1M.vec";
            var vocabulary = new Word2VecBinaryReader().Read(path1);

            //For w2v text sampling file use:
            //var vocabulary = new Word2VecTextReader().Read(path);

            Console.WriteLine("vectors file: " + path1);
            Console.WriteLine("vocabulary size: " + vocabulary.Words.Length);
            Console.WriteLine("w2v vector dimensions count: " + vocabulary.VectorDimensionsCount);

            return(vocabulary);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            string boy   = "boy";
            string girl  = "girl";
            string woman = "woman";

            Console.WriteLine("Reading the model...");

            //Set your w2v bin file path here:
            var path       = @"D:\GoogleNews-vectors-negative300.bin";
            var vocabulary = new Word2VecBinaryReader().Read(path);

            //For w2v text sampling file use:
            //var vocabulary = new Word2VecTextReader().Read(path);

            Console.WriteLine("vectors file: " + path);
            Console.WriteLine("vocabulary size: " + vocabulary.Words.Length);
            Console.WriteLine("w2v vector dimensions count: " + vocabulary.VectorDimensionsCount);

            Console.WriteLine();

            int count = 7;

            #region distance

            Console.WriteLine("top " + count + " closest to \"" + boy + "\" words:");
            var closest = vocabulary.Distance(boy, count);

            // Is simmilar to:
            // var closest = vocabulary[boy].GetClosestFrom(vocabulary.Words.Where(w => w != vocabulary[boy]), count);

            foreach (var neightboor in closest)
            {
                Console.WriteLine(neightboor.Representation.WordOrNull + "\t\t" + neightboor.DistanceValue);
            }
            #endregion

            Console.WriteLine();

            #region analogy
            Console.WriteLine("\"" + girl + "\" relates to \"" + boy + "\" as \"" + woman + "\" relates to ...");
            var analogies = vocabulary.Analogy(girl, boy, woman, count);
            foreach (var neightboor in analogies)
            {
                Console.WriteLine(neightboor.Representation.WordOrNull + "\t\t" + neightboor.DistanceValue);
            }
            #endregion

            Console.WriteLine();

            #region addition
            Console.WriteLine("\"" + girl + "\" + \"" + boy + "\" = ...");
            var additionRepresentation = vocabulary[girl].Add(vocabulary[boy]);
            var closestAdditions       = vocabulary.Distance(additionRepresentation, count);
            foreach (var neightboor in closestAdditions)
            {
                Console.WriteLine(neightboor.Representation.WordOrNull + "\t\t" + neightboor.DistanceValue);
            }
            #endregion

            Console.WriteLine();

            #region subtraction
            Console.WriteLine("\"" + girl + "\" - \"" + boy + "\" = ...");
            var subtractionRepresentation = vocabulary[girl].Substract(vocabulary[boy]);
            var closestSubtractions       = vocabulary.Distance(subtractionRepresentation, count);
            foreach (var neightboor in closestSubtractions)
            {
                Console.WriteLine(neightboor.Representation.WordOrNull + "\t\t" + neightboor.DistanceValue);
            }
            #endregion

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }