From 26ea272114a7512421f425c6f2436eeb8316c3c1 Mon Sep 17 00:00:00 2001 From: groceryheist Date: Tue, 11 Dec 2018 19:36:49 -0800 Subject: [PATCH] wikiq mostly functional, but reverters take all the credit for the content they restore. --- test/Wikiq_Unit_Test.py | 424 +++++++++++++++++++++++----------------- wikiq | 36 +++- 2 files changed, 266 insertions(+), 194 deletions(-) diff --git a/test/Wikiq_Unit_Test.py b/test/Wikiq_Unit_Test.py index e893718..5e6f51b 100644 --- a/test/Wikiq_Unit_Test.py +++ b/test/Wikiq_Unit_Test.py @@ -10,49 +10,105 @@ from io import StringIO # with / without url encode DONE # with / without collapse user DONE # with output to sdtout DONE -# note that the persistence radius is 7 by default +# note that the persistence radius is 7 by default # reading various file formats including # 7z, gz, bz2, xml DONE # wikia and wikipedia data DONE # malformed xmls DONE -# class Test_Persistence_Bug(unittest.TestCase): +class Test_Persistence(unittest.TestCase): -# def setUp(self): -# if not os.path.exists("test_output"): -# os.mkdir("test_output") + def setUp(self): + if not os.path.exists("test_output"): + os.mkdir("test_output") + + self.wiki = 'pwr-test' + self.wikiq_out_name = self.wiki + ".tsv" + self.test_output_dir = os.path.join(".", "test_output") + self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name) + + self.infile = "{0}.xml".format(self.wiki) + self.base_call = "../wikiq {0} -o {1}" + self.input_dir = "dumps" + self.input_file = os.path.join(".", self.input_dir,self.infile) + self.baseline_output_dir = "baseline_output" -# self.wiki = 'enwiki-test' -# self.wikiq_out_name = self.wiki + ".tsv" -# self.test_output_dir = os.path.join(".", "test_output") -# self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name) + def test_sequence_persistence(self): + test_filename = "sequence-" + self.wikiq_out_name + test_file = os.path.join(self.test_output_dir, test_filename) + if os.path.exists(test_file): + os.remove(test_file) + + call = self.base_call.format(self.input_file, self.test_output_dir) + call = call + " --url-encode --persistence sequence --collapse-user" + print(os.path.abspath('.')) + print(call) + proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) + proc.wait() -# self.infile = "{0}.xml".format(self.wiki) -# self.base_call = "../wikiq {0} -o {1}" -# self.input_dir = "dumps" -# self.input_file = os.path.join(".", self.input_dir,self.infile) -# self.baseline_output_dir = "baseline_output" + copyfile(self.call_output, test_file) + baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) + + # as a test let's make sure that we get equal data frames + test = pd.read_table(test_file) + self.assertEqual(test['tokens_added'][0],4) + self.assertEqual(test['tokens_added'][1],5) + self.assertEqual(test['tokens_added'][2],0) + self.assertEqual(test['tokens_added'][3],6) + self.assertEqual(test['tokens_added'][4],4) + self.assertEqual(test['tokens_removed'][0],0) + self.assertEqual(test['tokens_removed'][1],0) + self.assertEqual(test['tokens_removed'][2],5) + self.assertEqual(test['tokens_removed'][3],4) + self.assertEqual(test['tokens_removed'][4],6) + self.assertEqual(test['token_revs'][0],4*3) + self.assertEqual(test['token_revs'][1],0) + self.assertEqual(test['token_revs'][2],0) + self.assertEqual(test['token_revs'][3],0) + self.assertEqual(test['token_revs'][4],0) + + baseline = pd.read_table(baseline_file) + assert_frame_equal(test,baseline) + + + +class Test_Persistence_Bug(unittest.TestCase): + + def setUp(self): + if not os.path.exists("test_output"): + os.mkdir("test_output") + + self.wiki = 'enwiki-test' + self.wikiq_out_name = self.wiki + ".tsv" + self.test_output_dir = os.path.join(".", "test_output") + self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name) + + self.infile = "{0}.xml".format(self.wiki) + self.base_call = "../wikiq {0} -o {1}" + self.input_dir = "dumps" + self.input_file = os.path.join(".", self.input_dir,self.infile) + self.baseline_output_dir = "baseline_output" -# def test_segment_persistence(self): -# test_filename = "sequence-" + self.wikiq_out_name -# test_file = os.path.join(self.test_output_dir, test_filename) -# if os.path.exists(test_file): -# os.remove(test_file) + def test_sequence_persistence(self): + test_filename = "sequence-" + self.wikiq_out_name + test_file = os.path.join(self.test_output_dir, test_filename) + if os.path.exists(test_file): + os.remove(test_file) -# call = self.base_call.format(self.input_file, self.test_output_dir) -# call = call + " --url-encode --persistence sequence --collapse-user" -# print(os.path.abspath('.')) -# print(call) -# proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) -# proc.wait() + call = self.base_call.format(self.input_file, self.test_output_dir) + call = call + " --url-encode --persistence sequence --collapse-user" + print(os.path.abspath('.')) + print(call) + proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) + proc.wait() -# copyfile(self.call_output, test_file) -# baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) + copyfile(self.call_output, test_file) + baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) -# # as a test let's make sure that we get equal data frames -# test = pd.read_table(test_file) -# baseline = pd.read_table(baseline_file) -# assert_frame_equal(test,baseline) + # as a test let's make sure that we get equal data frames + test = pd.read_table(test_file) + baseline = pd.read_table(baseline_file) + assert_frame_equal(test,baseline) class Test_Wikipedia(unittest.TestCase): @@ -85,7 +141,7 @@ class Test_Wikipedia(unittest.TestCase): copyfile(self.call_output, test_file) baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) - # as a test let's make sure that we get equal data frames +# as a test let's make sure that we get equal data frames test = pd.read_table(test_file) baseline = pd.read_table(baseline_file) @@ -103,10 +159,10 @@ class Test_Wikipedia(unittest.TestCase): print(call) proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) proc.wait() -# copyfile(self.call_output, test_file) + copyfile(self.call_output, test_file) baseline_file = os.path.join(os.path.abspath("."), self.baseline_output_dir, test_filename) - # as a test let's make sure that we get equal data frames +# as a test let's make sure that we get equal data frames test = pd.read_table(test_file) num_wrong_ns = sum(~ test.namespace.isin({0,1})) self.assertEqual(num_wrong_ns, 0) @@ -114,192 +170,192 @@ class Test_Wikipedia(unittest.TestCase): assert_frame_equal(test,baseline) -# class Test_Basic(unittest.TestCase): +class Test_Basic(unittest.TestCase): -# def setUp(self): -# if not os.path.exists("test_output"): -# os.mkdir("test_output") + def setUp(self): + if not os.path.exists("test_output"): + os.mkdir("test_output") -# self.wiki = 'sailormoon' -# self.wikiq_out_name = self.wiki + ".tsv" -# self.test_output_dir = os.path.join(".", "test_output") -# self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name) + self.wiki = 'sailormoon' + self.wikiq_out_name = self.wiki + ".tsv" + self.test_output_dir = os.path.join(".", "test_output") + self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name) -# self.infile = "{0}.xml.7z".format(self.wiki) -# self.base_call = "../wikiq {0} -o {1}" -# self.input_dir = "dumps" -# self.input_file = os.path.join(".", self.input_dir,self.infile) -# self.baseline_output_dir = "baseline_output" + self.infile = "{0}.xml.7z".format(self.wiki) + self.base_call = "../wikiq {0} -o {1}" + self.input_dir = "dumps" + self.input_file = os.path.join(".", self.input_dir,self.infile) + self.baseline_output_dir = "baseline_output" -# def test_noargs(self): + def test_noargs(self): -# test_filename = "noargs_" + self.wikiq_out_name -# test_file = os.path.join(self.test_output_dir, test_filename) -# if os.path.exists(test_file): -# os.remove(test_file) + test_filename = "noargs_" + self.wikiq_out_name + test_file = os.path.join(self.test_output_dir, test_filename) + if os.path.exists(test_file): + os.remove(test_file) -# call = self.base_call.format(self.input_file, self.test_output_dir) -# proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) -# proc.wait() + call = self.base_call.format(self.input_file, self.test_output_dir) + proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) + proc.wait() -# copyfile(self.call_output, test_file) + copyfile(self.call_output, test_file) -# baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) + baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) -# test = pd.read_table(test_file) -# baseline = pd.read_table(baseline_file) -# assert_frame_equal(test,baseline) + test = pd.read_table(test_file) + baseline = pd.read_table(baseline_file) + assert_frame_equal(test,baseline) -# def test_collapse_user(self): -# test_filename = "collapse-user_" + self.wikiq_out_name -# test_file = os.path.join(self.test_output_dir, test_filename) -# if os.path.exists(test_file): -# os.remove(test_file) + def test_collapse_user(self): + test_filename = "collapse-user_" + self.wikiq_out_name + test_file = os.path.join(self.test_output_dir, test_filename) + if os.path.exists(test_file): + os.remove(test_file) -# call = self.base_call.format(self.input_file, self.test_output_dir) -# call = call + " --collapse-user" + call = self.base_call.format(self.input_file, self.test_output_dir) + call = call + " --collapse-user" -# proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) -# proc.wait() + proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) + proc.wait() -# copyfile(self.call_output, test_file) + copyfile(self.call_output, test_file) -# baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) -# test = pd.read_table(test_file) -# baseline = pd.read_table(baseline_file) -# assert_frame_equal(test,baseline) + baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) + test = pd.read_table(test_file) + baseline = pd.read_table(baseline_file) + assert_frame_equal(test,baseline) -# def test_pwr_segment(self): -# test_filename = "persistence_segment_" + self.wikiq_out_name -# test_file = os.path.join(self.test_output_dir, test_filename) -# if os.path.exists(test_file): -# os.remove(test_file) + def test_pwr_segment(self): + test_filename = "persistence_segment_" + self.wikiq_out_name + test_file = os.path.join(self.test_output_dir, test_filename) + if os.path.exists(test_file): + os.remove(test_file) -# call = self.base_call.format(self.input_file, self.test_output_dir) -# call = call + " --persistence segment" -# print(call) -# proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) -# proc.wait() + call = self.base_call.format(self.input_file, self.test_output_dir) + call = call + " --persistence segment" + print(call) + proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) + proc.wait() -# copyfile(self.call_output, test_file) + copyfile(self.call_output, test_file) -# baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) + baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) -# test = pd.read_table(test_file) -# print(test) -# baseline = pd.read_table(baseline_file) -# assert_frame_equal(test,baseline) + test = pd.read_table(test_file) + print(test) + baseline = pd.read_table(baseline_file) + assert_frame_equal(test,baseline) -# def test_pwr_legacy(self): -# test_filename = "persistence_legacy_" + self.wikiq_out_name -# test_file = os.path.join(self.test_output_dir, test_filename) -# if os.path.exists(test_file): -# os.remove(test_file) + def test_pwr_legacy(self): + test_filename = "persistence_legacy_" + self.wikiq_out_name + test_file = os.path.join(self.test_output_dir, test_filename) + if os.path.exists(test_file): + os.remove(test_file) -# call = self.base_call.format(self.input_file, self.test_output_dir) -# call = call + " --persistence legacy" -# proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) -# proc.wait() + call = self.base_call.format(self.input_file, self.test_output_dir) + call = call + " --persistence legacy" + proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) + proc.wait() -# copyfile(self.call_output, test_file) + copyfile(self.call_output, test_file) -# baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) + baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) -# test = pd.read_table(test_file) -# baseline = pd.read_table(baseline_file) -# assert_frame_equal(test,baseline) + test = pd.read_table(test_file) + baseline = pd.read_table(baseline_file) + assert_frame_equal(test,baseline) -# def test_pwr(self): -# test_filename = "persistence_" + self.wikiq_out_name -# test_file = os.path.join(self.test_output_dir, test_filename) -# if os.path.exists(test_file): -# os.remove(test_file) + def test_pwr(self): + test_filename = "persistence_" + self.wikiq_out_name + test_file = os.path.join(self.test_output_dir, test_filename) + if os.path.exists(test_file): + os.remove(test_file) -# call = self.base_call.format(self.input_file, self.test_output_dir) -# call = call + " --persistence" -# proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) -# proc.wait() + call = self.base_call.format(self.input_file, self.test_output_dir) + call = call + " --persistence" + proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) + proc.wait() -# copyfile(self.call_output, test_file) + copyfile(self.call_output, test_file) -# baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) + baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) -# test = pd.read_table(test_file) -# baseline = pd.read_table(baseline_file) -# assert_frame_equal(test,baseline) + test = pd.read_table(test_file) + baseline = pd.read_table(baseline_file) + assert_frame_equal(test,baseline) -# def test_url_encode(self): -# test_filename = "url-encode_" + self.wikiq_out_name + def test_url_encode(self): + test_filename = "url-encode_" + self.wikiq_out_name -# test_file = os.path.join(self.test_output_dir, test_filename) -# if os.path.exists(test_file): -# os.remove(test_file) + test_file = os.path.join(self.test_output_dir, test_filename) + if os.path.exists(test_file): + os.remove(test_file) -# call = self.base_call.format(self.input_file, self.test_output_dir) -# call = call + " --url-encode" -# proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) -# proc.wait() - -# copyfile(self.call_output, test_file) -# baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) -# test = pd.read_table(test_file) -# baseline = pd.read_table(baseline_file) -# assert_frame_equal(test,baseline) - - -# class Test_Malformed(unittest.TestCase): -# def setUp(self): -# if not os.path.exists("test_output"): -# os.mkdir("test_output") - -# self.wiki = 'twinpeaks' -# self.wikiq_out_name = self.wiki + ".tsv" -# self.test_output_dir = os.path.join(".", "test_output") -# self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name) - -# self.infile = "{0}.xml.7z".format(self.wiki) -# self.base_call = "../wikiq {0} -o {1}" -# self.input_dir = "dumps" -# self.input_file = os.path.join(".", self.input_dir,self.infile) - - -# def test_malformed_noargs(self): - -# call = self.base_call.format(self.input_file, self.test_output_dir) -# proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True) -# proc.wait() -# outs, errs = proc.communicate() -# errlines = str(errs).split("\\n") -# self.assertEqual(errlines[-2],'xml.etree.ElementTree.ParseError: no element found: line 1369, column 0') - -# class Test_Stdout(unittest.TestCase): - -# def setUp(self): -# self.wiki = 'sailormoon' -# self.wikiq_out_name = self.wiki + ".tsv" - -# self.infile = "{0}.xml.7z".format(self.wiki) -# self.base_call = "../wikiq {0} --stdout" -# self.input_dir = "dumps" -# self.input_file = os.path.join(".", self.input_dir,self.infile) -# self.baseline_output_dir = "baseline_output" - -# def test_noargs(self): - -# call = self.base_call.format(self.input_file) -# proc = subprocess.run(call,stdout=subprocess.PIPE,shell=True) -# outs = proc.stdout.decode("utf8") - -# test_file = "noargs_" + self.wikiq_out_name -# baseline_file = os.path.join(".", self.baseline_output_dir, test_file) -# print(baseline_file) -# test = pd.read_table(StringIO(outs)) -# baseline = pd.read_table(baseline_file) -# assert_frame_equal(test,baseline) + call = self.base_call.format(self.input_file, self.test_output_dir) + call = call + " --url-encode" + proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True) + proc.wait() + + copyfile(self.call_output, test_file) + baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) + test = pd.read_table(test_file) + baseline = pd.read_table(baseline_file) + assert_frame_equal(test,baseline) + + +class Test_Malformed(unittest.TestCase): + def setUp(self): + if not os.path.exists("test_output"): + os.mkdir("test_output") + + self.wiki = 'twinpeaks' + self.wikiq_out_name = self.wiki + ".tsv" + self.test_output_dir = os.path.join(".", "test_output") + self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name) + + self.infile = "{0}.xml.7z".format(self.wiki) + self.base_call = "../wikiq {0} -o {1}" + self.input_dir = "dumps" + self.input_file = os.path.join(".", self.input_dir,self.infile) + + + def test_malformed_noargs(self): + + call = self.base_call.format(self.input_file, self.test_output_dir) + proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True) + proc.wait() + outs, errs = proc.communicate() + errlines = str(errs).split("\\n") + self.assertEqual(errlines[-2],'xml.etree.ElementTree.ParseError: no element found: line 1369, column 0') + +class Test_Stdout(unittest.TestCase): + + def setUp(self): + self.wiki = 'sailormoon' + self.wikiq_out_name = self.wiki + ".tsv" + + self.infile = "{0}.xml.7z".format(self.wiki) + self.base_call = "../wikiq {0} --stdout" + self.input_dir = "dumps" + self.input_file = os.path.join(".", self.input_dir,self.infile) + self.baseline_output_dir = "baseline_output" + + def test_noargs(self): + + call = self.base_call.format(self.input_file) + proc = subprocess.run(call,stdout=subprocess.PIPE,shell=True) + outs = proc.stdout.decode("utf8") + + test_file = "noargs_" + self.wikiq_out_name + baseline_file = os.path.join(".", self.baseline_output_dir, test_file) + print(baseline_file) + test = pd.read_table(StringIO(outs)) + baseline = pd.read_table(baseline_file) + assert_frame_equal(test,baseline) if __name__ == '__main__': unittest.main() diff --git a/wikiq b/wikiq index f8e5fd6..35cdbc0 100755 --- a/wikiq +++ b/wikiq @@ -1,9 +1,8 @@ - #!/usr/bin/env python3 +#!/usr/bin/env python3 # original wikiq headers are: title articleid revid date_time anon # editor editor_id minor text_size text_entropy text_md5 reversion # additions_size deletions_size -import pdb import argparse import sys import os, os.path @@ -77,9 +76,25 @@ class PersistMethod: segment = 2 legacy = 3 -def calculate_persistence(tokens_added): +def calculate_persistence(tokens_added, tokens_removed, exclude_ws = True, exclude_punct = False): + cond = True + ws_lex = ['break','whitespace'] + punct_lex = ['period','qmark','epoint','comma','colon','scolon','paren_open','paren_close','brack_open','brack_close','dbrack_close','dbrack_open','tab_close','tab_open','dcurly_close','dcurly_open','equals','bar','etc','bold','italic','tag','comment_end','comment_start'] + + + if exclude_ws: + cond = lambda t: cond and not t.type in ws_lex + + if exclude_punct: + cond = lambda t: cond and not t.type in punct_lex + + tokens_added = [t for t in tokens_added if cond(t)] + tokens_removed = [t for t in tokens_removed if cond(t)] + return(sum([(len(x.revisions)-1) for x in tokens_added]), - len(tokens_added)) + len(tokens_added), + len(tokens_removed) + ) class WikiqIterator(Dump): @@ -395,11 +410,11 @@ class WikiqParser(): if len(window) == PERSISTENCE_RADIUS: old_rev_id, old_rev_data, old_tokens_added, old_tokens_removed = window[0] - num_token_revs, num_tokens = calculate_persistence(old_tokens_added) + num_token_revs, num_tokens_added, num_tokens_removed = calculate_persistence(old_tokens_added, old_tokens_removed) old_rev_data["token_revs"] = num_token_revs - old_rev_data["tokens_added"] = num_tokens - old_rev_data["tokens_removed"] = len(old_tokens_removed) + old_rev_data["tokens_added"] = num_tokens_added + old_rev_data["tokens_removed"] = num_tokens_removed old_rev_data["tokens_window"] = PERSISTENCE_RADIUS-1 self.print_rev_data(old_rev_data) @@ -417,11 +432,12 @@ class WikiqParser(): continue rev_id, rev_data, tokens_added, tokens_removed = item - num_token_revs, num_tokens = calculate_persistence(tokens_added) + + num_token_revs, num_tokens_added, num_tokens_removed = calculate_persistence(tokens_added, tokens_removed) rev_data["token_revs"] = num_token_revs - rev_data["tokens_added"] = num_tokens - rev_data["tokens_removed"] = len(tokens_removed) + rev_data["tokens_added"] = num_tokens_added + rev_data["tokens_removed"] = num_tokens_removed rev_data["tokens_window"] = len(window)-(i+1) self.print_rev_data(rev_data) -- 2.39.5