-
Notifications
You must be signed in to change notification settings - Fork 2
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
11 changed files
with
699 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import os | ||
|
||
from label import run, parser | ||
from label.lfs import FramingLabels | ||
from label.lfs.framing import get_lfs | ||
|
||
REGISTERED_MODEL_NAME = 'FramingLabelModel' | ||
LF_FEATURES = { | ||
'txt_clean_roberta': None, | ||
'txt_clean_use': None, | ||
} | ||
DEV_ANNOTATIONS_PATH = os.path.join('/annotations', 'framing', 'gold_df.pkl') | ||
|
||
|
||
def main(): | ||
parser.add_argument('--trld', default=0.5, type=float, help='cosine similarity threshold') | ||
parser.add_argument('--encoder', default='roberta', choices=('roberta', 'use'), type=str, | ||
help='which encoder embeddings to use') | ||
parsed_args = parser.parse_args() | ||
run.start(REGISTERED_MODEL_NAME, LF_FEATURES, DEV_ANNOTATIONS_PATH, get_lfs, FramingLabels, parsed_args) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import logging | ||
from snorkel.labeling import LabelingFunction | ||
import pandas as pd | ||
from label.lfs import FramingLabels, ABSTAIN | ||
from label import DATABASE_IP | ||
from scipy.spatial.distance import cdist | ||
|
||
FRAME_ELEMENT_QUERY = """ | ||
SELECT * FROM frame_elements; | ||
""" | ||
TRLD = 0.5 | ||
|
||
|
||
def get_lfs(parsed_args) -> [LabelingFunction]: | ||
""" | ||
This function creates and returns a list of all lfs in this module | ||
:return: A list of LabelingFunctions defined in this module | ||
""" | ||
global TRLD | ||
TRLD = parsed_args.trld | ||
lfs = [] | ||
element_lfs = [] | ||
frame_elements_df = pd.read_sql(FRAME_ELEMENT_QUERY, DATABASE_IP) | ||
for label in FramingLabels: | ||
element_lfs = element_lfs + [make_element_lf(row.element_id, getattr(row, parsed_args.encoder), | ||
'txt_clean_{}'.format(parsed_args.encoder), label) | ||
for row in frame_elements_df.itertuples(index=False)] | ||
lfs = lfs + element_lfs | ||
logging.info("LFs have been gathered.") | ||
return lfs | ||
|
||
|
||
def frame_element_similarity(x, element, encoder, label) -> int: | ||
distances = cdist([element], x[encoder], 'cosine')[0] | ||
smallest = min(distances) | ||
similarity = 1 - smallest | ||
return label.value if similarity >= TRLD else ABSTAIN | ||
|
||
|
||
def make_element_lf(element_id: str, element, encoder: str, label) -> LabelingFunction: | ||
return LabelingFunction( | ||
name=element_id, | ||
f=frame_element_similarity, | ||
resources=dict(element=element, encoder=encoder, label=label) | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<View> | ||
<Text name="text" value="$txt"/> | ||
<Choices name="frame" toName="txt" choice="single"> | ||
<Choice value="settled_science"/> | ||
<Choice value="uncertain_science"/> | ||
<Choice value="political_or_ideological_struggle"/> | ||
<Choice value="disaster"/> | ||
<Choice value="opportunity"/> | ||
<Choice value="economic"/> | ||
<Choice value="morality_and_ethics"/> | ||
<Choice value="role_of_science"/> | ||
<Choice value="security"/> | ||
<Choice value="health"/> | ||
</Choices> | ||
</View> | ||
|
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
Oops, something went wrong.