Skip to content

Commit

Permalink
Added detection of lanugage in English
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisMayo committed Feb 28, 2021
1 parent a84508e commit fdea639
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
15 changes: 10 additions & 5 deletions anim.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from textblob import TextBlob, exceptions
import re
from google.cloud import translate_v2 as translate
from langdetect import detect

nlp = spacy.load("xx_ent_wiki_sm")
nlp.add_pipe(nlp.create_pipe('sentencizer'))
Expand Down Expand Up @@ -715,11 +716,15 @@ def comments_to_scene(comments: List, characters: Dict, **kwargs):
inv_characters = {v: k for k, v in characters.items()}
for comment in comments:
try:
if (official_api):
result = translate_client.translate(comment.body, target_language="en")
blob = TextBlob(result["translatedText"])
else:
blob = TextBlob(comment.body).translate()
# We don't need to translate if we are already in english
if (detect(comment.body) == 'en'):
blob = TextBlob(comment.body)
else:
if (official_api):
result = translate_client.translate(comment.body, target_language="en")
blob = TextBlob(result["translatedText"])
else:
blob = TextBlob(comment.body).translate()
except Exception as e:
print(e)
blob = TextBlob(comment.body)
Expand Down
2 changes: 2 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def __init__(self, username: str, text: str, score: int = 0):
MockRedditComment('a', 'Hello as I am the most common I will be Phoenix'),
MockRedditComment('b', 'wassup I\'m edgyboy'),
MockRedditComment('c', 'I\'m someone random and I\'m angry', score=-1),
MockRedditComment('c', 'Bonjour, je m\'appelle Louis', score=-1),
MockRedditComment('c', ':(', score=-1),
MockRedditComment('d', 'd', score=-1),
MockRedditComment('e', 'e', score=-1),
MockRedditComment('f', 'f', score=-1),
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ praw
tinydb
https://github.com/explosion/spacy-models/releases/download/xx_ent_wiki_sm-2.3.0/xx_ent_wiki_sm-2.3.0.tar.gz
google-cloud-translate==2.0.1
langdetect

0 comments on commit fdea639

Please sign in to comment.