Exemplo n.º 1
0
        /// <summary>
        /// Initialize (or reinitialize) the stopwords remover.  Also called by the constructor.
        /// </summary>
        /// <param name="CustomStopWords"></param>
        public void InitializeStopWordsRemover(string[] CustomStopWords = null)
        {
            _mlContext        = new MLContext();
            _emptySamplesList = new List <TextData>();
            _emptyDataView    = _mlContext.Data.LoadFromEnumerable(_emptySamplesList);

            // stop words
            _stopWordsTextPipeline = _mlContext.Transforms.Text.TokenizeIntoWords("Words", "Text")
                                     .Append(_mlContext.Transforms.Text.RemoveDefaultStopWords("WordsWithoutStopWords", "Words", language: StopWordsRemovingEstimator.Language.English));

            if (CustomStopWords == null)
            {
                _textPipeline = _stopWordsTextPipeline.Fit(_emptyDataView);
                _mode         = removeStopWordswMode.Default;
            }
            else
            {
                var textPipeline = _mlContext.Transforms.Text.TokenizeIntoWords("Words",
                                                                                "Text")
                                   .Append(_mlContext.Transforms.Text.RemoveStopWords(
                                               "WordsWithoutStopWords", "Words", stopwords:
                                               CustomStopWords));
                _mode = removeStopWordswMode.Custom;
            }
            _predictionEngine = _mlContext.Model.CreatePredictionEngine <TextData, TransformedTextData>(_textPipeline);
        }
Exemplo n.º 2
0
        private bool IsWindowsInNightLightMode()
        {
            try
            {
                var data = (byte[])(_stateKeyInsider ?? _stateKeyWin10).GetValue("Data");

                if (data == null)
                {
                    const string msg = "could not read or find any data for the night light mode in the registry.";
                    _log.Error(msg);
                    throw new Exception(msg);
                }

                if (_lastData != null && data.SequenceEqual(_lastData))
                {
                    return(_lastResult);
                }
                if (_log.IsDebugEnabled)
                {
                    _log.Debug("read regval: " + data.Select(b => b.ToString()).Aggregate((s1, s2) => s1 + "," + s2));
                }

                var stateString = data.Select(b => b.ToString()).Aggregate((s1, s2) => s1 + "," + s2);

                if (_predictor == null)
                {
                    _context = new Microsoft.ML.MLContext();
                    using (var zipStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("adrilight.Resources.NightLightDetectionModel.zip"))
                    {
                        _model     = _context.Model.Load(zipStream, out var inputSchema) as Microsoft.ML.Data.TransformerChain <Microsoft.ML.ITransformer>;
                        _predictor = _context.Model.CreatePredictionEngine <NightLightDataRow, NightLightState>(_model);
                    }
                }

                var predicted       = _predictor.Predict(new NightLightDataRow(data, true));
                var isUnclearResult = predicted.Probability <= 0.9f && predicted.Probability >= 0.1f;
                if (isUnclearResult)
                {
                    _log.Warn($"predicted: {predicted.PredictedLabel}, {predicted.Probability:0.00000}");
                }
                else
                {
                    _log.Debug($"predicted: {predicted.PredictedLabel}, {predicted.Probability:0.00000}");
                }

                _lastData   = data;
                _lastResult = predicted.PredictedLabel;

                return(predicted.PredictedLabel);
            }
            catch (Exception ex)
            {
                _log.Error(ex, "IsWindowsInNightLightMode() failed");
                throw;
            }
        }