import redis
import codecs

ds = redis.StrictRedis(host="localhost", port=6379, db=1)

with codecs.open('freebase_links_en.ttl', 'r', encoding='utf-8') as f:
    for i, line in enumerate(f):
        if i != 0:
            try:
                k = line.split()[0].split('resource/')[1][:-1]
                v = line.split('/ns/')[1][:-4]
                ds.set(k, v)
            except IndexError:
                print(line)

        if i%10000 == 0:
            print('{} done'.format(i))


#####################################################
# for i, (k, v) in enumerate(freebase_ids.items()): #
#     print(k,v)                                    #
#     if i == 20:                                   #
#         break                                     #
# print(len(freebase_ids))                          #
#                                                   #
# with open('freebase_ids_en.pickle', 'wb') as f:   #
#     pickle.dump(freebase_ids, f)                  #
#                                                   #
# with open('freebase_ids_en.pickle', 'rb') as f:   #
#     freebase_ids = pickle.load(f)                 #
#                                                   #
# for i, (k, v) in enumerate(freebase_ids.items()): #
#     print(k, v)                                   #
#     if i == 20:                                   #
#         break                                     #
# print(len(freebase_ids))                          #
#####################################################


