SemCor / parse.py
thesofakillers's picture
handle column types
2db8cc2
raw
history blame contribute delete
No virus
2.15 kB
# 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.")