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)
);
SQLSQL
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
PythonPython
Expected result :
Call My_Python_Procedure(Table);
Entity
-----
test
python interface
iknow engine
sentence
program
SQLSQL
How do I go about it ?
Product version: IRIS 2022.1
Define a custom class query with Exec/Fetch methods in python, after that call this query from a method with ReturnResultsets enabled.
Or just call custom class query from SQL.
Is it possible to do the same thing only in SQL and Python?
CREATE PROCEDURE procname(parameter_list) [ characteristics ] LANGUAGE PYTHON { code_body }
You can generate methods using Native API for Python, so technically yes.
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 :)
https://community.intersystems.com/post/create-stored-procedures-using-e...