forked from stanfordnlp/dspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
42 lines (37 loc) · 1.48 KB
/
utils.py
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
import regex as re
import os
import nltk
nltk.download('punkt')
from nltk.tokenize import sent_tokenize
def extract_text_by_citation(paragraph):
citation_regex = re.compile(r'(.*?)(\[\d+\]\.)', re.DOTALL)
parts_with_citation = citation_regex.findall(paragraph)
citation_dict = {}
for part, citation in parts_with_citation:
part = part.strip()
citation_num = re.search(r'\[(\d+)\]\.', citation).group(1)
citation_dict.setdefault(str(int(citation_num) - 1), []).append(part)
return citation_dict
def correct_citation_format(paragraph):
modified_sentences = []
sentences = sent_tokenize(paragraph)
for sentence in sentences:
modified_sentences.append(sentence)
citation_regex = re.compile(r'\[\d+\]\.')
i = 0
if len(modified_sentences) == 1:
has_citation = bool(citation_regex.search(modified_sentences[i]))
while i < len(modified_sentences):
if len(modified_sentences[i:i+2]) == 2:
sentence_group = " ".join(modified_sentences[i:i+2])
has_citation = bool(citation_regex.search(sentence_group))
if not has_citation:
return False
i += 2 if has_citation and i+1 < len(modified_sentences) and citation_regex.search(modified_sentences[i+1]) else 1
else:
return True
return True
def has_citations(paragraph):
return bool(re.search(r'\[\d+\]\.', paragraph))
def citations_check(paragraph):
return has_citations(paragraph) and correct_citation_format(paragraph)