Ejemplo n.º 1
0
        static void NN(string[] args)
        {
            var k = 0;

            if (args.Length == 2)
            {
                k = 10;
            }
            else if (args.Length == 3)
            {
                k = int.Parse(args[2]);
            }
            else
            {
                PrintNNUsage();
                Environment.Exit(-1);
            }

            var fasttext = new FastText();

            fasttext.LoadModel(args[1]);

            const string prompt = "Query word? ";

            Console.Write(prompt);

            while (Console.In.Peek() != -1)
            {
                var queryWord = Console.ReadLine();
                PrintPredictions(fasttext.GetNN(queryWord, k), true, true);
                Console.Write(prompt);
            }
        }
Ejemplo n.º 2
0
        static void train(string[] args)
        {
            var a = new Args();

            a.ParseArgs(args);

            var fasttext       = new FastText();
            var outputFileName = a.output + ".bin";

            var ofs = new FileStream(outputFileName, FileMode.CreateNew, FileAccess.Write);

            if (!ofs.CanWrite)
            {
                throw new ArgumentException($"{outputFileName} cannot be opened for saving.");
            }
            ofs.Close();

            fasttext.Train(a);
            fasttext.SaveModel(outputFileName);
            fasttext.SaveVectors(a.output + ".vec");

            if (a.saveOutput)
            {
                fasttext.SaveOutput(a.output + ".output");
            }
        }
Ejemplo n.º 3
0
        static void Predict(string[] args)
        {
            if (args.Length < 3 || args.Length > 5)
            {
                PrintPredictUsage();
                Environment.Exit(-1);
            }

            var k         = 1;
            var threshold = 0f;

            if (args.Length > 3)
            {
                k = int.Parse(args[3]);
                if (args.Length == 5)
                {
                    threshold = float.Parse(args[4]);
                }
            }

            var printProb = args[0] == "predict-prob";
            var fasttext  = new FastText();

            fasttext.LoadModel(args[1]);

            Stream ifs;

            var infile       = args[2];
            var inputIsStdIn = infile == "-";

            if (!inputIsStdIn)
            {
                var fs = new FileStream(infile, FileMode.Open, FileAccess.Read);
                if (!inputIsStdIn && !fs.CanRead)
                {
                    Console.Error.WriteLine("Input file cannot be opened!");
                    Environment.Exit(-1);
                }

                ifs = fs;
            }
            else
            {
                ifs = Console.OpenStandardInput();
            }

            var predictions = new List <Tuple <float, string> >();

            while (fasttext.PredictLine(ifs, predictions, k, threshold))
            {
                PrintPredictions(predictions, printProb, false);
            }

            ifs.Close();
        }
Ejemplo n.º 4
0
        static void Dump(string[] args)
        {
            if (args.Length < 3)
            {
                PrintDumpUsage();
                Environment.Exit(-1);
            }

            var modelPath = args[1];
            var option    = args[2];

            var fasttext = new FastText();

            fasttext.LoadModel(modelPath);

            if (option == "args")
            {
                fasttext.GetArgs().Dump(Console.Out);
            }
            else if (option == "dict")
            {
                fasttext.GetDictionary().Dump(Console.Out);
            }
            else if (option == "input")
            {
                if (fasttext.IsQuant())
                {
                    Console.Error.WriteLine("Not supported for quantized models.");
                }
                else
                {
                    fasttext.GetInputMatrix().Dump(Console.Out);
                }
            }
            else if (option == "output")
            {
                if (fasttext.IsQuant())
                {
                    Console.Error.WriteLine("Not supported for quantized models.");
                }
                else
                {
                    fasttext.GetOutputMatrix().Dump(Console.Out);
                }
            }
            else
            {
                PrintDumpUsage();
                Environment.Exit(-1);
            }
        }
Ejemplo n.º 5
0
        static void Analogies(string[] args)
        {
            int k = 0;

            if (args.Length == 2)
            {
                k = 10;
            }
            else if (args.Length == 3)
            {
                k = int.Parse(args[2]);
            }
            else
            {
                PrintAnalogiesUsage();
                Environment.Exit(-1);
            }

            if (k <= 0)
            {
                throw new ArgumentException("k needs to be 1 or higher!");
            }

            var fasttext = new FastText();
            var model    = args[1];

            Console.WriteLine($"Loading model {model}");
            fasttext.LoadModel(model);

            const string prompt = "Query triplet (A - B + C)? ";
            string       wordA, wordB, wordC;

            Console.Write(prompt);

            while (true)
            {
                var words = Console.ReadLine().Split(' ');
                wordA = words[0];
                wordB = words[1];
                wordC = words[2];

                PrintPredictions(fasttext.GetAnalogies(k, wordA, wordB, wordC), true, true);

                Console.Write(prompt);
            }
        }
Ejemplo n.º 6
0
        static void Quantize(string[] args)
        {
            var a = new Args();

            if (args.Length < 3)
            {
                PrintQuantizeUsage();
                a.PrintHelp();
                Environment.Exit(-1);
            }

            a.ParseArgs(args);
            var fasttext = new FastText();

            // parseArgs checks if a->output is given.
            fasttext.LoadModel(a.output + ".bin");
            fasttext.Quantize(a);
            fasttext.SaveModel(a.output + ".ftz");
        }
Ejemplo n.º 7
0
        static void PrintNgrams(string[] args)
        {
            if (args.Length != 3)
            {
                PrintPrintNgramsUsage();
                Environment.Exit(-1);
            }

            var fasttext = new FastText();

            fasttext.LoadModel(args[1]);

            var word         = args[2];
            var ngramVectors = fasttext.GetNgramVectors(word);

            foreach (var ngramVector in ngramVectors)
            {
                Console.WriteLine($"{ngramVector.Item1} {ngramVector.Item2}");
            }
        }
Ejemplo n.º 8
0
        static void PrintSentenceVectors(string[] args)
        {
            if (args.Length != 2)
            {
                PrintPrintSentenceVectorsUsage();
                Environment.Exit(-1);
            }

            var fasttext = new FastText();

            fasttext.LoadModel(args[1]);

            var svec = new Vector(fasttext.GetDimension());

            while (Console.In.Peek() != -1)
            {
                fasttext.GetSentenceVector(Console.OpenStandardInput(), svec);
                // Don't print sentence
                Console.WriteLine(svec);
            }
        }
Ejemplo n.º 9
0
        static void PrintWordVectors(string[] args)
        {
            if (args.Length != 2)
            {
                PrintPrintWordVectorsUsage();
                Environment.Exit(-1);
            }

            var fasttext = new FastText();

            fasttext.LoadModel(args[1]);

            var vec = new Vector(fasttext.GetDimension());

            while (Console.In.Peek() != -1)
            {
                var word = Console.ReadLine();

                fasttext.GetWordVector(vec, word);
                Console.WriteLine($"{word} {vec}");
            }
        }
Ejemplo n.º 10
0
        static void Test(string [] args)
        {
            var perLabel = args[0] == "test-label";

            if (args.Length < 3 || args.Length > 5)
            {
                if (perLabel)
                {
                    PrintTestLabelUsage();
                }
                else
                {
                    PrintTestUsage();
                }

                Environment.Exit(-1);
            }

            var model     = args[1];
            var input     = args[2];
            int k         = args.Length > 3 ? int.Parse(args[3]) : 1;
            var threshold = args.Length > 4 ? float.Parse(args[4]) : 0f;

            var fasttext = new FastText();

            fasttext.LoadModel(model);

            var meter = new Meter();

            if (input == "-")
            {
                fasttext.Test(Console.OpenStandardInput(), k, threshold, meter);
            }
            else
            {
                var ifs = new FileStream(input, FileMode.Open, FileAccess.Read);

                if (!ifs.CanRead)
                {
                    Console.Error.WriteLine("Test file cannot be opened!");
                    Environment.Exit(-1);
                }

                fasttext.Test(ifs, k, threshold, meter);
            }

            if (perLabel)
            {
                void writeMetric(string name, double value)
                {
                    Console.Write($"{name} : ");
                    if (double.IsFinite(value))
                    {
                        Console.Write($"{value:0.######}");
                    }
                    else
                    {
                        Console.Write("--------");
                    }
                    Console.Write("  ");
                };

                var dict = fasttext.GetDictionary();
                for (int labelId = 0; labelId < dict.nlabels; labelId++)
                {
                    writeMetric("F1-Score", meter.F1Score(labelId));
                    writeMetric("Precision", meter.Precision(labelId));
                    writeMetric("Recall", meter.Recall(labelId));
                    Console.WriteLine($" {dict.GetLabel(labelId)}");
                }
            }
            meter.WriteGeneralMetrics(Console.Out, k);
        }