File size: 6,608 Bytes
54efb7e
 
 
 
 
 
 
 
 
 
 
 
a581ece
 
54efb7e
 
 
 
 
 
 
 
 
 
 
 
4365a98
 
 
 
 
 
 
 
54efb7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed95795
54efb7e
 
 
 
 
 
 
 
 
 
ed95795
54efb7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed95795
54efb7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a581ece
 
54efb7e
 
 
a581ece
 
ed95795
a581ece
54efb7e
a581ece
54efb7e
 
4365a98
a581ece
54efb7e
4365a98
 
 
 
 
 
 
 
 
 
 
a581ece
 
 
 
54efb7e
a581ece
4365a98
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from dataclasses import dataclass, field, fields
from functools import lru_cache
from xml.etree import ElementTree
from datetime import datetime
from enum import Enum
import typing
from typing import List, Optional, Union
import os.path
from itertools import groupby
import dataclasses
from tqdm import tqdm
from bs4 import BeautifulSoup
import sys
from pathlib import Path

"""
Author: E.W.Ayers

This code takes a dump of math overflow XML and produces
a structured set of questions with answers.

1. Get mathoverflow.net.7z file
2. Extract this to `DATA_DIR = 'data/mathoverflow.net'`
3. Run `questions()` and run it to get a dictionary of mathoverflow questions.
   Each question has an `Answers` field that contains a list of answers for the given q.
"""
def batch_loader(seq, size):
    """
    Iterator that takes in a list `seq` and returns
    chunks of size `size` 
    """
    return [seq[pos:pos + size] for pos in range(0, len(seq), size)]

DOC_SEP = "<|endoftext|>"

# source: https://meta.stackexchange.com/questions/2677/database-schema-documentation-for-the-public-data-dump-and-sede
class PostType(Enum):
    Question = 1
    Answer = 2
    OrphanedTagWiki = 3
    TagWikiExcerpt = 4
    TagWiki = 5
    ModeratorNomination = 6
    WikiPlaceholder = 7
    PrivilegeWiki = 8

def is_optional(field):
    return typing.get_origin(field) is Union and type(None) in typing.get_args(field)

def fromXML(cls, element):
    out = {}
    for field in fields(cls):
        field_key = field.name
        field_type = field.type
        f = field.metadata.get('from_xml')
        if f == 'skip':
            continue
        attr_key = f['key'] if (f is not None and f['key'] is not None) else field_key
        v = element.attrib.get(attr_key)
        if v is None:
            if field.default is not dataclasses.MISSING:
                out[field_key] = field.default
            elif field.default_factory is not dataclasses.MISSING:
                out[field_key] = field.default_factory()  # type: ignore
            elif is_optional(field_type):
                out[field_key] = None
            else:
                raise Exception(f"Missing field {attr_key}")
            continue
        if is_optional(field_type):
            field_type = typing.get_args(field_type)[0]
        if f is not None and f['fn'] is not None:
            out[field_key] = f['fn'](v)
        elif field_type is int:
            out[field_key] = int(v)
        elif field_type is str:
            out[field_key] = str(v)
        elif field_type is datetime:
            out[field_key] = datetime.fromisoformat(v)
        else:
            raise Exception(f"Don't know how to decode {field_type}")
    return cls(**out)

def use(fn, key=None):
    return field(metadata={'from_xml': {'fn': fn, 'key': key}})

def skip(default):
    return field(default=default, metadata={'from_xml': 'skip'})

def iter_rows(path):
    for [_, element] in ElementTree.iterparse(path, events = ['start']):
        if (element.tag == 'row'):
            yield element

DATA_DIR = 'nothing'

@dataclass
class Comment:
    Id: int
    PostId: int
    Score: int
    Text: str
    CreationDate: datetime
    UserId: Optional[int]

#lru_cache()
def comments():
    path = os.path.join(DATA_DIR, 'Comments.xml')
    out = {}
    for element in iter_rows(path):
        x : Comment = fromXML(Comment, element)
        out[x.Id] = x
    print(f"Processed {len(out)} comments.")
    return out

@dataclass
class Post:
    Id: int
    CreationDate: datetime
    DeletionDate: Optional[datetime]
    Score: int
    Body: str # in html; need to parse out?
    Title: Optional[str]
    OwnerUserId: Optional[int]
    ViewCount: Optional[int]
    AcceptedAnswerId: Optional[int]
    ParentId: Optional[int]
    PostType: "PostType" = use(lambda x: PostType(int(x)), 'PostTypeId')
    Comments: List[Comment] = skip(None)
    Answers: Optional[List["Post"]] = skip(None)
    Tags: str = field(default="")

#@lru_cache()
def questions():
    path = os.path.join(DATA_DIR, 'Posts.xml')
    cs = {}
    for k, c in groupby(comments().values(), lambda c: c.PostId):
        x = list(c)
        x.sort(key =  lambda x: -x.Score)
        cs[k] = x
    qs = {}
    answers = {}
    for element in iter_rows(path):
        post = fromXML(Post, element)
        post.Comments = cs.get(post.Id, [])
        if (post.PostType is PostType.Question):
            post.Answers = []
            qs[post.Id] = post
        elif (post.PostType is PostType.Answer):
            answers[post.Id] = post
    for qk, aa in groupby(answers.values(), lambda a: a.ParentId):
        x = list(aa)
        x.sort(key = lambda x: -x.Score)
        qs[qk].Answers = x
    print(f"Processed {len(qs)} questions with {len(answers)} answers.")
    return qs

def strip_html(string): 
    soup = BeautifulSoup(string, 'html.parser')
    return soup.get_text()

def text_of_post(post): 
    text = ""
    if post.Title: 
        text += "TITLE: " + post.Title

    text += f"\nQUESTION [{post.Score} upvotes]: {strip_html(post.Body).strip()}"
    
    commented = False 

    answered = False 
    for answer in post.Answers: 
        if answer.Score >= 2: 
            answered = True 
            text += f"\n\nREPLY [{answer.Score} votes]: {strip_html(answer.Body).strip()}"
            
    return text, post.Score, post.Id, answered

def get_and_format(url, save_dir):
    Path(save_dir).mkdir(exist_ok=True, parents=True)
    archive_path = os.path.join(save_dir, "archive.7z")
    os.system(f"wget -O {archive_path} {url}")

    global DATA_DIR
    DATA_DIR = os.path.join(save_dir, "xml")
    print(f"DATA DIR {DATA_DIR}")
    os.system(f"7z e {archive_path} -o{DATA_DIR}")

    print("parsing xml...")
    qs = questions()

    print("converting xml to text...")
    qs_texts = [text_of_post(qs[key]) for key in tqdm(qs.keys())]

    batches = batch_loader(qs_texts, 5000)
    
    for i, batch in tqdm(enumerate(batches)): 
        shard_path = os.path.join(save_dir, f"shard_{i}.txt")

        to_cat = [post for post, score, _, answered in batch
                if score >=5 and answered]
        shard = f"{DOC_SEP}\n".join(to_cat)

        with open(shard_path, "w") as f: 
            f.write(shard)

    os.system(f"rm -r {DATA_DIR}")
    os.remove(archive_path)

if __name__ == '__main__':
    get_and_format("https://archive.org/download/stackexchange/mathoverflow.net.7z", 
            "stack-exchange/math_overflow")
    get_and_format("https://archive.org/download/stackexchange/math.stackexchange.com.7z", 
            "stack-exchange/math_stack_exchange")