-
Notifications
You must be signed in to change notification settings - Fork 198
Adding two filters -- ambiguous characters and alphanumeric characters #274
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
Changes from all commits
8b8a4e1
0cb488a
a69ec1a
ce30eca
c62cf5b
713be42
0c42053
3646e93
a7a2639
3b79ce9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| ## Alphanumeric Characters Filter | ||
|
|
||
| ## What type of filter is this? | ||
|
|
||
| This transformation filters text that contains characters which are non-alphanumeric and not common punctuation. | ||
| The alphabetical characters are determined by the 26 letters of the English alphabet. | ||
|
|
||
| Author: Mo Tiwari | ||
| Author Email: motiwari@stanford.edu | ||
| Author Affiliation: Stanford University | ||
|
|
||
| ## Why is measuring performance on this split important? | ||
| This filter can be used to a) select text with only characters from a standard alphabet and | ||
| b) remove characters that are specifically meant to circumvent filters e.g. text that uses | ||
| `buy some pi//s` if the string `buy some pills` triggers spam filters | ||
|
|
||
| This is of import in domains such as profanity detection and spam, where bad actors may attempt to work around existing filters by using characters that can be easily mistaken for others. | ||
|
|
||
| ## Related Work | ||
|
|
||
| N/A | ||
|
|
||
| ## What are the limitations of this filter? | ||
| - Currently, the filter only permits characters as defined by the English alphabet. | ||
| The filter could be extended to handle the characters from other alphabets via the `args` | ||
| provided. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .filter import * |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| from tasks.TaskTypes import TaskType | ||
| from interfaces.SentenceOperation import SentenceOperation | ||
|
|
||
| class AlphanumericFilter(SentenceOperation): | ||
|
motiwari marked this conversation as resolved.
|
||
| """ | ||
| Filters sentence that characters which are a) not alphanumeric and b) not common punctuation. | ||
|
|
||
| Inherits SentenceOperation. | ||
| """ | ||
| tasks = [TaskType.TEXT_CLASSIFICATION, TaskType.TEXT_TO_TEXT_GENERATION] | ||
| languages = ["en"] | ||
| keywords = ["highly-meaning-preserving", "low-generations", "rule-based"] | ||
|
|
||
|
motiwari marked this conversation as resolved.
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self.punctuation = ['!', '.', '?', "'", '"', '(', ')', '-', ':', ';', ' '] | ||
|
|
||
| def filter(self, sentence: str = None) -> bool: | ||
| for c in sentence: | ||
| if not c.isalnum() and c not in self.punctuation: | ||
| return False | ||
| return True | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| { | ||
| "type": "keywords", | ||
| "test_cases": [ | ||
| { | ||
| "class": "AlphanumericFilter", | ||
| "inputs": { | ||
| "sentence": "Andrew played cricket in India." | ||
| }, | ||
| "outputs": true | ||
| }, | ||
| { | ||
| "class": "AlphanumericFilter", | ||
| "inputs": { | ||
| "sentence": "∂ is a Greek letter." | ||
| }, | ||
| "outputs": false | ||
| }, | ||
| { | ||
| "class": "AlphanumericFilter", | ||
| "inputs": { | ||
| "sentence": "I love tennis!" | ||
| }, | ||
| "outputs": true | ||
| }, | ||
| { | ||
| "class": "AlphanumericFilter", | ||
| "inputs": { | ||
| "sentence": "¿Cómo estás?" | ||
| }, | ||
| "outputs": false | ||
| }, | ||
| { | ||
| "class": "AlphanumericFilter", | ||
| "inputs": { | ||
| "sentence": "Some non-alphanumeric characters are ^, *, and ž." | ||
| }, | ||
| "outputs": false | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| ## Ambiguous Characters Filter | ||
|
|
||
| ## What type of a filter is this? | ||
|
|
||
| This filter filters sentences that contain ambiguous characters. | ||
| (Aside: `Buffalo buffalo buffalo Buffalo buffalo Buffalo buffalo buffalo --> Filter filters filter filter filters filter filters filter`?) | ||
|
|
||
| Author: Mo Tiwari | ||
| Author Email: motiwari@stanford.edu | ||
| Author Affiliation: Stanford University | ||
|
|
||
| ## Why is measuring performance on this split important? | ||
| This filter can be used to either a) select text with ambiguous characters, or b) select text that contains only unambiguous characters. | ||
| This is of import in domains such as profanity detection and spam, where bad actors may attempt to work around existing filters by using characters that can be easily mistaken for others. | ||
|
|
||
| For example, "Buy some piIIs here" actually contains two capital `I`s for `l`s. | ||
|
|
||
| This feature is also common in password managers, e.g. as a setting to avoid ambiguous characters when generating | ||
| passwords. | ||
|
|
||
| ## Related Work | ||
|
|
||
| N/A | ||
|
|
||
| ## What are the limitations of this filter? | ||
| - The usefulness of the filter depends on font in which the initial text was rendered; future work could accept the | ||
| source font as an argument | ||
| - The filter is also primarily useful for the English language. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .filter import * |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from interfaces.SentenceOperation import SentenceOperation | ||
| from tasks.TaskTypes import TaskType | ||
|
|
||
|
|
||
| class AmbiguousCharactersFilter(SentenceOperation): | ||
|
motiwari marked this conversation as resolved.
|
||
| """ | ||
| Filters sentence that contain ambiguous characters. The characters that might be ambiguous are defined below. | ||
|
|
||
| Inherits SentenceOperation. | ||
| """ | ||
| tasks = [TaskType.TEXT_CLASSIFICATION, TaskType.TEXT_TO_TEXT_GENERATION] | ||
| languages = ["en"] | ||
| keywords = ["highly-meaning-preserving", "low-generations", "rule-based"] | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self.ambiguous_chars = [ | ||
| '0', 'O', 'D', 'o', 'Q', | ||
| 'l', '1', 'I', 'i', '!', '|', | ||
| 'B', '8', | ||
| 'Z', '2', | ||
| 'S', '5', | ||
| 'G', '6', | ||
| "'", '`', | ||
| ] | ||
|
|
||
| def filter(self, sentence: str = None) -> bool: | ||
| for c in sentence: | ||
| if c in self.ambiguous_chars: | ||
| return False | ||
|
Comment on lines
+27
to
+30
Collaborator
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. One question, this filter will return "False" for every sentence which contains the above-defined ambiguous chars? So for the sentence "Buy some piIIs here" it returns "False" which looks fine, but sentences like "This is the last 007 movie of Daniel Craig.", it will also return "False" (because of 0). So every sentence which contains 0 (or "G" or "Z") is an ambiguous sentence and gets filtered. Because this list contains two vowels (O, I), and numbers (0,1,2,5,6,8), most of the sentences will fall under the ambiguous category.
Contributor
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. Hi @motiwari , I like the idea of ambiguouscharacters filter. But I have the same question as @ashish3586 . After I check out your PR and run it locally, I cannot pass all the test cases with the following error. If I understand it correctly, 'She has a cat.' returned False since it contains ambiguous_chars 'S' .
Author
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. Thanks @Yiwen-Shi -- I have fixed that test case. And @Yiwen-Shi and @ashish3586 your understandings are correct; for this filter, any sentence with a single ambiguous character will get filtered. This is a crude, first-pass filter that might be valuable to the community. For future work, it would be interesting to filter sentences only if the ambiguous character actually leads to a difference in interpretation of the sentence, e.g. have some dictionary of English words and consider all possible substitutions of the ambiguous characters that could create multiple words. For example:
Unfortunately such an approach is significantly more complex and I won't be able to implement it in this PR; however, I hope that someone will build off of this idea to implement it |
||
| return True | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "type": "ambiguouscharacters", | ||
| "test_cases": [ | ||
| ] | ||
| } |
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.
Your build is failing:probably because of missing
from interfaces.SentenceOperation import SentenceOperation