How to Extract Keywords from a Database Table that are matching with the Keywords in search string using Python NLP

Viewed 198

I have a database with a Table "Neon". I am trying to get all the keywords associated with the table "Neon" related to a search string.

Example:

Neon Records:

POLICY_NUM          DAYS_TO_BOUND   
0170254497              PL Rating
0755698054              PL Rating
1525668307              PL Rating
1525668312              Air
1525668314              Java
1525668356              Sand
    

and I have a search string

"Save the day by Sand and Java"

I want to get the results like

['Sand':'1525668356','Java':'1525668314']

Trails Done are connecting to Database and Extracting Table Data

import pandas as pd
import logging
import config
from sqlalchemy import create_engine  # install mysqlConnector and PyMySql


def db_connection():
    """
    :return:
    """
    try:
        engine = create_engine('mysql+pymysql://{0}:{1}@{2}/{3}'.format(config.database_config['user'],
                                                                        config.database_config['password'],
                                                                        config.database_config['host'],
                                                                        config.database_config['database']))

        return engine

    except Exception as e:
        logging.info(e)
    finally:
        pass


def extract_table(query):
    """
    :param query:
    :return:
    """

    engine = db_connection()
    sql_select_query = query
    details = pd.read_sql(sql_select_query, engine)
    return details

database_query = {
    'select_query_for_data': 'select * from Neon'
}
 

Please let me know your thoughts on this.

1 Answers

Are you looking for this?

DECLARE @DataSource TABLE
(
    [POLICY_NUM] VARCHAR(16)
   ,[DAYS_TO_BOUND] VARCHAR(16)
);

INSERT INTO @DataSource ([POLICY_NUM], [DAYS_TO_BOUND])
VALUES ('0170254497', 'PL Rating')
      ,('0755698054', 'PL Rating')
      ,('1525668307', 'PL Rating')
      ,('1525668312', 'Air')
      ,('1525668314', 'Java')
      ,('1525668356', 'Sand');

DECLARE @DataString VARCHAR(4000) = 'Save the day by Sand and Java';
DECLARE @DataStringXML XML = '<a>' + REPLACE(@DataString, ' ', '</a><a>') + '</a>';

WITH DataSource ([rowID], [rowValue]) AS
(
    SELECT ROW_NUMBER() OVER (ORDER BY T.c ASC)
          ,T.c.value('.', 'VARCHAR(256)')
    FROM @DataStringXML.nodes('a') T(c)
)
SELECT '[' + STUFF
(
    (
        SELECT ',' + '''' + V.[rowValue] + ''':''' + D.[POLICY_NUM] + ''''
        FROM DataSource V
        INNER JOIN @DataSource D
            ON V.[rowValue] = D.[DAYS_TO_BOUND]
        ORDER BY V.[rowID]
        FOR XML PATH(''), TYPE
    ).value('.', 'VARCHAR(MAX)')
    ,1
    ,1
    ,''
) + ']'

it yields:

['Sand':'1525668356','Java':'1525668314']
Related