Skip to content
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

PR_2_Structural_Design_Pattern(Bridge_Pattern) #159

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 71 additions & 33 deletions covid_nlp/language/detect_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,69 @@
from time import time
import requests

class LanguageDetector():
def __init__(self, model = 'sil'):
# apply bridge pattern
# print method is commonly used by the three class

# abstract class
class Bridge:

#__init__ and model define the printing format
def set_model(self, model):
self.model = model

def Dl_print(self, text):
pass

# concrete abstract class
class Bridge_cld2(Bridge):
def set_model(self, model):
self.model = model
def Dl_print(self, text):
self.model.Dl_print()

def detect_lang_cld2(self, text):
class Bridge_cld3(Bridge):
def set_model(self, model):
self.model = model
def Dl_print(self, text):
self.model.Dl_print()

class Bridge_sil(Bridge):
def set_model(self, model):
self.model = model
def Dl_print(self, text):
self.model.Dl_print()

# Implementation class
# model is the printing format of the output
class model:
def Dl_print(self, text):
pass

# concrete implementation classes which define different ways to print the information
class DlPrint_Detect_lang_cld2(model):

def Dl_print(self, text):
pred = cld2.detect(text)[2][0]
return pred[1], float(pred[2])
print(f"cld2: {pred[1], float(pred[2])}")


def detect_lang_cld3(self, text):
class DlPrint_Detect_lang_cld3(model, n = 3):

def Dl_print(self, text, n = 3):
# print ld3_detect_result
import cld3 # requires protobuf
pred = cld3.get_language(text)
return pred.language, 100*pred.probability
print(f"cld3: {pred.language, 100*pred.probability}")

def detect_lang_sil(self, text):
# print ld3_freq_top_results
pred = cld3.get_frequent_languages(text, num_langs = n)
pred_list = [ (p.language, 100*p.probability) for p in pred ]
print(f"cld3-freq: {pred_list}")


class DlPrint_Detect_lang_sil(model):

def Dl_print(self, text):
algorithm = 'HMAC+SHA1'
curr_time = str(int(time()))
concat = curr_time+os.environ.get('SIL_API_KEY')
Expand All @@ -31,39 +80,28 @@ def detect_lang_sil(self, text):
headers = {'Content-Type': 'application/json'}
r = requests.post(os.environ.get('SIL_API_URL'), json=[{"text": text}],
headers=headers, params=params)
return r.json()[0]['language'], 100*r.json()[0]['probability']

def detect_lang(self, text):
if self.model == 'cld2':
return self.detect_lang_cld2(text)
if self.model == 'cld3':
return self.detect_lang_cld3(text)
if self.model == 'sil':
return self.detect_lang_sil(text)

def detect_freq_lang(self, text, n = 3):
import cld3 # requires protobuf
pred = cld3.get_frequent_languages(text, num_langs = n)
pred_list = [ (p.language, 100*p.probability) for p in pred ]
return pred_list
pred_sil = r.json()[0]['language'], 100*r.json()[0]['probability']
print(f"sil: {pred_sil}")


def main():
my_text = "Was ist das Coronavirus?"
n_number = 4

ld3 = LanguageDetector(model = 'cld3')
ld3_result = ld3.detect_lang(my_text)
print(f"cld3: {ld3_result}")
ld3_top_results = ld3.detect_freq_lang(my_text, 4)
print(f"cld3-freq: {ld3_top_results}")
ld3 = Bridge_cld3()
ld3_print_format = DlPrint_Detect_lang_cld3(text = my_text, n = n_number)
ld3.set_model(model = ld3_print_format)
ld3.Dl_print

ld2 = LanguageDetector(model = 'cld2')
ld2_result = ld2.detect_lang(my_text)
print(f"cld2: {ld2_result}")
ld2 = Bridge_cld2()
ld2_print_format = DlPrint_Detect_lang_cld2(text = my_text)
ld2.set_model(model = ld2_print_format)
ld2.Dl_print

ldsil = LanguageDetector(model = 'sil')
ldsil_result = ldsil.detect_lang(my_text)
print(f"sil: {ldsil_result}")
ldsil = Bridge_sil()
ldsil_print_format = DlPrint_Detect_lang_sil(text = my_text)
ldsil.set_model(model = ldsil_print_format)
ldsil.Dl_print

if __name__ == "__main__":
main()