|
| 1 | +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import random |
| 16 | +cate_dict = {} |
| 17 | +sub_cate_dict = {} |
| 18 | +with open("cate_map", "r") as r: |
| 19 | + for l in r: |
| 20 | + line = l.split("\t") |
| 21 | + cate_dict[line[0]] = line[1][:-1] |
| 22 | + |
| 23 | +with open("sub_cate_map", "r") as r: |
| 24 | + for l in r: |
| 25 | + line = l.split("\t") |
| 26 | + sub_cate_dict[line[0]] = line[1][:-1] |
| 27 | + |
| 28 | +print(cate_dict) |
| 29 | +print(sub_cate_dict) |
| 30 | +title_r = [] |
| 31 | +content_r = [] |
| 32 | +index = 0 |
| 33 | +with open("convert_text8/convert_temp.txt", "r") as r1: |
| 34 | + for l in r1: |
| 35 | + if index % 2 == 0: |
| 36 | + title_r.append(l[:-1]) |
| 37 | + else: |
| 38 | + content_r.append(l[:-1]) |
| 39 | + index = index + 1 |
| 40 | +print(index) |
| 41 | +inx = 0 |
| 42 | +article_map = {} |
| 43 | +with open("article.txt", "w") as ar: |
| 44 | + with open("news_backup.tsv", "r") as r2: |
| 45 | + for l in r2: |
| 46 | + line = l.split("\t") |
| 47 | + id, cate, sub_cate = line[:3] |
| 48 | + article_map[id] = len(article_map) |
| 49 | + cate = cate_dict[cate] |
| 50 | + sub_cate = sub_cate_dict[sub_cate] |
| 51 | + title = title_r[inx] |
| 52 | + content = content_r[inx] |
| 53 | + inx = inx + 1 |
| 54 | + ar.write(id + "\t" + cate + "\t" + sub_cate + "\t" + title + "\t" + |
| 55 | + content + "\n") |
| 56 | + |
| 57 | +print(inx) |
| 58 | +print(len(article_map)) |
| 59 | +file_base = ["train", "dev", "test"] |
| 60 | +files = [ |
| 61 | + "train_raw/behaviors.tsv", "dev_raw/behaviors.tsv", |
| 62 | + "test_raw/behaviors.tsv" |
| 63 | +] |
| 64 | +for base, aim in zip(files, file_base): |
| 65 | + with open(aim + "/browse.txt", "w") as w1: |
| 66 | + print("generate " + aim) |
| 67 | + with open(base, "r") as f: |
| 68 | + for l in f: |
| 69 | + line = l.split("\t") |
| 70 | + visit = line[3] |
| 71 | + if len(visit) == 0: |
| 72 | + continue |
| 73 | + sample = line[4].split(" ") |
| 74 | + pos_sample = "" |
| 75 | + neg_sample = "" |
| 76 | + for s in sample: |
| 77 | + if len(s) > 0 and (s[-2:] == "-1" or s[-2:] == "-0"): |
| 78 | + id = s.split("-")[0] |
| 79 | + if id in article_map: |
| 80 | + if s[-2:] == "-1": |
| 81 | + if len(pos_sample) > 0: |
| 82 | + pos_sample = pos_sample + " " |
| 83 | + pos_sample = pos_sample + id |
| 84 | + else: |
| 85 | + if len(neg_sample) > 0: |
| 86 | + neg_sample = neg_sample + " " |
| 87 | + neg_sample = neg_sample + id |
| 88 | + |
| 89 | + if len(pos_sample) == 0 or len(neg_sample) == 0: |
| 90 | + continue |
| 91 | + |
| 92 | + line = visit + "\t" + pos_sample + "\t" + neg_sample + "\n" |
| 93 | + w1.write(line) |
0 commit comments