Semantria API Developer Reference

Lexalytics Semantria API Documentation Developer Hub

Welcome to the Lexalytics Semantria API Documentation developer hub. You'll find comprehensive guides and documentation to help you start working with Lexalytics Semantria API Documentation as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    

Process some documents

Now that you have your key and secret, you can send documents to Semantria. Since Semantria is asynchronous, you will need to request them as well. Here is a simple snippet to submit three documents for analysis and retrieve them. A more worked out example that handles errors and prints results is shown in our Quick Start Guides.

import semantria, time, uuid

#some sample text
initialTexts = [ "Lisa - there's 2 Skinny cow coupons available $5 skinny cow ice cream coupons on special k boxes and Printable FPC from facebook - a teeny tiny cup of ice cream. I printed off 2 (1 from my account and 1 from dh's). I couldn't find them instore and i'm not going to walmart before the 19th. Oh well sounds like i'm not missing much ...lol", "In Lake Louise - a guided walk for the family with Great Divide Nature Tours rent a canoe on Lake Louise or Moraine Lake  go for a hike to the Lake Agnes Tea House. In between Lake Louise and Banff - visit Marble Canyon or Johnson Canyon or both for family friendly short walks. In Banff  a picnic at Johnson Lake rent a boat at Lake Minnewanka  hike up Tunnel Mountain  walk to the Bow Falls and the Fairmont Banff Springs Hotel  visit the Banff Park Museum. The \"must-do\" in Banff is a visit to the Banff Gondola and some time spent on Banff Avenue - think candy shops and ice cream.", "On this day in 1786 - In New York City  commercial ice cream was manufactured for the first time." ]

documents = []
for text in initialTexts:
    doc_id = str(uuid.uuid4())
    documents.append({'id': doc_id, 'text': text})
    
for text in initialTexts:
    if len(documents) <= subscription['basic_settings']['incoming_batch_limit']:
        status = session.queueBatch(documents)
            if status in [200, 202]:
                print ("{0} documents queued successfully.".format(len(documents)))
                documents = []
    
length = len(initialTexts)
results = []

while len(results) < length:
    time.sleep(0.5)
    print ("Retrieving your processed results...")
    response = session.getProcessedDocuments()
    for item in response:
        results.append(item)
          
using System;
using System.Collections.Generic;
using System.Linq;

using Semantria.Com;

namespace Quickstart
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace with your API key and secret
            string API_KEY = "";
            string API_SECRET = "";
            
            // Some sample text
            List<String> myText = new List<string>();
            myText.Add("Lisa - there's 2 Skinny cow coupons available $5 skinny cow ice cream coupons on special k boxes and Printable FPC from facebook - a teeny tiny cup of ice cream. I printed off 2 (1 from my account and 1 from dh's). I couldn't find them instore and i'm not going to walmart before the 19th. Oh well sounds like i'm not missing much ...lol");
            myText.Add("In Lake Louise - a guided walk for the family with Great Divide Nature Tours  rent a canoe on Lake Louise or Moraine Lake  go for a hike to the Lake Agnes Tea House. In between Lake Louise and Banff - visit Marble Canyon or Johnson Canyon or both for family friendly short walks. In Banff  a picnic at Johnson Lake  rent a boat at Lake Minnewanka  hike up Tunnel Mountain  walk to the Bow Falls and the Fairmont Banff Springs Hotel  visit the Banff Park Museum. The \"must-do\" in Banff is a visit to the Banff Gondola and some time spent on Banff Avenue - think candy shops and ice cream. Have a Fanta while you're there.");
            myText.Add("On this day in 1786 - In New York City  commercial ice cream was manufactured for the first time.");

            // Instantiate a Semantria session
            ISerializer serializer = new Semantria.Com.Serializers.JsonSerializer();
            Semantria.Com.Session mySession = Semantria.Com.Session.CreateSession(API_KEY, API_SECRET, serializer);

            // Generate Semantria Document list to send for processing
            List<Semantria.Com.Mapping.Document> myOutgoingDocuments = new List<Semantria.Com.Mapping.Document>(myText.Count);
            foreach (string aText in myText)
            {
                string DocId = Guid.NewGuid().ToString();
                Semantria.Com.Mapping.Document aDocument = new Semantria.Com.Mapping.Document(){Id = DocId, Text = aText};
                myOutgoingDocuments.Add(aDocument);
            }

            // Queue a batch of documents
            mySession.QueueBatchOfDocuments(myOutgoingDocuments);
            
            // Prepare a list for results
            List<Semantria.Com.Mapping.Output.DocAnalyticData> myResults = new List<Semantria.Com.Mapping.Output.DocAnalyticData>();
            foreach (Semantria.Com.Mapping.Document aDocument in myOutgoingDocuments)
            {
                Semantria.Com.Mapping.Output.DocAnalyticData aResult = new Semantria.Com.Mapping.Output.DocAnalyticData();
                aResult.Id = aDocument.Id;
                aResult.Status = Semantria.Com.TaskStatus.QUEUED;
                myResults.Add(aResult);
            }

            // Poll for results until we've got results for everything we sent
            while (myResults.Any(item => item.Status == TaskStatus.QUEUED))
            {
                // Wait 3 seconds in between each poll for results
                System.Threading.Thread.Sleep(3000);

                // Check for results
                IList<Semantria.Com.Mapping.Output.DocAnalyticData> myIncomingResults = mySession.GetProcessedDocuments();
                foreach (Semantria.Com.Mapping.Output.DocAnalyticData aIncomingResult in myIncomingResults)
                {
                    for (int i = 0; i < myResults.Count; i++)
                    {
                        if (myResults[i].Id == aIncomingResult.Id)
                        {
                            myResults[i] = aIncomingResult;
                            break;
                        }
                    }
                }
            }
        }
    }
}

In a real application, you will want to have one job for queueing and another for requesting. time.sleep() will stop the entire program, so it will stop queueing and requesting; don't use it in your real application.