jncraton commited on
Commit
62c7aee
·
1 Parent(s): 554bdd4

Store embeddings and log during run

Browse files
Files changed (1) hide show
  1. parse.py +34 -28
parse.py CHANGED
@@ -1,6 +1,9 @@
1
  import re
2
  import os
 
3
  import xml.etree.ElementTree as ET
 
 
4
 
5
 
6
  def get_refs(root):
@@ -17,10 +20,7 @@ def get_refs(root):
17
  def get_text_content(root, strip_ref=False):
18
  """Return the plain text content of a node
19
 
20
- This will:
21
-
22
- 1. Remove all notes, footnotes, and references
23
- 2. Remove all newlines and combine whitespace to single spaces
24
  """
25
 
26
  # Start with the root text prior to the first node
@@ -34,17 +34,16 @@ def get_text_content(root, strip_ref=False):
34
  # Add the root node text between this child and the next child
35
  text += child.tail
36
 
37
- if strip_ref:
38
- # Remove empty parens from removed references
39
- text = re.sub(r"\([\n\t; ]*\)", " ", text)
40
 
41
- # Combine whitespace to single space
42
- # text = re.sub(r"[\n\t ]+", " ", text)
43
 
44
- return text
 
 
45
 
46
 
47
  def get_paras():
 
48
  paras = []
49
 
50
  from pathlib import Path
@@ -53,7 +52,6 @@ def get_paras():
53
  filename = str(filename)
54
  if "authInfo." in filename:
55
  continue
56
- print(filename)
57
  try:
58
  tree = ET.parse(filename)
59
  except:
@@ -62,31 +60,39 @@ def get_paras():
62
 
63
  root = tree.getroot()
64
 
65
- # for i, p in enumerate(root.findall(".//p[scripRef]")):
66
  for i, p in enumerate(root.findall(".//p")):
 
67
  text = get_text_content(p)
68
 
69
- if False:
70
- text = re.sub(r'".*?"', "...", text, flags=re.M | re.DOTALL)
71
- text = re.sub(r"“.*?”", "...", text, flags=re.M | re.DOTALL)
72
-
73
- # Remove spaces before punctuation
74
- text = re.sub(r"[\t\n ]\.", ".", text)
75
- text = re.sub(r"[\t\n ],", ",", text)
76
-
77
- # Combine elipses
78
- text = re.sub(r"\.\.\.+", "...", text)
79
-
80
- refs = get_refs(p)
81
 
82
- if len(text) > 160 and "id" in p.attrib:
83
- yield {
84
  "id": filename + ":" + p.attrib["id"],
85
- "refs": refs,
86
  "text": text,
87
- "text-noref": get_text_content(p, strip_ref=True),
 
 
88
  }
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  from datasets import Dataset
92
 
 
1
  import re
2
  import os
3
+ import json
4
  import xml.etree.ElementTree as ET
5
+ from languagemodels.embeddings import embed
6
+ from time import perf_counter
7
 
8
 
9
  def get_refs(root):
 
20
  def get_text_content(root, strip_ref=False):
21
  """Return the plain text content of a node
22
 
23
+ This will remove all notes, footnotes, and references
 
 
 
24
  """
25
 
26
  # Start with the root text prior to the first node
 
34
  # Add the root node text between this child and the next child
35
  text += child.tail
36
 
37
+ return text
 
 
38
 
 
 
39
 
40
+ log = open("log.txt", "w")
41
+ jsonl = open("train.jsonl", "w")
42
+ count = 0
43
 
44
 
45
  def get_paras():
46
+ global count
47
  paras = []
48
 
49
  from pathlib import Path
 
52
  filename = str(filename)
53
  if "authInfo." in filename:
54
  continue
 
55
  try:
56
  tree = ET.parse(filename)
57
  except:
 
60
 
61
  root = tree.getroot()
62
 
 
63
  for i, p in enumerate(root.findall(".//p")):
64
+ xml = ET.tostring(p, encoding="unicode")
65
  text = get_text_content(p)
66
 
67
+ if "id" in p.attrib:
68
+ start = perf_counter()
69
+ refs = get_refs(p)
70
+ emb = list(float(n) for n in embed([text])[0])
71
+ count += 1
 
 
 
 
 
 
 
72
 
73
+ row = {
 
74
  "id": filename + ":" + p.attrib["id"],
 
75
  "text": text,
76
+ "thml": xml,
77
+ "refs": refs,
78
+ "all-MiniLM-L6-v2": emb,
79
  }
80
 
81
+ print(
82
+ count,
83
+ f"{perf_counter()-start:.2f}",
84
+ len(row["text"]),
85
+ len(row["thml"]),
86
+ row["id"],
87
+ row["all-MiniLM-L6-v2"][:2],
88
+ file=log,
89
+ flush=True,
90
+ )
91
+
92
+ print(json.dumps(row), file=jsonl, flush=True)
93
+
94
+ yield row
95
+
96
 
97
  from datasets import Dataset
98