def test_get_feature_map_conjunction(self): doc = self.sm_nlp( 'Richard and the man said they were entering the big house') self.sm_rules_analyzer.initialize(doc) feature_map = self.sm_tendencies_analyzer.get_feature_map( Mention(doc[0], False), doc) self.assertEqual(len(self.sm_feature_table), len(feature_map)) self.assertEqual([ 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0 ], feature_map) feature_map = self.sm_tendencies_analyzer.get_feature_map( Mention(doc[0], True), doc) self.assertEqual(len(self.sm_feature_table), len(feature_map)) self.assertEqual([ 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0 ], feature_map) feature_map = self.sm_tendencies_analyzer.get_feature_map( Mention(doc[5], False), doc) self.assertEqual(len(self.sm_feature_table), len(feature_map)) self.assertEqual([ 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1 ], feature_map)
def test_get_position_map_conjunction_second_sentence_mentions_true(self): doc = self.sm_nlp( 'A preceding sentence. Peter and Jane spoke to him and her.') self.sm_rules_analyzer.initialize(doc) position_map = self.sm_tendencies_analyzer.get_position_map( Mention(doc[4], True), doc) self.assertEqual([0, 1, 1, 0, 0, 1, 0], position_map) position_map = self.sm_tendencies_analyzer.get_position_map( Mention(doc[9], True), doc) self.assertEqual([5, 2, 1, 2, 0, 1, 0], position_map)
def test_get_position_map_second_sentence_mention(self): doc = self.sm_nlp( 'This is a preceding sentence. Richard said he was entering the big house' ) self.sm_rules_analyzer.initialize(doc) position_map = self.sm_tendencies_analyzer.get_position_map( Mention(doc[6], False), doc) self.assertEqual([0, 1, 1, 0, 0, 0, 0], position_map) position_map = self.sm_tendencies_analyzer.get_position_map( Mention(doc[8], False), doc) self.assertEqual([2, 2, 2, 0, 0, 0, 0], position_map)
def test_vectors_twoway_coordination_lg(self): doc = self.lg_nlp('Peter and Jane arrived') self.lg_rules_analyzer.initialize(doc) peter_vectors = self.lg_tendencies_analyzer.get_vectors( Mention(doc[0], False), doc) jane_vectors = self.lg_tendencies_analyzer.get_vectors( Mention(doc[2], False), doc) combined_vectors = self.lg_tendencies_analyzer.get_vectors( Mention(doc[0], True), doc) for index in range(len(peter_vectors[0])): self.assertAlmostEqual( (peter_vectors[0][index] + jane_vectors[0][index]) / 2, combined_vectors[0][index])
def test_get_position_map_root_mention(self): doc = self.sm_nlp('Richard said he was entering the big house') self.sm_rules_analyzer.initialize(doc) position_map = self.sm_tendencies_analyzer.get_position_map( Mention(doc[1], False), doc) self.assertEqual([1, 0, 0, 0, -1, 0, 0], position_map)
def func(nlp): if nlp.meta['name'] in excluded_nlps: return doc = nlp(doc_text) rules_analyzer = RulesAnalyzerFactory.get_rules_analyzer(nlp) rules_analyzer.initialize(doc) assert rules_analyzer.is_independent_noun(doc[referred_index]) or \ rules_analyzer.is_potential_anaphor(doc[referred_index]) assert rules_analyzer.is_potential_anaphor(doc[referring_index]) referred_mention = Mention(doc[referred_index], include_dependent_siblings) self.assertEqual( expected_truth, rules_analyzer.is_potential_anaphoric_pair( referred_mention, doc[referring_index], True), nlp.meta['name']) self.assertEqual( expected_reflexive_truth, rules_analyzer.is_potential_reflexive_pair( referred_mention, doc[referring_index]), nlp.meta['name']) self.assertEqual( is_reflexive_anaphor_truth, rules_analyzer.is_reflexive_anaphor(doc[referring_index]), nlp.meta['name'])
def test_get_position_map_conjunction_first_sentence_mentions_false(self): doc = self.sm_nlp('Peter and Jane spoke to him and her.') self.sm_rules_analyzer.initialize(doc) position_map = self.sm_tendencies_analyzer.get_position_map( Mention(doc[0], False), doc) self.assertEqual([0, 1, 1, 0, 0, -1, 0], position_map) position_map = self.sm_tendencies_analyzer.get_position_map( Mention(doc[2], False), doc) self.assertEqual([2, 2, 1, 1, 1, -1, 1], position_map) position_map = self.sm_tendencies_analyzer.get_position_map( Mention(doc[5], False), doc) self.assertEqual([5, 2, 1, 2, 0, -1, 0], position_map) position_map = self.sm_tendencies_analyzer.get_position_map( Mention(doc[7], False), doc) self.assertEqual([7, 3, 1, 1, 1, -1, 1], position_map)
def test_get_position_map_first_sentence_mention(self): doc = self.sm_nlp('Richard said he was entering the big house') self.sm_rules_analyzer.initialize(doc) mention = Mention(doc[0], False) position_map = self.sm_tendencies_analyzer.get_position_map( mention, doc) self.assertEqual(mention.temp_position_map, position_map) self.assertEqual([0, 1, 1, 0, 0, 0, 0], position_map) position_map = self.sm_tendencies_analyzer.get_position_map( Mention(doc[2], False), doc) self.assertEqual([2, 2, 2, 0, 0, 0, 0], position_map) position_map = self.sm_tendencies_analyzer.get_position_map( Mention(doc[6], False), doc) self.assertEqual([6, 3, 2, 1, 1, 0, 0], position_map)
def test_vectors_threeway_coordination_lg(self): doc = self.lg_nlp('They spoke to Richard, Peter and Jane.') self.lg_rules_analyzer.initialize(doc) richard_vectors = self.lg_tendencies_analyzer.get_vectors( Mention(doc[3], False), doc) peter_vectors = self.lg_tendencies_analyzer.get_vectors( Mention(doc[5], False), doc) jane_vectors = self.lg_tendencies_analyzer.get_vectors( Mention(doc[7], False), doc) combined_vectors = self.lg_tendencies_analyzer.get_vectors( Mention(doc[3], True), doc) for index in range(len(peter_vectors[0])): self.assertAlmostEqual( (peter_vectors[0][index] + jane_vectors[0][index] + richard_vectors[0][index]) / 3, combined_vectors[0][index], places=3)
def test_vectors_threeway_coordination_sm(self): doc = self.sm_nlp('Richard, Peter and Jane arrived') self.sm_rules_analyzer.initialize(doc) richard_vectors = self.sm_tendencies_analyzer.get_vectors( Mention(doc[0], False), doc) peter_vectors = self.sm_tendencies_analyzer.get_vectors( Mention(doc[2], False), doc) jane_vectors = self.sm_tendencies_analyzer.get_vectors( Mention(doc[4], False), doc) combined_vectors = self.sm_tendencies_analyzer.get_vectors( Mention(doc[0], True), doc) for index in range(len(peter_vectors[0])): self.assertAlmostEqual( (peter_vectors[0][index] + jane_vectors[0][index] + richard_vectors[0][index]) / 3, combined_vectors[0][index], places=3)
def test_get_vectors_mention_without_head_lg(self): doc = self.lg_nlp('He arrived') self.lg_rules_analyzer.initialize(doc) vectors = self.lg_tendencies_analyzer.get_vectors( Mention(doc[1], False), doc) self.assertTrue(vectors[0].any()) self.assertFalse(vectors[1].any()) self.assertEqual(len(vectors[0]), len(vectors[1]))
def test_get_cosine_similarity_lg_root_2(self): doc = self.lg_nlp('Richard arrived. He.') self.lg_rules_analyzer.initialize(doc) self.assertEqual( '[3, 1, 0, -1, 1]', str( self.lg_tendencies_analyzer.get_compatibility_map( Mention(doc[0], False), doc[3])))
def test_get_compatibility_map_simple(self): doc = self.sm_nlp('Richard said he was entering the big house') self.sm_rules_analyzer.initialize(doc) self.assertEqual( '[2, 0, 1, 0.30341318, 3]', str( self.sm_tendencies_analyzer.get_compatibility_map( Mention(doc[0], False), doc[2])))
def test_get_compatibility_map_same_sentence_no_governance(self): doc = self.sm_nlp( 'After Richard arrived, he said he was entering the big house') self.sm_rules_analyzer.initialize(doc) self.assertEqual( '[4, 0, 0, 0.045050576, 5]', str( self.sm_tendencies_analyzer.get_compatibility_map( Mention(doc[0], False), doc[4])))
def test_get_vectors_mention_with_head_sm(self): doc = self.sm_nlp('He arrived') self.sm_rules_analyzer.initialize(doc) mention = Mention(doc[0], False) vectors = self.sm_tendencies_analyzer.get_vectors(mention, doc) self.assertTrue(vectors[0].any()) self.assertTrue(vectors[1].any()) self.assertEqual(len(vectors[0]), len(vectors[1])) self.assertEqual(vectors, mention.temp_vectors)
def test_get_cosine_similarity_lg_root_1(self): doc = self.lg_nlp('Richard. He said he was entering the big house') self.lg_rules_analyzer.initialize(doc) self.assertEqual( '[2, 1, 0, -1, 1]', str( self.lg_tendencies_analyzer.get_compatibility_map( Mention(doc[0], False), doc[2])))
def test_get_compatibility_map_different_sentences(self): doc = self.sm_nlp( 'Richard called. He said he was entering the big house') self.sm_rules_analyzer.initialize(doc) self.assertEqual( '[3, 1, 0, 0.507851, 6]', str( self.sm_tendencies_analyzer.get_compatibility_map( Mention(doc[0], False), doc[3])))
def test_get_compatibility_map_coordination(self): doc = self.sm_nlp( 'Richard and Jane said he was entering the big house') self.sm_rules_analyzer.initialize(doc) self.assertEqual( '[4, 0, 1, 0.23422348, 3]', str( self.sm_tendencies_analyzer.get_compatibility_map( Mention(doc[0], True), doc[4])))
def test_get_cosine_similarity_lg(self): doc = self.lg_nlp( 'After Richard arrived, he said he was entering the big house') self.lg_rules_analyzer.initialize(doc) self.assertEqual( '[4, 0, 0, 0.3336621, 5]', str( self.lg_tendencies_analyzer.get_compatibility_map( Mention(doc[0], False), doc[4])))
def test_get_feature_map_simple_mention(self): doc = self.sm_nlp('Richard said he was entering the big house') self.sm_rules_analyzer.initialize(doc) mention = Mention(doc[0], False) feature_map = self.sm_tendencies_analyzer.get_feature_map(mention, doc) self.assertEqual(len(self.sm_feature_table), len(feature_map)) self.assertEqual(mention.temp_feature_map, feature_map) self.assertEqual([ 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0 ], feature_map) feature_map = self.sm_tendencies_analyzer.get_feature_map( Mention(doc[2], False), doc) self.assertEqual(len(self.sm_feature_table), len(feature_map)) self.assertEqual([ 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1 ], feature_map)
def test_get_compatibility_map_same_sentence_lefthand_sibling_governance( self): doc = self.lg_nlp( 'Richard said Peter and he were entering the big house') self.lg_rules_analyzer.initialize(doc) self.assertEqual( '[4, 0, 1, 0.15999001, 3]', str( self.sm_tendencies_analyzer.get_compatibility_map( Mention(doc[0], False), doc[4])))
def test_get_compatibility_map_same_sentence_lefthand_sibling_no_governance( self): doc = self.sm_nlp( 'After Richard arrived, Peter and he said he was entering the big house' ) self.sm_rules_analyzer.initialize(doc) self.assertEqual( '[5, 0, 0, 0.41453412, 1]', str( self.sm_tendencies_analyzer.get_compatibility_map( Mention(doc[1], False), doc[6])))
def test_object_access(self): doc = self.sm_nlp( 'I saw Peter. Richard and he came in. They had arrived') self.assertEqual('1: [4, 6], [10]', str(doc._.coref_chains[1])) self.assertEqual(2, len(doc._.coref_chains)) self.assertEqual(2, len(doc._.coref_chains[1])) self.assertEqual('[4, 6]', str(doc._.coref_chains[1][0])) self.assertEqual(2, len(doc._.coref_chains[1][0])) self.assertEqual(4, doc._.coref_chains[1][0][0]) found = False for chain in doc._.coref_chains: for mention in chain.mentions: if mention == Mention(doc[10], False): found = True self.assertTrue(found)
def func(nlp): if nlp.meta['name'] in excluded_nlps: return doc = nlp(doc_text) rules_analyzer = RulesAnalyzerFactory.get_rules_analyzer(nlp) rules_analyzer.initialize(doc) assert rules_analyzer.is_independent_noun(doc[referred_index]) or \ rules_analyzer.is_potential_anaphor(doc[referred_index]) assert rules_analyzer.is_potential_anaphor(doc[referring_index]) referred_mention = Mention(doc[referred_index], include_dependent_siblings) if consider_syntax: self.assertEqual(expected_truth, rules_analyzer.language_independent_is_potential_anaphoric_pair( referred_mention, doc[referring_index]), nlp.meta['name']) else: self.assertEqual(expected_truth, rules_analyzer.is_potential_anaphoric_pair( referred_mention, doc[referring_index], False), nlp.meta['name'])