예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CalaisDotNet"/> class.
        /// </summary>
        /// <param name="apiKey">The API key.</param>
        /// <param name="content">The content.</param>
        /// <param name="inputFormat">Format of the input content.</param>
        /// <param name="baseUrl">Base URL to be put in rel-tag microformats.</param>
        /// <param name="allowDistribution">if set to <c>true</c> Indicates whether the extracted metadata can be distributed.</param>
        /// <param name="allowSearch">if set to <c>true</c> Indicates whether future searches can be performed on the extracted metadata.</param>
        /// <param name="externalId">User-generated ID for the submission.</param>
        /// <param name="submitter">Identifier for the content submitter.</param>
        /// <param name="enableMetadataType">if set to <c>true</c> Indicates whether the output (RDF only) will include Generic Relation extractions.</param>
        /// <param name="calculateRelevanceScore">if set to <c>true</c> Indicates whether the extracted metadata will include relevance score for each unique entity.</param>
        public CalaisDotNet(string apiKey, string content, CalaisInputFormat inputFormat, string baseUrl, bool allowDistribution, bool allowSearch, string externalId, string submitter, bool enableMetadataType, bool calculateRelevanceScore)
        {
            //Contract
            this.Require(x => apiKey.Length == 24, new ArgumentException("API Key must be 24 characters long (yours was" + apiKey.Length + ")"));
            this.Require(x => !string.IsNullOrEmpty(content), new ArgumentException("Content cannot be empty"));

            // initialise inputs required to call web service
            ApiKey                  = apiKey;
            Content                 = content;
            InputFormat             = inputFormat;
            OutputFormat            = CalaisOutputFormat.Simple;
            BaseUrl                 = baseUrl;
            AllowDistribution       = allowDistribution;
            AllowSearch             = allowSearch;
            ExternalId              = externalId;
            Submitter               = submitter;
            CalculateRelevanceScore = calculateRelevanceScore;
            EnableMetadataType      = string.Empty;

            if (enableMetadataType)
            {
                EnableMetadataType = "GenericRelations";
            }

            // create a new web service proxy to Calais
            _webServiceProxy = new CalaisServiceProxy();
        }
예제 #2
0
        public T Call <T>() where T : ICalaisDocument
        {
            T document;

            // Switch output type to version based on the object being returned
            // Call object factory to create concrete implimentation

            switch (typeof(T).Name)
            {
            case ("CalaisRdfDocument"):
            {
                OutputFormat = CalaisOutputFormat.Rdf;
                document     = ObjectFactory.Create <T>("Calais.CalaisRdfDocument");
                break;
            }

            case ("CalaisMicroFormatsDocument"):
            {
                OutputFormat = CalaisOutputFormat.MicroFormats;
                document     = ObjectFactory.Create <T>("Calais.CalaisMicroFormatsDocument");
                break;
            }

            case ("CalaisSimpleDocument"):
            {
                OutputFormat = CalaisOutputFormat.Simple;
                document     = ObjectFactory.Create <T>("Calais.CalaisSimpleDocument");
                break;
            }

            case ("CalaisJsonDocument"):
            {
                OutputFormat = CalaisOutputFormat.JSON;
                document     = ObjectFactory.Create <T>("Calais.CalaisJsonDocument");
                break;
            }

            default:
            {
                throw new ArgumentException("Unknown type");
            }
            }

            // Get correctly formatted input XML
            string paramsXml = BuildInputParamsXml();

            //Check XML was built correctly
            this.Ensure(x => !string.IsNullOrEmpty(paramsXml), new ApplicationException("Building parameters XML failed!"));

            // call web service to get response
            string response = _webServiceProxy.Enlighten(ApiKey, Content, paramsXml);

            //Check response is not empty
            this.Ensure(x => !string.IsNullOrEmpty(response), new ApplicationException("Server response is empty!"));

            //Check for error message
            this.Ensure(x => !response.Contains("<Error>"), new ApplicationException("Server reported an error"));

            //TODO: Process <Error> message here !

            ((ICalaisDocument)document).ProcessResponse(response);

            return(document);
        }