File size: 1,721 Bytes
89cd787
 
 
 
 
 
 
 
 
 
5dedc64
89cd787
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5dedc64
89cd787
 
 
 
 
5dedc64
 
89cd787
 
 
 
 
 
 
 
 
5dedc64
89cd787
 
 
 
 
 
 
 
 
 
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
from pywiki_custom import *
from urllib.parse import unquote

import io
import csv
import random

lang = "de"
n = 1100

# removing duplicates
def cleandata():
    input_file = f'data_{lang}.csv'
    output_file = f'{lang}-wikihow-qa-dataset-1k.csv'

    unique_urls = {}

    with open(input_file, 'r') as f_input, open(output_file, 'w', newline='') as f_output:
        csv_input = csv.reader(f_input)
        csv_output = csv.writer(f_output)

        header = next(csv_input)
        header[3] = 'METADATA'
        csv_output.writerow(header)

        for row in csv_input:
            url = row[3]
            if url not in unique_urls:
                row[3] = f'{{"url": "{url}", "language": "{lang}"}}'
                csv_output.writerow(row)
                unique_urls[url] = True
            else:
                print(f"Duplicate row found, url: {url}")
    print("Done!")

# getting a random article
def getrandom():
    how_to = RandomHowTo(lang)
    wkhowto_url = how_to.url

    theme = unquote(how_to.title.encode('utf-8'))

    # randomization
    templates = [
    "Wie mache ich {theme}?",
    "Schreibe wie ich folgendes tun kann: {theme}"
    ]

    wkhowto_q = random.choice(templates).format(theme=theme)
    wkhowto_a = how_to.print(extended=True)
    return wkhowto_q, wkhowto_a, wkhowto_url

# saving .csv
with open(f'data_{lang}.csv', mode='w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['INSTRUCTION', 'RESPONSE', 'SOURCE', 'URL'])
    for i in range(n):
        wkhowto_q, wkhowto_a, wkhowto_url = getrandom()
        data = [wkhowto_q, wkhowto_a, f'{lang}.wikihow.com', wkhowto_url]
        writer.writerow(data)
        print(f"{i+1} out of {n}")

cleandata()