<jupyter_start><jupyter_text>Test ML ORES article "Härnösand"<jupyter_code>%matplotlib notebook
import csv
import json
import time
import sys

import requests
import mwapi
import mwtypes
import pandas
import seaborn
session = mwapi.Session("https://en.wikipedia.org", user_agent="<EMAIL> -- Littbank quality")
WEIGHTS = {'Stub': 1, 'Start': 2, 'C': 3, 'B': 4, 'GA': 5, 'FA': 6}
def score2sum(score_doc):
    if not score_doc:
        return None
    weighted_sum = 0
    for cl, proba in score_doc['probability'].items():
        weighted_sum += WEIGHTS[cl] * proba
    return weighted_sum
def fetch_wp10_score(rev_id):
    response = requests.get('https://ores.wikimedia.org/v3/scores/enwiki/{0}/wp10'.format(rev_id))
    try:
        return response.json()['enwiki']['scores'][str(rev_id)]['wp10']['score']
    except:
        return None

def fetch_historical_scores(page_name):
    historical_scores = []
    for response_doc in session.get(action='query', prop='revisions', titles=page_name, 
                                    rvprop=['ids', 'timestamp'], rvlimit=100, rvdir="newer", 
                                    formatversion=2, continuation=True):
        for rev_doc in response_doc['query']['pages'][0]['revisions']:
            rev_id = rev_doc['revid']
            timestamp = rev_doc['timestamp']
            weighted_sum = score2sum(fetch_wp10_score(rev_id))
            historical_scores.append({'rev_id': rev_id, 'timestamp': timestamp, 'weighted_sum': weighted_sum})
            sys.stderr.write(".")
            sys.stderr.flush()
        sys.stderr.write("\n")
    
    return historical_scores
def oresToPandas(title):
    historical_scores = pandas.DataFrame(fetch_historical_scores(title))
    historical_scores['time'] =pandas.to_datetime(historical_scores.timestamp, format='%Y-%m-%dT%H:%M:%SZ',errors='ignore')
    historical_scores = historical_scores.set_index('time')
    historical_scores = historical_scores[['weighted_sum']]
    return historical_scores
data = oresToPandas('Härnösand')
data.info()
data
data.plot()
#for response_doc in session.get(action='query', prop='revisions', titles='Chile', 
#                                    rvprop=['ids', 'timestamp'], rvlimit=100, rvdir="newer", 
#                                    formatversion=2, continuation=True):
#    for rev_doc in response_doc['query']['pages'][0]['revisions']:
#        timestamp = rev_doc['timestamp']
#        print(timestamp)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14147130153597412, 'fraction_numerical': 0.011721907841552142, 'mean_word_length': 3.4594594594594597, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '138463', 'license': 'MIT', 'n_tokens_mistral': 819, 'n_tokens_neox': 772, 'n_words': 144, 'path': 'Notebook/ML ORES quality LItteraturbanken articles in Wikidata.ipynb', 'repo_name': 'salgo60/spa2Commons', 'size': '2482'}