Question
· Sep 23, 2022

Store Procedure Returning a ResultSet with Embedded Python

Hello,

I'm looking for a way to write a stored procedure or something to return a ResultSet with Embedded Python.

My goal is the following:

I have a Goal table with a Text field that is free text.

CREATE Table Goal (
    Id int,
    Text VARCHAR(5000)
);


I would like to create a procedure that returns all the entities (in the iKnow sense) in a new Entity column.

Python code, i would like to use :

import iknowpy

engine = iknowpy.iKnowEngine()

# Row to parse
text = 'This is a test of the Python interface to the iKnow engine. another sentence to test this program with.'
engine.index(text, 'en')

for s in engine.m_index['sentences']:
    for e in s['entities']:
        if e['type'] == 'Concept':
            print(e['index']) # Should return one new row in the result set

Expected result :

Call My_Python_Procedure(Table);

Entity
-----
test
python interface
iknow engine
sentence
program

How do I go about it ?

Product version: IRIS 2022.1
Discussion (5)1
Log in or sign up to continue

I have partially found a solution:

First I create an SQL function with python code :

CREATE FUNCTION sqliknowparser(tText VARCHAR(50000))
    RETURNS VARCHAR(50000)
    LANGUAGE PYTHON
{
    import iknowpy

    engine = iknowpy.iKnowEngine()

    # index some text
    text = tText
    engine.index(text, 'en')

    t_output = ""

    # or make it a little nicer
    for s in engine.m_index['sentences']:
        for e in s['entities']:
            if e['type'] == 'Concept':
                t_output = t_output  + e['index']+ "|"

    return t_output[:-1]
}

Then I use this function in my query :

SELECT 
ID, sqliknowparser(Text) as entities
FROM AA.Goal 

Then I "piece" it an use a union query :

SELECT 
ID, $piece(sqliknowparser(Text),'|',1) as entities
FROM AA.Goal 
union
SELECT 
ID, $piece(sqliknowparser(Text),'|',2) as entities
FROM AA.Goal 

Any improvement are welcome :)