mirror of
				https://github.com/explosion/spaCy.git
				synced 2025-10-30 23:47:31 +03:00 
			
		
		
		
	Don't throw an error if using displacy on an unset span key (#11845)
* Don't throw an error if using displacy on an unset span key * List available keys in W117
This commit is contained in:
		
							parent
							
								
									07026337d2
								
							
						
					
					
						commit
						2201459603
					
				|  | @ -227,12 +227,13 @@ def parse_spans(doc: Doc, options: Dict[str, Any] = {}) -> Dict[str, Any]: | ||||||
|             "kb_id": span.kb_id_ if span.kb_id_ else "", |             "kb_id": span.kb_id_ if span.kb_id_ else "", | ||||||
|             "kb_url": kb_url_template.format(span.kb_id_) if kb_url_template else "#", |             "kb_url": kb_url_template.format(span.kb_id_) if kb_url_template else "#", | ||||||
|         } |         } | ||||||
|         for span in doc.spans[spans_key] |         for span in doc.spans.get(spans_key, []) | ||||||
|     ] |     ] | ||||||
|     tokens = [token.text for token in doc] |     tokens = [token.text for token in doc] | ||||||
| 
 | 
 | ||||||
|     if not spans: |     if not spans: | ||||||
|         warnings.warn(Warnings.W117.format(spans_key=spans_key)) |         keys = list(doc.spans.keys()) | ||||||
|  |         warnings.warn(Warnings.W117.format(spans_key=spans_key, keys=keys)) | ||||||
|     title = doc.user_data.get("title", None) if hasattr(doc, "user_data") else None |     title = doc.user_data.get("title", None) if hasattr(doc, "user_data") else None | ||||||
|     settings = get_doc_settings(doc) |     settings = get_doc_settings(doc) | ||||||
|     return { |     return { | ||||||
|  |  | ||||||
|  | @ -195,7 +195,7 @@ class Warnings(metaclass=ErrorsWithCodes): | ||||||
|     W117 = ("No spans to visualize found in Doc object with spans_key: '{spans_key}'. If this is " |     W117 = ("No spans to visualize found in Doc object with spans_key: '{spans_key}'. If this is " | ||||||
|             "surprising to you, make sure the Doc was processed using a model " |             "surprising to you, make sure the Doc was processed using a model " | ||||||
|             "that supports span categorization, and check the `doc.spans[spans_key]` " |             "that supports span categorization, and check the `doc.spans[spans_key]` " | ||||||
|             "property manually if necessary.") |             "property manually if necessary.\n\nAvailable keys: {keys}") | ||||||
|     W118 = ("Term '{term}' not found in glossary. It may however be explained in documentation " |     W118 = ("Term '{term}' not found in glossary. It may however be explained in documentation " | ||||||
|             "for the corpora used to train the language. Please check " |             "for the corpora used to train the language. Please check " | ||||||
|             "`nlp.meta[\"sources\"]` for any relevant links.") |             "`nlp.meta[\"sources\"]` for any relevant links.") | ||||||
|  |  | ||||||
|  | @ -203,6 +203,16 @@ def test_displacy_parse_spans_different_spans_key(en_vocab): | ||||||
|     ] |     ] | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def test_displacy_parse_empty_spans_key(en_vocab): | ||||||
|  |     """Test that having an unset spans key doesn't raise an error""" | ||||||
|  |     doc = Doc(en_vocab, words=["Welcome", "to", "the", "Bank", "of", "China"]) | ||||||
|  |     doc.spans["custom"] = [Span(doc, 3, 6, "BANK")] | ||||||
|  |     with pytest.warns(UserWarning, match="W117"): | ||||||
|  |         spans = displacy.parse_spans(doc) | ||||||
|  | 
 | ||||||
|  |     assert isinstance(spans, dict) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| def test_displacy_parse_ents(en_vocab): | def test_displacy_parse_ents(en_vocab): | ||||||
|     """Test that named entities on a Doc are converted into displaCy's format.""" |     """Test that named entities on a Doc are converted into displaCy's format.""" | ||||||
|     doc = Doc(en_vocab, words=["But", "Google", "is", "starting", "from", "behind"]) |     doc = Doc(en_vocab, words=["But", "Google", "is", "starting", "from", "behind"]) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user