非线性推理系统

2020-02-06  本文已影响0人  月夜星空下
# encoding:utf-8
import mysql.connector  # 先安装mysql-connector-python-1.0.12-py3.3,再引入包
import random
import re
import codecs
import json
import requests
import time
import os, pprint
import os
from aip import AipNlp
import jieba

# dir_name = str(input("请输入生成文件夹名称:"))
# page_num = int(input("请输入生成文章篇数:"))
# dir_name = 'test'
# all_link = '/Users/lilong/Desktop/{}'.format(dir_name)
# dir = os.makedirs(all_link)
config = {'host': '127.0.0.1',
          'user': 'root',
          'password': 'yz1028959',
          'port': 3306,
          'database': 'data_likeshuo',
          'charset': 'utf8'
          }
try:
    cnn = mysql.connector.connect(**config)  # connect方法加载config的配置进行数据库的连接,完成后用一个变量进行接收
except mysql.connector.Error as e:
    print('数据库链接失败!', str(e))
else:  # try没有异常的时候才会执行
    print("")
cursor = cnn.cursor(buffered=True)  # 获取插入的标记位

# sql_insert1 = 'insert into Inference_system (keywords_a, keywords_b, keywords_c) values (%s,%s,%s)'
sql_insert1 = "insert into Inference_system(keywords_a,keywords_b,keywords_c) values (%s,%s,%s)"
num = int(input("输入1进入推理建设系统,输入2进入查询系统: "))
if num == 1:
    print("Hello 欢迎进入推理系统!\r\n目前推理系统正在建设中...\r\n需要大量的三氏信息(A+B=C)和概念信息用于推理建设。\r\n需要你帮助填写下面的信息:\r\na=关键词1\r\nb=关键词2\r\nc=包含a/b关键词的结果")
    i = 10
    article = 0
    while True:
        if article == i:
            break
        a = input("请输入关键词1:")
        b = input("请输入关键词2:")
        c = input("请输入包含a/b关键词的结果:")
        data = (a, b, c)
        cursor.execute(sql_insert1, data)  # 执行插入
        cursor.execute('commit')
        print("ok +1")
        article += 1
elif num == 2:
    print("|灵感系统|")
    w_key = input("请输入希望达到的结果:")
    word = w_key
    cleaned_data = re.findall(u"[\u4e00-\u9fa5]+", word)
    # print(cleaned_data)
    r = ''
    for i in cleaned_data:
        b = str(cleaned_data)
        i = str(i)
        r += i
    a = jieba.lcut(r)
    # print(a)
    def stopwordslist():
        stopwords = [line.strip() for line in open('/Users/lilong/Desktop/stop_words', encoding='UTF-8').readlines()]
        return stopwords
    cleaned_data = re.findall(u"[\u4e00-\u9fa5]+", word)
    # print(cleaned_data)
    r = ''
    for ic in cleaned_data:
        b = str(cleaned_data)
        i = str(ic)
        r += ic
    a = jieba.lcut(r)
    print("正在解析意图...")
    # print(a)
    stopwords = stopwordslist()
    # 输出结果为outstr
    outstr = ''
    # 去停用词
    for word in a:
        if word not in stopwords:
            if word != '\t':
                outstr += word
                outstr += " "
    print(outstr)
    for words_key in outstr:
        s_title = "select keywords_a,keywords_b from Inference_system WHERE keywords_c like '%%%%%s%%%%' ORDER BY RAND()" % words_key
        cursor.execute(s_title)
        rand_title = cursor.fetchall()
        print(rand_title)

else:
    print("你的输入错误")
上一篇 下一篇

猜你喜欢

热点阅读