示例#1
0
 /// <summary>
 /// Construct a request for the online service to annotate or classify a document downloaded directly from a remote URL
 /// </summary>
 /// <param name="documentUrl">the URL from which the document should be downloaded</param>
 /// <param name="type">the MIME type that the service should use to parse the document</param>
 /// <param name="imageTagging">The boolean flag to allow/deny image tagging of the document</param>
 /// <param name="imageCategorization">The boolean flag to allow/deny image categorization of the document</param>
 public ServiceRequest(Uri documentUrl, SupportedMimeType type, bool imageTagging, bool imageCategorization)
 {
     this.DocumentUrl         = documentUrl.ToString();
     this.DocumentType        = type.ToStringValue();
     this.ImageTagging        = imageTagging;
     this.ImageCategorization = imageCategorization;
 }
示例#2
0
 /// <summary>
 /// Construct a request for the online service to annotate or classify a document provided as part of the request.
 /// </summary>
 /// <param name="document">the content to process.</param>
 /// <param name="type">the MIME type that the service should use to parse the document.</param>
 public ServiceRequest(String document, SupportedMimeType type)
 {
     this.Document            = document;
     this.DocumentType        = type.ToStringValue();
     this.ImageTagging        = false;
     this.ImageCategorization = false;
 }
示例#3
0
        /// <summary>
        /// Annotates a single document publicly available under a given URL.
        /// Returns an object which allows for convenient access to the
        /// annotations in the annotated document
        /// </summary>
        /// <param name="documentUrl">the publicly accessible URL from where the document will be downloaded</param>
        /// <param name="documentMimeType">the MIME type of the document which will be annotated</param>
        /// <returns>an {@link AnnotatedDocument} which allows for convenient programmatic access to the annotated document</returns>
        public AnnotatedDocument annotateDocumentFromUrl(Uri documentUrl,
                                                         SupportedMimeType documentMimeType)
        {
            ServiceRequest rq =
                new ServiceRequest(documentUrl, documentMimeType, null);

            return(processRequest(rq, true));
        }
示例#4
0
        /// <summary>
        /// Annotates a single document with the specified MIME type. Returns
        /// an object which allows for convenient access to the annotations in
        /// the annotated document.
        /// </summary>
        /// <param name="documentText">the document content to annotate</param>
        /// <param name="documentMimeType"> the MIME type of the document which will beannotated</param>
        /// <returns>an {@link AnnotatedDocument} containing the original content as well as the annotations produced</returns>
        public AnnotatedDocument annotateDocument(String documentText,
                                                  SupportedMimeType documentMimeType)
        {
            ServiceRequest rq =
                new ServiceRequest(documentText, documentMimeType, null);

            return(processRequest(rq, true));
        }
示例#5
0
 /// <summary>
 /// Construct a request for the online service to annotate a document
 /// it downloads directly from a remote URL.
 /// </summary>
 /// <param name="documentUrl">the URL from which the document should be
 /// downloaded. This must be accessible to the service so it
 /// must not require authentication credentials etc. (but it
 /// may be, for example, a pre-signed Amazon S3 URL).</param>
 /// <param name="type">the MIME type that the service should use to parse the
 /// document.</param>
 /// <param name="annotationSelectors">annotations to return. Leave as
 /// <code>null</code> to use the default selectors recommended
 /// by the pipeline provider.</param>
 public ServiceRequest(Uri documentUrl, SupportedMimeType type,
         List<AnnotationSelector> annotationSelectors)
 {
     this.documentUrl = documentUrl.ToString();
     this.documentType = type.ToStringValue();
     if (annotationSelectors != null)
     {
         this.annotationSelectors = new List<string>();
         foreach (AnnotationSelector aSel in annotationSelectors)
         {
             this.annotationSelectors.Add(aSel.ToString());
         }
     }
 }
示例#6
0
        /// <summary>
        /// Annotates a document publicly available under a given URL and tags and/or categorizes any images inside.
        /// </summary>
        /// <param name="documentUrl">the publicly accessible URL from where the document will be downloaded</param>
        /// <param name="documentMimeType">the MIME type of the document which will be annotated</param>
        /// <param name="serializationFormat">the serialization format of the output</param>
        /// <returns>Аn {@link InputStream} from where the serialized output can be read</returns>
        public Stream annotateDocumentAsStream(Uri documentUrl, SupportedMimeType documentMimeType, ResponseFormat serializationFormat, bool imageTagging, bool imageCategorization)
        {
            ServiceRequest rq = new ServiceRequest(documentUrl, documentMimeType, imageTagging, imageCategorization);

            try
            {
                return(client.requestForStream("", "POST", rq, constructHeaders(serializationFormat)));
            }
            catch (HttpClientException e)
            {
                String msg = handleErrors(e);
                throw new S4ServiceClientException(msg == null ? e.Message : msg, e);
            }
        }
示例#7
0
        /// <summary>
        /// Annotates a single document and returns an {@link InputStream} from
        /// which the contents of the serialized annotated document can be read
        /// </summary>
        /// <param name="documentText">the contents of the document which will be annotated</param>
        /// <param name="documentMimeType">the MIME type of the file which will be annotated</param>
        /// <param name="serializationFormat">the format which will be used for serialization of the annotated document</param>
        /// <returns>an {@link InputStream} from which the serialization of the annotated document can be read</returns>
        public Stream annotateDocumentAsStream(String documentText, SupportedMimeType documentMimeType, ResponseFormat serializationFormat)
        {
            ServiceRequest rq = new ServiceRequest(documentText, documentMimeType);

            try
            {
                return(client.requestForStream("", "POST", rq, constructHeaders(serializationFormat)));
            }
            catch (HttpClientException e)
            {
                String msg = handleErrors(e);
                throw new S4ServiceClientException(msg == null ? e.Message : msg.ToString());
            }
        }
示例#8
0
 /// <summary>
 /// Construct a request for the online service to annotate a document
 /// it downloads directly from a remote URL.
 /// </summary>
 /// <param name="documentUrl">the URL from which the document should be
 /// downloaded. This must be accessible to the service so it
 /// must not require authentication credentials etc. (but it
 /// may be, for example, a pre-signed Amazon S3 URL).</param>
 /// <param name="type">the MIME type that the service should use to parse the
 /// document.</param>
 /// <param name="annotationSelectors">annotations to return. Leave as
 /// <code>null</code> to use the default selectors recommended
 /// by the pipeline provider.</param>
 public ServiceRequest(Uri documentUrl, SupportedMimeType type,
                       List <AnnotationSelector> annotationSelectors)
 {
     this.documentUrl  = documentUrl.ToString();
     this.documentType = type.ToStringValue();
     if (annotationSelectors != null)
     {
         this.annotationSelectors = new List <string>();
         foreach (AnnotationSelector aSel in annotationSelectors)
         {
             this.annotationSelectors.Add(aSel.ToString());
         }
     }
 }
示例#9
0
        /// <summary>
        /// Classifies a single document publicly available under a given URL.
        /// Returns the classification information for the document serialized into the specified format
        /// </summary>
        /// <param name="documentUrl">the publicly accessible URL from where the document will be downloaded</param>
        /// <param name="documentMimeType">the MIME type of the document which will be classified</param>
        /// <returns>Аn {@link InputStream} from where the serialized output can be read</returns>
        public Stream classifyDocumentAsStream(Uri documentUrl, SupportedMimeType documentMimeType)
        {
            ServiceRequest rq = new ServiceRequest(documentUrl, documentMimeType);

            try
            {
                return(client.requestForStream("", "POST", rq, constructHeaders(ResponseFormat.JSON)));
            }
            catch (HttpClientException e)
            {
                String msg = handleErrors(e);
                throw new S4ServiceClientException(msg == null ? e.Message : msg, e);
            }
        }
示例#10
0
        /// <summary>
        /// Classifies the contents of a single file returning an {@link InputStream}
        /// from which the classification information can be read
        /// </summary>
        /// <param name="documentContent">the file which will be classified</param>
        /// <param name="documentEncoding">the encoding of the file which will be classified</param>
        /// <param name="documentMimeType">the MIME type of the file which will be classified</param>
        public Stream classifyDocumentAsStream(FileStream documentContent,
                                               String documentEncoding, SupportedMimeType documentMimeType)
        {
            String documentPath = Path.GetFullPath(documentContent.Name);

            if (!documentContent.CanRead)
            {
                throw new IOException("File " + documentPath + " is not readable.");
            }

            Byte[] buff;
            buff = File.ReadAllBytes(documentPath);
            Encoding encode  = System.Text.Encoding.GetEncoding(documentEncoding);
            String   content = new UTF8Encoding().GetString(buff);

            return(classifyDocumentAsStream(content, documentMimeType));
        }
示例#11
0
        /// <summary>
        /// Annotates the contents of a single file returning an {@link InputStream} from which the annotated content can be read
        /// </summary>
        /// <param name="documentContent">the file which will be annotated</param>
        /// <param name="documentEncoding">the encoding of the file which will be annotated</param>
        /// <param name="documentMimeType">the MIME type of the file which will be annotated</param>
        /// <param name="serializationFormat">the serialization format used for the annotated content</param>
        public Stream annotateFileContentsAsStream(FileStream documentContent,
                                                   String documentEncoding, SupportedMimeType documentMimeType,
                                                   ResponseFormat serializationFormat)
        {
            String documentPath = Path.GetFullPath(documentContent.Name);

            if (!documentContent.CanRead)
            {
                throw new IOException("File " + documentPath + " is not readable.");
            }
            Byte[] buff;
            buff = File.ReadAllBytes(documentPath);
            Encoding encode  = System.Text.Encoding.GetEncoding(documentEncoding);
            String   content = new UTF8Encoding().GetString(buff);

            return(annotateDocumentAsStream(content, documentMimeType, serializationFormat));
        }
示例#12
0
        private static void CompressImage(string filePath, Bitmap img, string savePath, int quality, Size size,
                                          SupportedMimeType type, bool isOnlyForDisplay = true)
        {
            ImageCodecInfo imageCodecInfo;

            if (type == SupportedMimeType.JPEG)
            {
                imageCodecInfo = Helper.GetEncoderInfo("image/jpeg");
            }
            else if (type == SupportedMimeType.PNG)
            {
                imageCodecInfo = Helper.GetEncoderInfo("image/png");
            }
            else
            {
                imageCodecInfo = Helper.GetEncoderInfoFromOriginalFile(filePath);
            }

            EncoderParameters encoderParameters;

            if (img.Size.Height < size.Height || img.Size.Width < size.Width)
            {
                size = img.Size;
            }

            Bitmap imgCompressed = new Bitmap(img, size);



            foreach (var id in img.PropertyIdList)
            {
                imgCompressed.SetPropertyItem(img.GetPropertyItem(id));
            }


            SetImageComments(imgCompressed);
            img.Dispose();

            encoderParameters          = new EncoderParameters(1);
            encoderParameters.Param[0] =
                new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);


            imgCompressed.Save(Helper.FolderSaveFile(filePath, savePath), imageCodecInfo, encoderParameters);
            imgCompressed.Dispose();
        }
示例#13
0
        /// <summary>
        /// Classifies a single document and returns an {@link InputStream} from
        /// which the contents of the serialized classified document can be read
        /// </summary>
        /// <param name="documentText">the contents of the document which will be classified</param>
        /// <param name="documentMimeType">the MIME type of the file which will be classified</param>
        /// <returns>an {@link InputStream} from which the serialization of the classified document can be read</returns>
        public Stream classifyDocumentAsStream(String documentText, SupportedMimeType documentMimeType)
        {
            ServiceRequest      rq      = new ServiceRequest(documentText, documentMimeType);
            WebHeaderCollection headers = new WebHeaderCollection();

            headers.Add("Accept", ResponseFormat.JSON.ToStringValue());
            try
            {
                return(client.requestForStream("", "POST", rq, headers));
            }
            catch (HttpClientException e)
            {
                JsonObject response = e.getResponse();
                if (response == null)
                {
                    throw new S4ServiceClientException(e.Message);
                }
                String msg = response.ToString();
                throw new S4ServiceClientException(msg == null ? e.Message : msg.ToString());
            }
        }
示例#14
0
        /// <summary>
        /// Annotates the contents of a single file with the specified MIME
        /// type. Returns an object which allows for convenient access to the
        /// annotations in the annotated document.
        /// </summary>
        /// <param name="documentContent">the file whose contents will be annotated</param>
        /// <param name="documentEncoding">the encoding of the document file</param>
        /// <param name="documentMimeType">the MIME type of the document to annotated</param>
        /// <returns>an {@link AnnotatedDocument} containing the original content as well as the annotations produced</returns>
        public AnnotatedDocument annotateFileContents(FileStream documentContent,
                                                      String documentEncoding, SupportedMimeType documentMimeType)
        {
            String documentPath = Path.GetFullPath(documentContent.Name);

            if (!File.Exists(documentPath))
            {
                throw new IOException("File " + documentPath.ToString()
                                      + " is not readable.");
            }
            MemoryStream stream = new MemoryStream();

            using (BinaryWriter writer = new BinaryWriter(stream))
            {
                writer.Write(File.ReadAllBytes(documentPath));
            }
            byte[] bytes   = stream.ToArray();
            String content = Encoding.UTF8.GetString(bytes);

            return(annotateDocument(content, documentMimeType));
        }
示例#15
0
        /// <summary>
        /// Classifies a single document publicly available under a given URL.
        /// Returns the classification information for the document serialized into the specified format
        /// </summary>
        /// <param name="documentUrl">the publicly accessible URL from where the document will be downloaded</param>
        /// <param name="documentMimeType">the MIME type of the document which will be classified</param>
        /// <returns>an {@link InputStream} from where the serialized output can be read</returns>
        public Stream classifyDocumentFromUrlAsStream(Uri documentUrl, SupportedMimeType documentMimeType)
        {
            ServiceRequest rq = new ServiceRequest(documentUrl, documentMimeType);

            try
            {
                WebHeaderCollection collection = new WebHeaderCollection();
                collection.Set(HttpRequestHeader.Accept, ResponseFormat.JSON.ToStringValue());
                return(client.requestForStream("", "POST", rq, collection));
            }
            catch (HttpClientException e)
            {
                JsonObject response = e.getResponse();
                if (response == null)
                {
                    throw new S4ServiceClientException(e.Message, e);
                }
                String msg = response.ToString();
                throw new S4ServiceClientException(msg == null ? e.Message : msg, e);
            }
        }
示例#16
0
 /// <summary>
 /// Construct a request for the online service to annotate or classify a document provided as part of the request.
 /// </summary>
 /// <param name="document">the content to process.</param>
 /// <param name="type">the MIME type that the service should use to parse the document.</param>
 public ServiceRequest(String document, SupportedMimeType type)
 {
     this.Document     = document;
     this.DocumentType = type.ToStringValue();
 }
示例#17
0
 /// <summary>
 /// Construct a request for the online service to annotate or classify a document downloaded directly from a remote URL
 /// </summary>
 /// <param name="documentUrl">the URL from which the document should be
 /// downloaded. This must be accessible to the service so it
 /// must not require authentication credentials etc. (but it
 /// may be, for example, a pre-signed Amazon S3 URL).</param>
 /// <param name="type">the MIME type that the service should use to parse the
 /// document.</param>
 public ServiceRequest(Uri documentUrl, SupportedMimeType type)
 {
     this.documentUrl  = documentUrl.ToString();
     this.documentType = type.ToStringValue();
 }
示例#18
0
        /// <summary>
        /// Annotates a document publicly available under a given URL and tags and/or categorizes any images inside.
        /// </summary>
        /// <param name="documentUrl">the publicly accessible URL from where the document will be downloaded</param>
        /// <param name="documentMimeType">the MIME type of the document which will be annotated</param>
        /// <returns>An {@link AnnotatedDocument} object with the processed image tags and categories</returns>
        public AnnotatedDocument annotateDocument(Uri documentUrl, SupportedMimeType documentMimeType, bool imageTagging, bool imageCategorization)
        {
            ServiceRequest rq = new ServiceRequest(documentUrl, documentMimeType, imageTagging, imageCategorization);

            return(processRequest(rq));
        }
示例#19
0
        /// <summary>
        /// Annotates a single document publicly available under a given URL.
        /// Returns an object which allows for convenient access to the annotations in the annotated document
        /// </summary>
        /// <param name="documentUrl">the publicly accessible URL from where the document will be downloaded</param>
        /// <param name="documentMimeType">the MIME type of the document which will be annotated</param>
        /// <returns>an {@link AnnotatedDocument} which allows for convenient programmatic access to the annotated document</returns>
        public AnnotatedDocument annotateDocument(Uri documentUrl, SupportedMimeType documentMimeType)
        {
            ServiceRequest rq = new ServiceRequest(documentUrl, documentMimeType);

            return(processRequest(rq));
        }
示例#20
0
        public static void main(String[] args)
        {
            if (args == null || args.Length == 0)
            {
                printUsageAndTerminate(null);
            }
            Parameters parameters = new Parameters(args);
            String     serviceID  = parameters.getValue("service");

            if (serviceID == null)
            {
                printUsageAndTerminate("No service name provided");
            }
            ServiceDescriptor service = null;

            try
            {
                service = ServiceCatalog.getItem(serviceID);
            }
            catch (NotSupportedException nse)
            {
                printUsageAndTerminate("Unsupported service '" + serviceID + '\'');
                Console.WriteLine(nse.Message);
            }
            SupportedMimeType mimetype = SupportedMimeType.PLAINTEXT;

            if (parameters.getValue("dtype") != null)
            {
                try
                {
                    mimetype = NumToEnum <SupportedMimeType>(parameters.getValue("dtype").ToString());
                }
                catch (ArgumentException ae)
                {
                    printUsageAndTerminate("Unsupported document type (dtype) : " + parameters.getValue("dtype"));
                    Console.WriteLine(ae.Message);
                }
            }
            String inFile  = parameters.getValue("file");
            String url     = parameters.getValue("url");
            String outFile = parameters.getValue("out", "result.txt");

            if (inFile != null)
            {
                if (false == new FileStream(inFile, FileMode.OpenOrCreate).CanRead)
                {
                    printUsageAndTerminate("Input file is not found : " + inFile);
                }
            }
            else
            {
                if (url == null)
                {
                    printUsageAndTerminate("Neither input file, nor remote URL provided");
                }
            }

            Dictionary <String, String> creds = readCredentials(parameters);

            if (false == creds.ContainsKey("apikey") ||
                false == creds.ContainsKey("secret"))
            {
                printUsageAndTerminate("No credentials details found");
            }

            S4ServiceClient client = new S4ServiceClient(service, creds["apikey"], creds["secret"]);

            try
            {
                Stream resultData = (inFile != null) ?
                                    client.annotateFileContentsAsStream(new FileStream(inFile, FileMode.OpenOrCreate), CharSet.Unicode.ToString(), mimetype, ResponseFormat.JSON)
                        : client.annotateDocumentFromUrlAsStream(new Uri(url), mimetype, ResponseFormat.JSON);

                FileStream outStream = new FileStream(outFile, FileMode.Create);
                resultData.CopyTo(outStream);

                outStream.Close();
                resultData.Close();
            }
            catch (IOException ioe)
            {
                Console.WriteLine(ioe.Message);
                Environment.Exit(1);
            }
        }
示例#21
0
        /// <summary>
        /// Classifies a single document with the specified MIME type. Returns an object
        /// which allows for convenient access to the classification information for the document.
        /// </summary>
        /// <param name="documentText">the document content to classify</param>
        /// <param name="documentMimeType"> the MIME type of the document which will be classified</param>
        /// <returns>A {@link ClassifiedDocument} containing the original content as well as the classifications produced</returns>
        public ClassifiedDocument classifyDocument(String documentText, SupportedMimeType documentMimeType)
        {
            ServiceRequest rq = new ServiceRequest(documentText, documentMimeType);

            return(classifyRequest(rq));
        }
示例#22
0
        /// <summary>
        /// Classifies a single document publicly available under a given URL.
        /// Returns an object which allows for convenient access to the classification information for the document
        /// </summary>
        /// <param name="documentUrl">the publicly accessible URL from where the document will be downloaded</param>
        /// <param name="documentMimeType">the MIME type of the document which will be classified</param>
        /// <returns>A {@link ClassifiedDocument} which allows for convenient programmatic access to the classified document</returns>
        public ClassifiedDocument classifyDocument(Uri documentUrl, SupportedMimeType documentMimeType)
        {
            ServiceRequest rq = new ServiceRequest(documentUrl, documentMimeType);

            return(classifyRequest(rq));
        }
示例#23
0
        public static void CompressImage(string filePath, string savePath, int quality, int size, SupportedMimeType type)
        {
            Bitmap img = new Bitmap(filePath);

            CompressImage(
                filePath,
                img,
                savePath,
                quality,
                new Size((int)(img.Width * size / 100), (int)(img.Height * size / 100)),
                type
                );
            img.Dispose();
        }