import MySQLdb, sys, configparser, time, subprocess


def sampling_0(project, sampling, labels, mysql_id, mysql_pass, root):
    import_from_mysql(project, sampling, labels, mysql_id, mysql_pass, root)



def import_from_mysql(project, sampling, labels, mysql_id, mysql_pass):

    if int(labels) > 0:
        sql="select label_name, morpheme from text_table"
    else:
        sql="select morpheme from text_table"

    try:
        conn=MySQLdb.connect(db=project, user=mysql_id, passwd=mysql_pass, charset="utf8mb4")
        cursor=conn.cursor()

        time.sleep(0.1)

        cursor.execute(sql)
        all_sentence = cursor.fetchall()

        time.sleep(0.1)

        conn.commit()
        cursor.close()
        conn.close()

        g = open("encode_temp.txt","w")
        for n in all_sentence:
            n=str(n)
            n=n.replace("(' ","").replace("',)","")
            n=n.replace("', '", ", ").replace("('", "").replace("')","").replace("  "," ")
            g.write(n)
            g.write("\n")
        g.close()

    except Exception as e:
        print(e)
