-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
samples/vision/content_moderator_text_moderation_samples.py
This file contains 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,51 @@ | ||
import os.path | ||
from pprint import pprint | ||
import time | ||
|
||
from azure.cognitiveservices.vision.contentmoderator import ContentModeratorClient | ||
from azure.cognitiveservices.vision.contentmoderator.models import ( | ||
Screen | ||
) | ||
from msrest.authentication import CognitiveServicesCredentials | ||
|
||
SUBSCRIPTION_KEY_ENV_NAME = "CONTENTMODERATOR_SUBSCRIPTION_KEY" | ||
CONTENTMODERATOR_LOCATION = os.environ.get("CONTENTMODERATOR_LOCATION", "westcentralus") | ||
|
||
# The number of minutes to delay after updating the search index before | ||
# performing image match operations against the list. | ||
LATENCY_DELAY = 0.5 | ||
|
||
TEXT = """ | ||
Is this a grabage email [email protected], phone: 6657789887, IP: 255.255.255.255, 1 Microsoft Way, Redmond, WA 98052. | ||
Crap is the profanity here. Is this information PII? phone 3144444444 | ||
""" | ||
|
||
def text_moderation(subscription_key): | ||
"""TextModeration. | ||
This will moderate a given long text. | ||
""" | ||
|
||
client = ContentModeratorClient( | ||
CONTENTMODERATOR_LOCATION+'.api.cognitive.microsoft.com', | ||
CognitiveServicesCredentials(subscription_key) | ||
) | ||
|
||
# Screen the input text: check for profanity, | ||
# do autocorrect text, and check for personally identifying | ||
# information (PII) | ||
screen = client.text_moderation.screen_text( | ||
"eng", | ||
"text/plain", | ||
TEXT, | ||
autocorrect=True, | ||
pii=True | ||
) | ||
assert isinstance(screen, Screen) | ||
pprint(screen.as_dict()) | ||
|
||
if __name__ == "__main__": | ||
import sys, os.path | ||
sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) | ||
from tools import execute_samples | ||
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) |