File size: 3,424 Bytes
fa01209 |
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
spouse relation classification # 9 labeling functions (weak_labels) lfs = [ lf_husband_wife, lf_husband_wife_left_window, lf_same_last_name, lf_married, lf_familial_relationship, lf_family_left_window, lf_other_relationship, lf_distant_supervision, lf_distant_supervision_last_names, ] # Labels 0 as Negative 1 as Positive -1 as ABSTAIN #### LF 1 # Check for the `spouse` words appearing between the person mentions spouses = {"spouse", "wife", "husband", "ex-wife", "ex-husband"} def lf_husband_wife(x, spouses): return POSITIVE if len(spouses.intersection(set(x.between_tokens))) > 0 else ABSTAIN #### LF 2 # Check for the `spouse` words appearing to the left of the person mentions def lf_husband_wife_left_window(x, spouses): if len(set(spouses).intersection(set(x.person1_left_tokens))) > 0: return POSITIVE elif len(set(spouses).intersection(set(x.person2_left_tokens))) > 0: return POSITIVE else: return ABSTAIN #### LF 3 # Check for the person mentions having the same last name @labeling_function(pre=[get_person_last_names]) def lf_same_last_name(x): p1_ln, p2_ln = x.person_lastnames if p1_ln and p2_ln and p1_ln == p2_ln: return POSITIVE return ABSTAIN #### LF 4 # Check for the word `married` between person mentions @labeling_function() def lf_married(x): return POSITIVE if "married" in x.between_tokens else ABSTAIN #### LF 5 # Check for words that refer to `family` relationships between the person mentions family = { "father", "mother", "sister", "brother", "son", "daughter", "grandfather", "grandmother", "uncle", "aunt", "cousin", } family = family.union({f + "-in-law" for f in family}) def lf_familial_relationship(x, family): return NEGATIVE if len(family.intersection(set(x.between_tokens))) > 0 else ABSTAIN #### LF 6 # Check for words that refer to `family` relationships to the left of the person mentions def lf_family_left_window(x, family): if len(set(family).intersection(set(x.person1_left_tokens))) > 0: return NEGATIVE elif len(set(family).intersection(set(x.person2_left_tokens))) > 0: return NEGATIVE else: return ABSTAIN #### LF 7 # Check for `other` relationship words between person mentions other = {"boyfriend", "girlfriend", "boss", "employee", "secretary", "co-worker"} def lf_other_relationship(x, other): return NEGATIVE if len(other.intersection(set(x.between_tokens))) > 0 else ABSTAIN #### LF 8 # Simple distant supervision labeling function via DBPedia @labeling_function(resources=dict(known_spouses=known_spouses), pre=[get_person_text]) def lf_distant_supervision(x, known_spouses): p1, p2 = x.person_names if (p1, p2) in known_spouses or (p2, p1) in known_spouses: return POSITIVE else: return ABSTAIN #### LF 9 # Last name pairs for known spouses last_names = set( [ (last_name(x), last_name(y)) for x, y in known_spouses if last_name(x) and last_name(y) ] ) @labeling_function(resources=dict(last_names=last_names), pre=[get_person_last_names]) def lf_distant_supervision_last_names(x, last_names): p1_ln, p2_ln = x.person_lastnames return ( POSITIVE if (p1_ln != p2_ln) and ((p1_ln, p2_ln) in last_names or (p2_ln, p1_ln) in last_names) else ABSTAIN ) |