Last active
November 13, 2017 12:32
-
-
Save bowbowbow/d7eb9daa733a0cb30e03947f08db6f68 to your computer and use it in GitHub Desktop.
trust있으면 만점, distrust있으면 최하점 넣는 방식으로 fr_train.txt 생성
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import defaultdict | |
| users = {} | |
| trust = {} | |
| # 신뢰 관계 특정 개수 이상인 유저 | |
| group = {} | |
| group_list = [] | |
| def build_users(): | |
| input = open("./user_relation.txt", "r") | |
| while True: | |
| line = input.readline() | |
| if not line: break | |
| p = line.split(' ') | |
| uid = int(p[0]) | |
| u = int(p[1]) | |
| cnt = int(p[2]) | |
| if cnt >= 10: | |
| group[uid] = 1 | |
| group_list.append(uid) | |
| users[u]=uid | |
| input.close() | |
| output = open("./group10.txt", "w") | |
| group_list.sort() | |
| for i in range(0, len(group_list)): | |
| output.write(str(group_list[i])+"\n") | |
| output.close() | |
| def build_trust(): | |
| input = open("./trust.txt", "r") | |
| while True: | |
| line = input.readline() | |
| if not line: break | |
| p = line.split('\t') | |
| u = users[int(p[0])] | |
| v = users[int(p[1])] | |
| t = int(p[2]) | |
| trust[(u,v)] = t | |
| input.close() | |
| # 훈련에 사용할 fr 값 | |
| def generate_train(): | |
| input = open("./fr_given.txt", "r") | |
| output = open("./fr_train.txt", "w") | |
| train_fr = defaultdict(lambda: 0) | |
| # 원래 주어진 fr값 채우기 | |
| while True: | |
| line = input.readline() | |
| if not line: break | |
| p = line.split(' ') | |
| u = users[int(p[0])] | |
| v = users[int(p[1])] | |
| r = float(p[2]) | |
| if u in group and v in group and (u,v) in trust: | |
| train_fr[(u, v)] += r | |
| # trust 관계로 train_fr에 추가하기 | |
| for key, value in trust.items(): | |
| u = key[0] | |
| v = key[1] | |
| t = value | |
| if u in group and v in group: | |
| if t == 1: | |
| if not (u, v) in train_fr: | |
| train_fr[(u, v)] = 0.85 | |
| else: | |
| if not (u, v) in train_fr: | |
| train_fr[(u, v)] = 0 | |
| # 출력하기 | |
| for key, value in train_fr.items(): | |
| u = key[0] | |
| v = key[1] | |
| r = value | |
| if u in group and v in group: | |
| output.write(str(u) + "," + str(v) + "," + str(r) + "\n") | |
| input.close() | |
| output.close() | |
| build_users() | |
| build_trust() | |
| generate_train() | |
| print('finish') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment