【问题标题】:How to Extract Keywords from a Database Table that are matching with the Keywords in search string using Python NLP如何使用 Python NLP 从数据库表中提取与搜索字符串中的关键字匹配的关键字
【发布时间】:2020-08-12 05:02:30
【问题描述】:

我有一个带有“Neon”表的数据库。我正在尝试获取与搜索字符串相关的“Neon”表相关的所有关键字。

例子:

Neon Records:

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

我有一个搜索字符串

"Save the day by Sand and Java"

我想得到类似的结果

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

Trails Done 正在连接数据库并提取表数据

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'
}
 

请让我知道您对此的看法。

【问题讨论】:

  • @KranthiKumarReddy 您使用的 SQL Server 版本是什么?
  • SQL Server 2014 版本

标签: python python-3.x string flask nlp


【解决方案1】:

你在找这个吗?

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
    ,''
) + ']'

它产生:

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

【讨论】:

  • 嗨@gotqn,感谢您提供的可能性,但我正在寻找与 Python 脚本类似的东西
  • @KranthiKumarReddy 如果你想要 Python 代码,为什么要标记 SQL Server?请更新您的问题以明确您在寻找什么。
猜你喜欢
  • 2019-03-13
  • 2016-06-02
  • 1970-01-01
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
相关资源
最近更新 更多