#%%
import pandas as pd
import numpy as np
import os
import re
import jieba
from pyecharts import options as opts
from pyecharts.charts import Funnel, Page

def Str2value(oristr):#将oristr中的数字提取成float返回
    pattern=re.compile("\\d+")
    result=pattern.findall(oristr)
    return np.float64(result[0])

def Colum2value(df,col):#将oricolum列中所有string转为包含的数字
    oricolum=df[col]
    droplist=[]
    try:
        pattern=re.compile("\\d+")
        for i in range(0,oricolum.values.size):
            if(oricolum.values[i] is np.nan):
                #空数据丢弃
                droplist.append(i)
                continue

            result=pattern.findall(oricolum.values[i])
            if (result):
                oricolum.values[i]=np.float64(result[0])
            else:#无数字数据丢弃
                droplist.append(i)
        df.drop(index=droplist,inplace=True)
        return oricolum
    except:
        print(i,sep=' ')

def read_file(file_name):#去除换行符的行读取，返回内容组
    fp = open(file_name, "r", encoding="utf-8")
    content_lines = fp.readlines()
    fp.close()
    for i in range(len(content_lines)):
        content_lines[i] = content_lines[i].rstrip("\n")
    return content_lines

def GoodsSort(path,outputName="KeyValueSort"):#传入单个goods文件路径，统计其title中关键词，取前十 生成 关键词-平均价格的 outputName.html
    # oridata=os.walk(path)
    # for path,dir_list,file_list in oridata:#遍历路径下所有文件,搜集条目数  
    #     for file_name in file_list:
    #         oneDataPath=os.path.join(path, file_name)#单个文件路径

    #关键字:[计数，价格] 字典
    WordToPrice={}
    #过滤词库 避免无意义词
    StopWords=read_file("StopWords.txt")
    #读取文件
    oridata=pd.read_csv(path)
    #price列清理为数值
    Colum2value(oridata,"price")
    titles=[]
    #对数据每行读取，对标题进行分词产生 关键字组，对组内每个关键字进行过滤，然后字典统计
    for index,row in oridata.iterrows():
        titles=jieba.lcut(row['title'])
        for word in titles:
            if(word not in StopWords):
                WordToPrice[word][0]=WordToPrice.setdefault(word,[0,0])[0]+1;#出现次数
                WordToPrice[word][1]=WordToPrice.setdefault(word,[0,0])[1]+row['price'];#价格累积
    
    WordToPrice=sorted(WordToPrice.items(),key=lambda item:item[1][0],reverse=True)#对字典按关键字出现次数进行排序，从高到低
    #取前十用于显示
    WordToPrice=WordToPrice[:10]
    #用于制图的数据 字典 关键字：平均价格
    DataForShow={}
    for i in WordToPrice:
        DataForShow[i[0]]=int(i[1][1]/i[1][0])

    c = (
        Funnel()
        .add("关键字", [list(z) for z in zip(DataForShow.keys(),DataForShow.values())])
        .set_global_opts(title_opts=opts.TitleOpts(title="关键字-平均价格\n（由高到低）"))
    )

    c.render('../html/goods/' + outputName + ".html")



#%%示例
# GoodsSort("C:\\MyWorkSpace\\shu-innovation-project\\data\\goods\\suning.csv","SuNing")
# GoodsSort("C:\\MyWorkSpace\\shu-innovation-project\\data\\goods\\jingdong.csv","JingDong")
# GoodsSort("C:\\MyWorkSpace\\shu-innovation-project\\data\\goods\\taobao.csv","TaoBao")