Created
February 27, 2018 17:07
-
-
Save Ladsgroup/cdc6d04d8d737453a1f67182039fd331 to your computer and use it in GitHub Desktop.
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
# License: GPL2+ | |
import pywikibot | |
import json | |
import random | |
import string | |
import numpy as np | |
import math | |
site = pywikibot.Site('en', 'lexeme') | |
distro = np.random.normal(10, 5, 550) | |
distro_list = [math.ceil(abs(i)) for i in distro] | |
def request(params): | |
if not site.logged_in(): | |
site.login() | |
req = pywikibot.data.api.Request(site=site, **params) | |
return req.submit() | |
def get_token(): | |
if not site.logged_in(): | |
site.login() | |
return site.tokens['csrf'] | |
def make_lexeme(): | |
params = { | |
'action': 'wbeditentity', | |
'new': 'lexeme', | |
'data': json.dumps({"lemmas": {"en": {"language": "en", "value": ''.join(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase + ' ') for _ in range(20))} },"lexicalCategory": "Q2", "language": "Q3" }), | |
'token': get_token() | |
} | |
res = request(params) | |
if 'success' in res: | |
print(res['entity']['id']) | |
def new_guid(): | |
return '{:04X}{:04X}-{:04X}-{:04X}-{:04X}-{:04X}{:04X}{:04X}'.format( | |
random.randint(0, 65535), | |
random.randint(0, 65535), | |
random.randint(0, 65535), | |
random.randint(16384, 20479), | |
random.randint(32768, 49151), | |
random.randint(0, 65535), | |
random.randint(0, 65535), | |
random.randint(0, 65535) | |
) | |
def add_statements_to_lexeme(lexeme_id): | |
params = { | |
'action': 'wbsetclaim', | |
'claim': json.dumps( | |
{"type":"statement","mainsnak":{"snaktype":"value","property":"P17","datavalue":{"type":"wikibase-entityid","value":{"id":"L%d" % random.randint(55,545)}}},"id":"%s$%s" % (lexeme_id, new_guid()),"rank":"normal"} | |
), | |
'token': get_token() | |
} | |
res = request(params) | |
if 'success' in res: | |
print(lexeme_id, res['claim']['id']) | |
#for i in range(460): | |
# try: | |
# make_lexeme() | |
# except: | |
# continue | |
for i in range(55, 550): | |
for j in range(distro_list[i-54]): | |
try: | |
add_statements_to_lexeme('L%d' % i) | |
except: | |
try: | |
add_statements_to_lexeme('L%d' % i) | |
except: | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment