Esempio n. 1
0
File: myLda2.py Progetto: haha8x/eds
num_words = 50
print('本院系文章总数为%d,即将分为主题数%d个,关键字%d个......' %
      (len(corpus), num_topics, num_words))
ldamodel = gensim.models.ldamodel.LdaModel(corpus,
                                           num_topics=num_topics,
                                           id2word=dictionary,
                                           passes=50)
result = ldamodel.print_topics(num_topics=num_topics, num_words=num_words)
time2 = time.time()
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
print('模型训练用时:', time2 - time1)
print('LDA模型训练完成。插入数据库......')

doc_lda = ldamodel[corpus]
for n in range(len(doc_lda)):
    Topic = doc_lda[n]
    wordTopic = [i[1] for i in result if int(Topic[0][0]) == i[0]]
    d = strToMap(wordTopic[0])

    t = {}
    for key in DocWord[n]:
        if key in d.keys():
            t[key] = d[key]
    topic = Topic[0][0]
    prams = (institution_paper_list[n][0], "topic" + str(topic),
             json.dumps(d,
                        ensure_ascii=False), json.dumps(t, ensure_ascii=False))
    sql = 'insert into lda2 values(%s,%s,%s,%s)'
    list = dbs.exe_sql(sql, prams)
print("结束")
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
Esempio n. 2
0
File: egonet.py Progetto: haha8x/eds
        if len(a) >= 1 and name != a['name']:
            find = False
            for t in teacher:
                if a['name'] == t['name'] and (len(
                        a['org']) == 0 or a['org'].find(t['school']) >= 0):
                    find = True
                    key = id + '_' + str(t['id']) + '_' + a['name']
                    if key in egoNet.keys():
                        if l['id'] not in egoNet[key]:
                            egoNet[key].append(l['id'])
                    else:
                        egoNet[key] = []
                        egoNet[key].append(l['id'])
            if find == False:
                key = id + '_-1_' + a['name'] + '*' + a['org']
                if key in egoNet.keys():
                    if l['id'] not in egoNet[key]:
                        egoNet[key].append(l['id'])
                else:
                    egoNet[key] = []
                    egoNet[key].append(l['id'])

for e in egoNet:
    id = e.split('_')
    try:
        parm = (id[0], id[1], id[2], len(egoNet[e]))
    except:
        print(e)
    sql = 'insert into ego_network values(null,%s,%s,%s,%s)'
    dbs.exe_sql(sql, parm)