-
Notifications
You must be signed in to change notification settings - Fork 4
Adding the lowercase aspect #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lunayach
wants to merge
7
commits into
MI2DataLab:master
Choose a base branch
from
lunayach:add-lowercase-aspect
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
84c6637
Adding lowercase aspect
lunayach 6437d46
Simplifying the code
lunayach d8decf3
Adding lowercase aspect
lunayach 95accbf
Simplifying the code
lunayach 01b87b4
Add tests
lunayach b4dd625
Merge branch 'add-lowercase-aspect' of https://github.com/lunayach/Wi…
lunayach 8096acf
Add to docs
lunayach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,12 @@ | ||
| from wildnlp.aspects import LowerCase | ||
|
|
||
|
|
||
| def test_single_word(): | ||
| assert LowerCase()("Language") == "language" | ||
|
|
||
|
|
||
| def test_sentence(): | ||
| sentence = "EU rejects German call to boycott British lamb." | ||
| transformed = LowerCase()(sentence) | ||
|
|
||
| assert transformed == "eu rejects german call to boycott british lamb." |
This file contains hidden or 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 hidden or 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,18 @@ | ||
| from .base import Aspect | ||
|
|
||
|
|
||
| class LowerCase(Aspect): | ||
| """Lower-cases the dataset. | ||
| """ | ||
|
|
||
| def __call__(self, sentence): | ||
| return " ".join([self._lowercase_word(word) | ||
| if word != '' else '' | ||
| for word in sentence.split(' ')]) | ||
|
|
||
| @staticmethod | ||
| def _lowercase_word(word): | ||
| if len(word) == 0: | ||
| raise ValueError("Can't lowercase empty words") | ||
| return word.lower() | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -54,14 +54,17 @@ def load(self, path): | |
| processed = self._process_sample(sample) | ||
| self._data.append(processed) | ||
|
|
||
| def apply(self, aspect, apply_to_ne=False): | ||
| def apply(self, aspect, apply_to_ne=False, apply_to_both=False): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would be better to rewrite it so for example there is the |
||
| """ | ||
|
|
||
| :param aspect: transformation function | ||
|
|
||
| :param apply_to_ne: if `False`, transformation won't be applied | ||
| to Named Entities. If `True`, transformation | ||
| will be applied only to Named Entities. | ||
| :param apply_to_both: if `True`, transformation will be applied | ||
| to both the Named Entities and other tokens. | ||
|
|
||
|
|
||
| :return: modified dataset in the following form: | ||
|
|
||
|
|
@@ -82,10 +85,13 @@ def apply(self, aspect, apply_to_ne=False): | |
| for entry in self._data: | ||
| tags = entry['ner_tags'] | ||
|
|
||
| if apply_to_ne is False: | ||
| non_ner = np.where(tags == 'O')[0] | ||
| if not apply_to_both: | ||
| if apply_to_ne is False: | ||
| non_ner = np.where(tags == 'O')[0] | ||
| else: | ||
| non_ner = np.where(tags != 'O')[0] | ||
| else: | ||
| non_ner = np.where(tags != 'O')[0] | ||
| non_ner = range(len(entry['tokens'])) | ||
|
|
||
| if len(non_ner) == 0: | ||
| modified.append(entry) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add the info that it's especially important for NER tasks?