Running R Package POMDP from Python

python R

Chatbots are now used in many applications for different purposes. The popularity of this type widget can be estimated from this fact:
As of August 2019, search results on Google for the following keywords:

  • chatbot – Volume: 246,000 searches per month and found 32,700,000 results
  • neural net – Volume 3600 searches per month and 127,000,000 results
  • One of the key components of chatbot architecture is Dialog Management. In order to incorporate some degree of uncertainty in classifying intents and entities, partially observable Markov decision process (POMDP) algorithm was proposed for building chatbots with Dialog Management. [1],[2]

    How can we run POMDP? There is a package in R for this.

    But what if we want run from python? One of the way could be call R from python. Here is how it can be done.

    First we need to create R program. For running POMDP in R we need first download and install (only one time). We also need set working directory (if it is different than default) and specify pdf file name – this is where the R
    script will output charts and diagrams. See below lines for this:

    r = getOption("repos")
    r["CRAN"] = ""
    options(repos = r)
    setwd ("C://Users//username//Documents")
    ## the below line need to be run only first time and it must be run with admin priv.

    Now we can continue with our R program. Documentation for POMDP [3] has R code for tiger problem, that we can insert here.

    Running POMDP R Package from Python

    Now we need to create python script that will call R script. In addition to calling the R program, python will also display output from running R program. This is possible because when we call R program, we save output within txt file. So the python script will save output into file and also output on screen.

    # -*- coding: utf-8 -*-
    import os
    os.system('"C:\\Program Files\\R\\R-3.4.3\\bin\\Rscript" C:\\Users\\username\\POMDP_R\\r_example.r > C:\\Users\\username\\output_file_ex.txt')
    with open('output_file_ex.txt', 'r') as reader:

    Now we can run R package POMDP or any other R program from python and start build advanced chatbot dialog system.

    1. Dialog management
    2. An Improved Approach of Intention Discovery with Machine Learning for POMDP-based Dialogue Management
    3. POMDP: Introduction to Partially Observable Markov Decision Processes

    Leave a Comment