-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
244 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/harmony/parsing/text_extraction/smart_document_parser.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import re | ||
|
||
import numpy as np | ||
import pandas as pd | ||
from spacy.tokens import Span | ||
from harmony.parsing.text_extraction.sequence_finder import find_longest_uninterrupted_sequence | ||
from harmony.parsing.text_extraction.spacy_wrapper import nlp | ||
from harmony.schemas.requests.text import Question | ||
from harmony.parsing.text_extraction.options_extractor import add_candidate_options | ||
|
||
|
||
def normalise(text): | ||
return re.sub(r'\W', '', text.lower()) | ||
|
||
|
||
def clean_question(text): | ||
return re.sub(r'^\s*(-|\))\s*|\s*(-|\()\s*$', '', re.sub(r'\s+', ' ', text)).strip() | ||
|
||
|
||
def get_question_from_span(question_span): | ||
""" | ||
Get the text of a question, excluding any of the leading or trailing Likert options | ||
:param question_span: | ||
:return: | ||
""" | ||
doc = question_span.doc | ||
tokens_to_include = set(range(question_span.start, question_span.end)) | ||
|
||
# Logic to delete Likert options from end of text | ||
tokens_to_exclude = set() | ||
for option_span in doc.spans['CANDIDATE_OPTION']: | ||
for i in range(option_span.start, option_span.end): | ||
tokens_to_exclude.add(i) | ||
|
||
for i in tokens_to_exclude: | ||
if i + 1 in tokens_to_exclude or i - 1 in tokens_to_exclude: | ||
if i in tokens_to_include: | ||
tokens_to_include.remove(i) | ||
|
||
if len(tokens_to_include) == 0: | ||
return "" | ||
start = question_span.start | ||
end = max(tokens_to_include) + 1 | ||
if start < end: | ||
question_span = doc[start:end] | ||
|
||
return clean_question(question_span.text) | ||
|
||
|
||
def convert_to_dataframe(doc, is_training=False): | ||
df = pd.DataFrame({"span": list(doc.spans['CANDIDATE_QUESTION'])}) | ||
|
||
if is_training: | ||
df["ground_truth"] = df.question.apply(lambda span: span._.ground_truth) | ||
|
||
# df["question"] = df["span"].apply(lambda span: clean_question(span.text)) | ||
df["question"] = df["span"].apply(lambda span: get_question_from_span(span)) | ||
|
||
df["preceding_bullet_value"] = df["span"].apply(lambda span: span._.preceding_bullet_value) | ||
|
||
return df | ||
|
||
|
||
def is_acceptable_span(span: Span) -> bool: | ||
if span.end - span.start < 2: | ||
return False | ||
question = get_question_from_span(span) | ||
non_whitespace_text = re.sub(r'\W', '', question) | ||
if len(non_whitespace_text) < 10: | ||
return False | ||
return True | ||
|
||
|
||
def get_questions(df): | ||
preceding_bullet_values = list(df.preceding_bullet_value) | ||
longest_uninterrupted_sequence = find_longest_uninterrupted_sequence(preceding_bullet_values) | ||
|
||
if longest_uninterrupted_sequence is not None: | ||
is_question_to_include = np.zeros((len(df),), dtype=bool) | ||
for idx, seq_type, value in longest_uninterrupted_sequence: | ||
is_question_to_include[idx] = 1 | ||
df["is_question_to_include"] = is_question_to_include | ||
else: | ||
# df["prediction"] = list(predictions) | ||
# df["is_question_to_include"] = df["prediction"] == 2 | ||
df["is_question_to_include"] = df.span.apply(is_acceptable_span) | ||
|
||
df_pred = df[df["is_question_to_include"]] | ||
df_pred.rename(columns={"preceding_bullet_value": "question_no"}, inplace=True) | ||
|
||
return df_pred | ||
|
||
|
||
def parse_document(text): | ||
doc = nlp(text) | ||
df = convert_to_dataframe(doc) | ||
|
||
df = get_questions(df) | ||
add_candidate_options(df, doc) | ||
|
||
questions = [] | ||
for idx in range(len(df)): | ||
if df.is_question_to_include.iloc[idx]: | ||
options = df.options.iloc[idx] | ||
question = Question(question_no=df.question_no.iloc[idx], question_intro="", question_text=df.question.iloc[idx], options=list(options)) | ||
questions.append(question) | ||
|
||
return questions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import os | ||
import requests | ||
|
||
files = ["11_ner_0_spacy/model-best/config.cfg", | ||
"11_ner_0_spacy/model-best/meta.json", | ||
"11_ner_0_spacy/model-best/ner/cfg", | ||
"11_ner_0_spacy/model-best/ner/model", | ||
"11_ner_0_spacy/model-best/ner/moves", | ||
"11_ner_0_spacy/model-best/tok2vec/.gitattributes", | ||
"11_ner_0_spacy/model-best/tok2vec/cfg", | ||
"11_ner_0_spacy/model-best/tok2vec/model", | ||
"11_ner_0_spacy/model-best/tokenizer", | ||
"11_ner_0_spacy/model-best/vocab/key2row", | ||
"11_ner_0_spacy/model-best/vocab/lookups.bin", | ||
"11_ner_0_spacy/model-best/vocab/strings.json", | ||
"11_ner_0_spacy/model-best/vocab/vectors", | ||
"11_ner_0_spacy/model-best/vocab/vectors.cfg", | ||
"29_classifier_spacy/model-best/.gitattributes", | ||
"29_classifier_spacy/model-best/config.cfg", | ||
"29_classifier_spacy/model-best/meta.json", | ||
"29_classifier_spacy/model-best/textcat/cfg", | ||
"29_classifier_spacy/model-best/textcat/model", | ||
"29_classifier_spacy/model-best/tok2vec/cfg", | ||
"29_classifier_spacy/model-best/tok2vec/model", | ||
"29_classifier_spacy/model-best/tokenizer", | ||
"29_classifier_spacy/model-best/vocab/key2row", | ||
"29_classifier_spacy/model-best/vocab/lookups.bin", | ||
"29_classifier_spacy/model-best/vocab/strings.json", | ||
"29_classifier_spacy/model-best/vocab/vectors", | ||
"29_classifier_spacy/model-best/vocab/vectors.cfg", | ||
] | ||
def download_models(is_force=False): | ||
""" | ||
Downloads spaCy models to local. | ||
""" | ||
local_path = os.getenv("HARMONY_DATA_PATH", os.path.expanduser("~") + "/harmony") | ||
|
||
print ("Downloading spaCy models to " + local_path ".\nSet environment variable HARMONY_DATA_PATH if you want to change model file location.") | ||
|
||
remote_base = "https://raw.githubusercontent.com/harmonydata/models/main/" | ||
|
||
for file_to_download in files: | ||
url = remote_base + file_to_download | ||
local_filename = local_path + "/" + file_to_download | ||
if os.path.exists(local_filename) and not is_force: | ||
print ("File exists: ", local_filename) | ||
print ("Exiting.\nRun download_models(True) to force redownload.") | ||
break | ||
|
||
r = requests.get(url) | ||
|
||
if not os.path.isdir(os.path.dirname(local_filename)): | ||
os.makedirs(os.path.dirname(local_filename)) | ||
|
||
with open(local_filename, 'wb') as f: | ||
f.write(r.content) |