-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from h-peng17/master
Use anchor link instead of alias to get wikidata item id.
- Loading branch information
Showing
5 changed files
with
109 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import requests | ||
import sys | ||
import json | ||
import os | ||
from multiprocessing import Pool, Lock | ||
from nltk.tokenize import sent_tokenize | ||
import math | ||
import pdb | ||
from tqdm import tqdm | ||
|
||
|
||
anchors = json.load(open("pretrain_data/all_anchors_name.json")) | ||
part = int(math.ceil(len(anchors) / 256.)) # IMPORTANT! This number must be consistant with workers' number | ||
anchors = [anchors[i:i+part] for i in range(0, len(anchors), part)] | ||
print(len(anchors)) | ||
|
||
def run_proc(idx, n, input_names): | ||
folder = "pretrain_data/anchor" | ||
target = "{}/{}".format(folder, idx) | ||
fout = open(target+"_anchor2id", "a+") | ||
for input_name in tqdm(input_names): | ||
try: | ||
entity_url = f"https://en.wikipedia.org/w/api.php?action=query&prop=pageprops&ppprop=wikibase_item&redirects=1&titles={input_name}&format=json" | ||
entity_info = requests.get(entity_url) | ||
id = list(entity_info.json()['query']['pages'].items())[0][1]['pageprops']['wikibase_item'] | ||
except: | ||
id = '#UNK#' | ||
fout.write(f"{input_name}\t{id}\n") | ||
fout.close() | ||
|
||
folder = "pretrain_data/anchor" | ||
if not os.path.exists(folder): | ||
os.makedirs(folder) | ||
|
||
n = int(sys.argv[1]) | ||
p = Pool(n) | ||
for i in range(n): | ||
p.apply_async(run_proc, args=(i, n, anchors[i])) | ||
p.close() | ||
p.join() | ||
|
||
|
||
|
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import os | ||
import sys | ||
import json | ||
from collections import Counter | ||
from tqdm import tqdm, trange | ||
|
||
def get_all_anchors_name(): | ||
input_folder = "pretrain_data/ann" | ||
file_list = [] | ||
for path, _, filenames in os.walk(input_folder): | ||
for filename in filenames: | ||
file_list.append(os.path.join(path, filename)) | ||
|
||
anchors = {} | ||
for i in trange(len(file_list)): | ||
input_name = file_list[i] | ||
fin = open(input_name, "r") | ||
for doc in fin.readlines(): | ||
doc = doc.strip() | ||
segs = doc.split("[_end_]") | ||
map_segs = segs[1:] | ||
maps = {} | ||
for x in map_segs: | ||
v = x.split("[_map_]") | ||
if len(v) != 2: | ||
continue | ||
if anchors.get(v[1], -1) != -1: | ||
continue | ||
anchors[v[1]] = 1 | ||
fin.close() | ||
print(len(anchors)) | ||
json.dump(list(anchors.keys()), open("pretrain_data/all_anchors_name.json", 'w')) | ||
|
||
def aggregate_anchor2id(): | ||
fout = open("anchor2id.txt", 'w') | ||
files = os.listdir("pretrain_data/anchor") | ||
for file in files: | ||
f = open(os.path.join("pretrain_data/anchor", file)) | ||
fout.write(f.read()) | ||
f.close() | ||
fout.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
mode = sys.argv[1] | ||
if mode == 'get_anchors': | ||
get_all_anchors_name() | ||
elif mode == 'agg_anchors': | ||
aggregate_anchor2id() | ||
|
||
|