Using other Algorithms from your Algorithm: Pipelining (aka Model Chaining)

Using other Algorithms from your Algorithm: Pipelining (aka Model Chaining)

When you put your code on Algorithmia, you have the option of making your Algorithm public so that other user's can run it (even if they can't see the source code). This is completely optional -- and there are certainly cases when you wouldn't want to do so -- but you can still take advantage of the wide variety of Algorithms others have created, and incorporate their capabilities into your own Algorithm. You can also incorporate your own private Algorithms (or your Organization's Algorithms) into other Algorithms that you create, building up a library of functions which can call each other, even if they are running in completely different languages.

When inside an Algorithm, you call other algorithms the same way you would from local code, with one exception: you don't include an API Key (since it gets automatically inherited from the call to the parent Algorithm). Try creating a new Algorithm containing the following code, which accepts a URL as input, then uses Url2Text to extract the text from that webpage, and nlp/SentimentAnalysis to analyze it. Make sure that when you create the Algorithm, the "Internet Access" and "can call other Algorithms" options are turned on:

import Algorithmia
client = Algorithmia.client()
url2text_algo = client.algo('util/Url2Text/0.1.4')
sentiment_algo = client.algo('nlp/SentimentAnalysis/1.0.5')
def apply(url):
    text = url2text_algo.pipe(url).result
    sentiment_input = {"document":text}
    return sentiment_algo.pipe(sentiment_input).result[0]['sentiment']

Now, with just a few lines of code, you have a new Algorithm which takes a URL and returns the bare sentiment value for that page's content. Browse through Algorithmia's catalog, and you may come across some other useful Algorithms you'd like to incorporate into your own.

Congratulations on learning so much about how to create and use Algorithms... let's wrap things up.