示例#1
0
        /// <summary>
        /// Gets information about the assigned categories and some more information.
        /// </summary>
        /// <param name="input">The result of an operation performed on a file.</param>
        /// <param name="categoriesFilter">The format of the classification result.</param>
        /// <returns>Результат классификации файла.</returns>
        public ClassificationResult Parse(byte[] input, CategoriesFilter categoriesFilter)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            // Convert the result into a string.
            string content = _encoding.GetString(input);

            // Remove byte order mark (if any) to work with JSON.NET correctly.
            if (content.StartsWith(_byteOrderMark))
            {
                content = content.Remove(0, _byteOrderMark.Length);
            }

            // Convert the string representation of the result into a dynamic object.
            dynamic obj = JsonConvert.DeserializeObject<dynamic>(content);

            // Извлекаем текст, выделенный из файла.
            string documentText = obj.textAnnotations == null ? null : obj.textAnnotations.documentText;

            // Извлекаем результат классификации файла по использованным моделям.
            List<ClassificationModelResult> models = parseModels(obj, categoriesFilter);

            return new ClassificationResult(documentText, models);
        }
示例#2
0
 /// <summary>
 /// Initializes a new instance of the class <see cref="ClassificationParameters"/>.
 /// </summary>
 /// <param name="modelName">The name of the classification model to be used.</param>
 /// <param name="filter">The format of the classification result.</param>
 public ClassificationParameters(string modelName, CategoriesFilter filter)
     : this(modelName)
 {
     CategoriesFilter = filter;
 }
示例#3
0
        /// <summary>
        /// Converts the processing results for a file in a batch to the result of an classification.
        /// </summary>
        /// <param name="taskResult">The processing results for a file in a batch.</param>
        /// <param name="categoriesFilter">The format of the classification result.</param>
        /// <returns>The result of an classification performed on file.</returns>
        private ClassifiedFile convertToClassifiedFile(BatchComprenoTaskResult taskResult, CategoriesFilter categoriesFilter)
        {
            if (taskResult.IsFaulted)
                return new ClassifiedFile(taskResult.FilePath, taskResult.Exception);

            ClassificationResult classificationResult = _resultParser.Parse(taskResult.Operations.Single().Content, categoriesFilter);

            return new ClassifiedFile(taskResult.FilePath, classificationResult);
        }
示例#4
0
 /// <summary>
 /// Converts the format of the classification result into a flag that shows
 /// that only categories with high probability scores should be used.
 /// </summary>
 /// <param name="filter">The format of the classification result.</param>
 /// <returns>The flag that shows that only categories with high probability should be used.</returns>
 private static CategoriesConfidenceLevel convertToConfidenceLevel(CategoriesFilter filter)
 {
     switch (filter)
     {
         case CategoriesFilter.SingleCategory:
         case CategoriesFilter.ConfidentCategories:
             return CategoriesConfidenceLevel.Confident;
         case CategoriesFilter.AllCategories:
             return CategoriesConfidenceLevel.All;
         default:
             throw new InvalidEnumArgumentException("filter", (int)filter, typeof(CategoriesFilter));
     }
 }
示例#5
0
        /// <summary>
        /// Извлекает результат классификации файла по использованным моделям.
        /// </summary>
        /// <param name="obj">Dynamic object that contains the result of an operation performed on a file.</param>
        /// <param name="categoriesFilter">The format of the classification result.</param>
        /// <returns>Information about the assigned categories.</returns>
        private List<ClassificationModelResult> parseModels(dynamic obj, CategoriesFilter categoriesFilter)
        {
            var list = new List<ClassificationModelResult>();

            // Iterate through the obtained results by model.
            foreach (dynamic resultItem in obj.classificationResult.resultList)
            {
                string modelName = resultItem.modelName;
                string errorMessage = resultItem.errorMessage;

                // Allow for classification error.
                if (errorMessage != null)
                {
                    list.Add(new ClassificationModelResult(modelName, errorMessage));

                    continue;
                }

                // Information from the server about the assigned categories.
                IEnumerable<CategoryInfo> categories = enumerateCategories(resultItem.categoryList, obj.textAnnotations);

                // Apply the required CategoriesFilter.
                switch (categoriesFilter)
                {
                    case CategoriesFilter.SingleCategory:
                        categories = categories.Take(1);
                        break;
                    case CategoriesFilter.ConfidentCategories:
                    case CategoriesFilter.AllCategories:
                        break;
                    default:
                        throw new InvalidEnumArgumentException("categoriesFilter", (int)categoriesFilter, typeof(CategoriesFilter));
                }

                list.Add(new ClassificationModelResult(modelName, categories.ToList()));
            }

            return list;
        }