File size: 2,150 Bytes
194af0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2db8cc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194af0e
2db8cc2
194af0e
 
 
 
 
 
 
 
 
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
# Parses raw semcor into csv files
import pandas as pd
import os
from bs4 import BeautifulSoup


def process_split(split_name, parent_path="semcor3.0"):
    data = []
    for file in os.listdir(os.path.join(parent_path, split_name, "tagfiles")):
        file_path = os.path.join(parent_path, split_name, "tagfiles", file)
        with open(file_path, "r") as f:
            raw_file = f.read()
        parsed_file = BeautifulSoup(raw_file, "html.parser")
        for p in parsed_file.contextfile.context.find_all("p"):
            pnum = p.get("pnum")
            for s in p.find_all("s"):
                snum = s.get("snum")
                for child in s.find_all(text=False):
                    child_data = {
                        "tagfile": file,
                        "pnum": pnum,
                        "snum": snum,
                        "tag": child.name,
                        "lemma": child.get("lemma"),
                        "lexsn": child.get("lexsn"),
                        "wnsn": child.get("wnsn"),
                        "value": child.string,
                        "cmd": child.get("cmd"),
                        "dc": child.get("dc"),
                        "ot": child.get("ot"),
                        "pn": child.get("pn"),
                        "pos": child.get("pos"),
                        "rdf": child.get("rdf"),
                        "sep": child.get("sep"),
                    }
                    data.append(child_data)
    types_dict = {
        "tagfile": str,
        "pnum": int,
        "snum": int,
        "tag": str,
        "lemma": str,
        "lexsn": str,
        "wnsn": str,
        "value": str,
        "cmd": str,
        "dc": str,
        "ot": str,
        "pn": str,
        "pos": str,
        "rdf": str,
        "sep": str,
    }
    df = pd.DataFrame(data)
    df = df.astype(types_dict)
    return df


if __name__ == "__main__":
    for split in ["brown1", "brown2", "brownv"]:
        print(f"processing split {split}")
        df = process_split(split)
        df.to_csv(f"data/{split}-00000-of-00001.csv", index=False)
        print("Done. Saved to disk.")