Advanced plugin definition

In addition to finding plugins defined in source code files, senpy can also load a special type of definition file (.senpy files). This used to be the only mechanism for loading in earlier versions of senpy.

The definition file contains basic information

Lastly, it is also possible to add new plugins programmatically.

Definition files

The definition file complements and overrides the attributes provided by the plugin. It can be written in YAML or JSON. The most important attributes are:

  • name: unique name that senpy will use internally to identify the plugin.

  • module: indicates the module that contains the plugin code, which will be automatically loaded by senpy.

  • version

  • extra_params: to add parameters to the senpy API when this plugin is requested. Those parameters may be required, and have aliased names. For instance:

            aliases: # required
                - hello_param
                - hello
            required: true
            default: Hi you
                - Hi you
                - Hello y'all
                - Howdy

A complete example:

name: <Name of the plugin>
module: <Python file>
version: 0.1

And the json equivalent:

  "name": "<Name of the plugin>",
  "module": "<Python file>",
  "version": "0.1"

Example plugin with a definition file

In this section, we will implement a basic sentiment analysis plugin. To determine the polarity of each entry, the plugin will compare the length of the string to a threshold. This threshold will be included in the definition file.

The definition file would look like this:

name: helloworld
module: helloworld
version: 0.0
threshold: 10
description: Hello World

Now, in a file named

#!/bin/env python

from senpy import AnalysisPlugin
from senpy import Sentiment

class HelloWorld(AnalysisPlugin):

    def analyse_entry(entry, params):
        '''Basically do nothing with each entry'''

        sentiment = Sentiment()
        if len(entry.text) < self.threshold:
            sentiment['marl:hasPolarity'] = 'marl:Positive'
            sentiment['marl:hasPolarity'] = 'marl:Negative'
        yield entry