{"id":13935772,"url":"https://github.com/jenojp/negspacy","last_synced_at":"2025-04-13T00:46:14.505Z","repository":{"id":35063898,"uuid":"201071164","full_name":"jenojp/negspacy","owner":"jenojp","description":"spaCy pipeline object for negating concepts in text","archived":false,"fork":false,"pushed_at":"2024-06-14T01:20:36.000Z","size":273,"stargazers_count":277,"open_issues_count":3,"forks_count":35,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-13T00:46:09.275Z","etag":null,"topics":["negation","negation-phrases","negex","nlp","python","spacy","spacy-extension","spacy-pipeline"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jenojp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-07T14:51:45.000Z","updated_at":"2025-04-09T05:27:26.000Z","dependencies_parsed_at":"2022-07-04T15:05:00.555Z","dependency_job_id":"f0599a9b-35ca-4359-a93e-7abc29cd1d5c","html_url":"https://github.com/jenojp/negspacy","commit_stats":{"total_commits":97,"total_committers":6,"mean_commits":"16.166666666666668","dds":"0.21649484536082475","last_synced_commit":"fe8004c05ac557c771aea046fa08018daede781f"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenojp%2Fnegspacy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenojp%2Fnegspacy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenojp%2Fnegspacy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenojp%2Fnegspacy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jenojp","download_url":"https://codeload.github.com/jenojp/negspacy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650420,"owners_count":21139672,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["negation","negation-phrases","negex","nlp","python","spacy","spacy-extension","spacy-pipeline"],"created_at":"2024-08-07T23:02:04.997Z","updated_at":"2025-04-13T00:46:14.471Z","avatar_url":"https://github.com/jenojp.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"\n\u003cp align=\"center\"\u003e\u003cimg width=\"40%\" src=\"docs/icon.png\" /\u003e\u003c/p\u003e\n\n\n# negspacy: negation for spaCy\n\n[![Build Status](https://dev.azure.com/jenopizzaro/negspacy/_apis/build/status/jenojp.negspacy?branchName=master)](https://dev.azure.com/jenopizzaro/negspacy/_build/latest?definitionId=2\u0026branchName=master) [![Built with spaCy](https://img.shields.io/badge/made%20with%20❤%20and-spaCy-09a3d5.svg)](https://spacy.io) [![pypi Version](https://img.shields.io/pypi/v/negspacy.svg?style=flat-square)](https://pypi.org/project/negspacy/) [![DOI](https://zenodo.org/badge/201071164.svg)](https://zenodo.org/badge/latestdoi/201071164) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square)](https://github.com/ambv/black)\n\nspaCy pipeline object for negating concepts in text. Based on the NegEx algorithm.\n\n***NegEx - A Simple Algorithm for Identifying Negated Findings and Diseases in Discharge Summaries\nChapman, Bridewell, Hanbury, Cooper, Buchanan***\n[https://doi.org/10.1006/jbin.2001.1029](https://doi.org/10.1006/jbin.2001.1029)\n\n## What's new\nVersion 1.0 is a major version update providing support for spaCy 3.0's new interface for adding pipeline components. As a result, it is not backwards compatible with previous versions of negspacy.\n\nIf your project uses spaCy 2.3.5 or earlier, you will need to use version 0.1.9. See [archived readme](https://github.com/jenojp/negspacy/blob/v0.1.9_spacy_2.3.5/README.md).\n\n## Installation and usage\nInstall the library.\n```bash\npip install negspacy\n```\n\nImport library and spaCy.\n```python\nimport spacy\nfrom negspacy.negation import Negex\n```\n\nLoad spacy language model. Add negspacy pipeline object. Filtering on entity types is optional.\n```python\nnlp = spacy.load(\"en_core_web_sm\")\nnlp.add_pipe(\"negex\", config={\"ent_types\":[\"PERSON\",\"ORG\"]})\n\n```\n\nView negations.\n```python\ndoc = nlp(\"She does not like Steve Jobs but likes Apple products.\")\n\nfor e in doc.ents:\n\tprint(e.text, e._.negex)\n```\n\n```console\nSteve Jobs True\nApple False\n```\n\nConsider pairing with [scispacy](https://allenai.github.io/scispacy/) to find UMLS concepts in text and process negations.\n\n## NegEx Patterns\n\n* **pseudo_negations** - phrases that are false triggers, ambiguous negations, or double negatives\n* **preceding_negations** - negation phrases that precede an entity\n* **following_negations** - negation phrases that follow an entity\n* **termination** - phrases that cut a sentence in parts, for purposes of negation detection (.e.g., \"but\")\n\n### Termsets\n\nDesignate termset to use, `en_clinical` is used by default.\n\n* `en` = phrases for general english language text\n* `en_clinical` **DEFAULT** = adds phrases specific to clinical domain to general english\n* `en_clinical_sensitive` = adds additional phrases to help rule out historical and possibly irrelevant entities\n\nTo set:\n```python\nfrom negspacy.negation import Negex\nfrom negspacy.termsets import termset\n\nts = termset(\"en\")\n\nnlp = spacy.load(\"en_core_web_sm\")\nnlp.add_pipe(\n    \"negex\",\n    config={\n        \"neg_termset\":ts.get_patterns()\n    }\n)\n\n```\n\n## Additional Functionality\n\n### Change patterns or view patterns in use\n\nReplace all patterns with your own set\n```python\nnlp = spacy.load(\"en_core_web_sm\")\nnlp.add_pipe(\n    \"negex\", \n    config={\n        \"neg_termset\":{\n            \"pseudo_negations\": [\"might not\"],\n            \"preceding_negations\": [\"not\"],\n            \"following_negations\":[\"declined\"],\n            \"termination\": [\"but\",\"however\"]\n        }\n    }\n    )\n```\n\nAdd and remove individual patterns on the fly from built-in termsets\n```python\nfrom negspacy.termsets import termset\nts = termset(\"en\")\nts.add_patterns({\n            \"pseudo_negations\": [\"my favorite pattern\"],\n            \"termination\": [\"these are\", \"great patterns\", \"but\"],\n            \"preceding_negations\": [\"wow a negation\"],\n            \"following_negations\": [\"extra negation\"],\n        })\n#OR\nts.remove_patterns(\n        {\n            \"termination\": [\"these are\", \"great patterns\"],\n            \"pseudo_negations\": [\"my favorite pattern\"],\n            \"preceding_negations\": [\"denied\", \"wow a negation\"],\n            \"following_negations\": [\"unlikely\", \"extra negation\"],\n        }\n    )\n```\n\nView patterns in use\n```python\nfrom negspacy.termsets import termset\nts = termset(\"en_clinical\")\nprint(ts.get_patterns())\n```\n\n\n### Negations in noun chunks\n\nDepending on the Named Entity Recognition model you are using, you _may_ have negations \"chunked together\" with nouns. For example:\n```python\nnlp = spacy.load(\"en_core_sci_sm\")\ndoc = nlp(\"There is no headache.\")\nfor e in doc.ents:\n    print(e.text)\n\n# no headache\n```\nThis would cause the Negex algorithm to miss the preceding negation. To account for this, you can add a ```chunk_prefix```:\n\n```python\nnlp = spacy.load(\"en_core_sci_sm\")\nts = termset(\"en_clinical\")\nnlp.add_pipe(\n    \"negex\",\n    config={\n        \"chunk_prefix\": [\"no\"],\n    },\n    last=True,\n)\ndoc = nlp(\"There is no headache.\")\nfor e in doc.ents:\n    print(e.text, e._.negex)\n\n# no headache True\n```\n\n\n## Contributing\n[contributing](https://github.com/jenojp/negspacy/blob/master/CONTRIBUTING.md)\n\n## Authors\n* Jeno Pizarro\n\n## License\n[license](https://github.com/jenojp/negspacy/blob/master/LICENSE)\n\n## Other libraries\n\nThis library is featured in the [spaCy Universe](https://spacy.io/universe). Check it out for other useful libraries and inspiration.\n\nIf you're looking for a spaCy pipeline object to extract values that correspond to a named entity (e.g., birth dates, account numbers, or laboratory results) take a look at [extractacy](https://github.com/jenojp/extractacy).\n\n\u003cp align=\"left\"\u003e\u003cimg width=\"40%\" src=\"https://github.com/jenojp/extractacy/blob/master/docs/icon.png?raw=true\" /\u003e\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjenojp%2Fnegspacy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjenojp%2Fnegspacy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjenojp%2Fnegspacy/lists"}