예제 #1
0
    def __init__(self, dict_src="CSW19", width=5):
        self.d = Dictionary(dict_src)

        self.width = width

        # numpy array of size (width)^4, which gives the adjacents mask for a given x,y. Generated initially to avoid repeats
        self.adjacents = gen_adjacents(width)
예제 #2
0
    def __init__(self):
        self.client = zulip.Client(site="https://nokia3310.zulipchat.com/api/")
        self.subscribe_all()
        # self.chatbot = ChatBot("Friday", trainer='chatterbot.trainers.ChatterBotCorpusTrainer')
        # self.chatbot.train("chatterbot.corpus.english")

        self.hacknews = Hackernews()

        self.dictionary = Dictionary()

        self.joke = Joke()

        self.motivate = Motivate()

        self.subkeys = [
            "joke", "news", "motivate", "help", "hi", "hello", "hey", "stat.*",
            "define", ["what", "is"]
        ]
예제 #3
0
from sklearn.metrics import mean_squared_error
import tweepy

app = Flask(__name__)
class SentimentScore:
    def __init__(self, positive_tweets, negative_tweets, neutral_tweets):

        self.positive_tweets = positive_tweets
        self.negative_tweets = negative_tweets
        self.neutral_tweets = neutral_tweets

        self.neg = len(negative_tweets)
        self.pos = len(positive_tweets)
        self.neut = len(neutral_tweets)

dictionaryN  = Dictionary('negative.txt')
dictionaryP  = Dictionary('positive.txt')
dictionaryN2  = Dictionary('negative-words.txt')
dictionaryP2  = Dictionary('positive-words.txt')

def fetch(key):
	data={}
	auth = tweepy.auth.OAuthHandler("8IpOjg0S14jliFJ8f1gRvc9Y5" , "pg52bFarcIrtDJkfwdDdZkqAOi3hORw2l7v3amsK1YWocGIrUS")
	auth.set_access_token("609857856-2ze0BElTOm3AJbSIFrbwJFXi87ffkE0QmcOmyC1h", "7ny4oyk5Kx49P0mQTA8wK2o2wTtxhbfqnn6fNdsFFJCxm")

	api = tweepy.API(auth, wait_on_rate_limit = True)
	count = 0
	for tweet in tweepy.Cursor(api.search,
                           q = key,
                           since = "2019-04-05",
                           until = "2019-04-06",
예제 #4
0
def main():

    # Our program uses Victoria University of Wellington's range software vocabulary list
    # For further information on that, please visit and download it at http://www.victoria.ac.nz/lals/resources/range
    # establish the paths for the dictionaries
    dict1 = os.path.join("/home/ubuntu/workspace/text_assessment/dicts", "gsl_1st.txt")
    dict2 = os.path.join("/home/ubuntu/workspace/text_assessment/dicts", "gsl_2nd.txt")
    dict3 = os.path.join("/home/ubuntu/workspace/text_assessment/dicts", "gsl_3rd.txt")
    if not dict1 or not dict2 or not dict3:
        sys.exit("Could not find the path")
    
    # instantiate the object
    dictionary = Dictionary()
    loaded = dictionary.load(dict1, dict2, dict3)
    if not loaded:
       sys.exit("Could not load the dictionaries")
    
    # make the counters for each dictionary
    count1, count2, count3 = 0, 0, 0
    path = '/home/ubuntu/workspace/text_assessment/extDocs'
    #print 'File ID' + '\t\t\t' + 'Easy' + '\t' + 'Medium' + '\t' + 'Advanced'
    workbook = xl.Workbook('results.xlsx')
    worksheet = workbook.add_worksheet()
    worksheet.write('A1', 'FILE ID')
    worksheet.write('B1', 'EASY')
    worksheet.write('C1', 'MEDIUM')
    worksheet.write('D1', 'ADVANCED')
    worksheet.write('E1', 'NUMBER OF WORDS')
    worksheet.write('F1', 'NUMBER OF SENTENCES')
    worksheet.write('G1', 'NUMBER OF COMPLEX WORDS')
    worksheet.write('H1', 'NUMBER OF UNIQUE WORDS')
    
    # get each file from the folder and analyze
    i = 1
    
    for filename in os.listdir(path):
        essay = os.path.join("/home/ubuntu/workspace/text_assessment/extDocs", filename)
        # initialize the object
        analyzer = Analyzer(essay)
        # get the whole text in the form of token put into the list
        tokens = analyzer.return_tokens(essay)
        twd = 0
        count1, count2, count3 = 0, 0, 0
        for token in tokens:
           if token not in string.punctuation:
               if dictionary.check(token) == 1: # function returns 1 if the word is in the 'easy' list
                   count1 = count1 + 1
               if dictionary.check(token) == 2: # returns 2 if in the 'medium' list
                   count2 += 1
               if dictionary.check(token) == 3: # returns 3 if in the 'advanced' list
                   count3 += 1
               else:
                   continue
        
        # twd stands for "total words detected"
        twd = count1 + count2 + count3
        worksheet.write(i, 0, filename)
        worksheet.write(i,1, float("{0:.2f}".format(count1*100.0/twd)))
        worksheet.write(i,2, float("{0:.2f}".format(count2*100.0/twd)))
        worksheet.write(i,3, float("{0:.2f}".format(count3*100.0/twd)))
        worksheet.write(i,4, analyzer.word_count())
        worksheet.write(i,5, analyzer.number_of_sentences())
        worksheet.write(i,6, analyzer.complex_words())
        worksheet.write(i,7, analyzer.unique_word_count())
        i += 1
        # print the result in so that it aligns like a table
        #print (filename + "\t\t" + "{0:.2f}".format(count1*100.0/twd) + "%" + "\t" + 
        #"{0:.2f}".format(count2*100.0/twd) + "%" + "\t" + "{0:.2f}".format(count3*100.0/twd) + "%")
    workbook.close()
예제 #5
0
파일: env.py 프로젝트: volkancirik/refer360
    def __init__(self,
                 splits=['train'],
                 batch_size=64,
                 seed=0,
                 image_h=FOV_SIZE,
                 image_w=FOV_SIZE,
                 fov=90,
                 degrees=5,
                 use_look_ahead=False,
                 use_sentences=False,
                 use_gt_action=False,
                 use_gpu_camera=False,
                 oracle_mode=False,
                 prepare_vocab=False,
                 images='all'):

        self.batch_size = batch_size
        self.image_w = image_w
        self.image_h = image_h
        self.fov = 90
        self.degrees = degrees
        self.use_look_ahead = use_look_ahead
        self.use_gpu_camera = use_gpu_camera

        # observed image size
        self.observation_space = (3, FOV_SIZE, FOV_SIZE)
        self.action_space = 5
        self.images = images

        # set the number of cameras(envs) to batch_size
        self.env = EnvBatch(batch_size=self.batch_size,
                            image_h=self.image_h,
                            image_w=self.image_w,
                            fov=self.fov,
                            degrees=self.degrees,
                            use_gpu_camera=self.use_gpu_camera)
        # keep a list of datum
        self.data = []

        # load splits
        self.vocab = None

        # load connectivitiy of nodes
        connectivity = []
        for ii, split_name in enumerate(splits):
            data_split, sentences = load_datasets([split_name], self.images)
            for jj, datum in enumerate(data_split[0]):
                datum['id'] = '{}_{}'.format(split_name, jj)

                if 'nodes' in datum:
                    for node in datum['nodes']:
                        connectivity += [
                            len(datum['nodes'][node]['neighbors'])
                        ]
                self.data.append(datum)

            if prepare_vocab:
                self.vocab = Dictionary(sentences, min_freq=3, split=True)
        print('{} split(s) loaded with {} instances'.format(
            ','.join(splits), len(self.data)))

        if 'nodes' in datum:
            print('min mean max connectivity %d %d %d' %
                  (np.min(connectivity), np.mean(connectivity),
                   np.max(connectivity)))
            self.max_connectivity = np.max(connectivity)

        else:
            self.max_connectivity = 0
        self.use_sentences = use_sentences
        self.use_gt_action = use_gt_action
        if self.use_sentences:
            self.sentence_ids = [0 for ii in range(self.batch_size)]
        self.oracle_mode = oracle_mode
        self.diff_threshold = 10
        self.coor_threshold = 10
        self.preprocess = transforms.Compose([
            transforms.ToTensor(),
        ])
        self.predictions = [None for ii in range(self.batch_size)]

        np.random.seed(seed)
        torch.manual_seed(seed)
예제 #6
0
class ZulipBot(object):
    def f(self, f_stop):

        print('timer..')

        self.client.send_message({
            "type": "stream",
            "to": 'general',
            "subject": 'swiming turtles',
            "content": 'Make sure to wash your hands.'
        })
        if not f_stop.is_set():
            # call f() again in 60 seconds
            threading.Timer(5 * 60, self.f, [f_stop]).start()

    def __init__(self):
        self.client = zulip.Client(site="https://nokia3310.zulipchat.com/api/")
        self.subscribe_all()
        # self.chatbot = ChatBot("Friday", trainer='chatterbot.trainers.ChatterBotCorpusTrainer')
        # self.chatbot.train("chatterbot.corpus.english")

        self.hacknews = Hackernews()

        self.dictionary = Dictionary()

        self.joke = Joke()

        self.motivate = Motivate()

        self.subkeys = [
            "joke", "news", "motivate", "help", "hi", "hello", "hey", "stat.*",
            "define", ["what", "is"]
        ]

    def urls(self, link):
        urls = re.findall(
            'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
            link)
        return urls

    def subscribe_all(self):
        json = self.client.get_streams()["streams"]

        stream_names = [{"name": stream["name"]} for stream in json]
        self.client.add_subscriptions(stream_names)

    def help(self):
        message = "**Welcome to Friday Bot**\nFriday Bot has various subfields\nType `friday help <subfield>` to get help for specific subfield.\n"
        message += "\n**Subfields**\n"

        message += "`joke` - Get Jokes\n"

        message += "`motivate` - Get a motivational quote\n"

        message += "`news <topic>` - Get Top News Results about a topic\n"

        message += "`coronavirus stats` - Get the current Covid-19 global/country statistics \n"

        message += "`friday <question>`Ask any query related to coronavirus.\n"

        message += "\nIf you're bored Talk to Friday Bot, it will supercharge you"
        return message

    def help_sub(self, key):
        key = key
        message = "**Usage**\n"

        if key == "define":
            message += "`friday define <word>` - To Get Definition of the word\n"
        elif key == "joke":
            message += "`friday joke` - Get a Joke\n"

        elif key == "mustread":
            message += "`friday mustread @**<User Mention>** <message>` - Pass Important Messages to Team Members\n"

        elif key == "news" or key == "news":
            message += "`friday news` OR `friday hackernews` - Show Top 10 stories News\n"

        else:
            message = self.help()
            message += "\n{} is not a valid subfield\n".format(key)
        return message

    def send(self, type, msg, message):

        # print('sending to:' + msg['display_recipient'][0]['email'])
        if type == 'stream':
            stream_name = stream_name = msg['display_recipient']
            stream_topic = msg['subject']

            self.client.send_message({
                "type": "stream",
                "to": stream_name,
                "subject": stream_topic,
                "content": message
            })
        else:
            if 'friday' in msg['display_recipient'][0]['email']:
                i = 1
            else:
                i = 0
            id = msg['display_recipient'][i]['email']
            self.client.send_message({
                "type": "private",
                "to": id,
                "content": message
            })

    def checkFriday(self, msg, type):

        content = msg["content"]

        #check for reference to friday
        if (type == 'private' and content[0] in ['hi', 'hello', 'hey']) or (
                len(content) > 1 and
            (((content[0] in ['hi', 'hello', 'hey']) and
              (content[1] == 'friday'
               or re.match('@\*\*friday.*', content[1]))) or
             ((content[1] in ['hi', 'hello', 'hey']) and
              (content[0] == 'friday'
               or re.match('@\*\*friday.*', content[0]))))):
            message = getHelloMessage(msg)
            self.send(type, msg, message)
            return
        elif type == 'stream' and (content[0] == 'friday'
                                   or '**friday' in content[0]):
            if len(content) > 1:

                index = 1
            else:
                index = 0

        elif type == 'private':
            if (content[0] == 'friday'
                    or '**friday' in content[0]) and len(content) > 1:
                index = 1
            else:
                index = 0

        else:
            return

        # #check for subkey
        #
        # flag = 0
        # for i in range(0, len(self.subkeys)):

        # 	for j in range(0, len(content)):

        # 		if( re.match(self.subkeys[i], content[j])):
        # 			flag = 1
        # 			key = j
        # 			#set a value here as index of condition
        # 			#then replace all elifs below (enws, joke) with that index
        # 			break

        # 	if(flag == 1):
        # 		break
        # else:
        # 	return

        print('index is:' + str(index))
        message = ''

        if ('family' in content
                or True in [bool(re.match('friend.*', i)) for i in content]
                or True in [bool(re.match('relative.*', i)) for i in content]
                or checkListContinsWords(
                    list=content,
                    words=[
                        'brother', 'sister', 'mother', 'father', 'uncle',
                        'aunt', 'girlfriend', 'boyfriend', 'son', 'daughter'
                    ] + ['pet', 'dog', 'cat', 'mouse', 'hamster', 'monkey'],
                    relation='any')):
            if (True in [bool(re.match('symptom.*', i)) for i in content]
                    or True in [bool(re.match('sign.*', i)) for i in content]
                ) or ('has' in content or 'got' in content
                      or 'have' in content) or (checkListContinsWords(
                          content, symptoms +
                          ['symptom', 'coronavirus', 'covid'], 'any')):
                message = 'The symptoms of coronavirus (fever, cough, sore throat, and trouble breathing) can look a lot like illnesses from other viruses. If a family member or friend or relative has trouble breathing, go to the emergency room or call an ambulance right away.\n\nCall your doctor if someone in your family or friends or a relative has a fever, cough, sore throat, or other flu-like symptoms. If this person has been near someone with coronavirus or lived in or traveled to an area where lots of people have coronavirus, tell the doctor. The doctor can decide whether your family member:\n\n\t* can be treated at home\n\t* should come in for a visit\n\t* can have a telehealth visit\n\t* needs to be tested for coronavirus'
                self.send(type, msg, message)
                familyFlag = True
        elif checkListContinsWords(
                list=content,
                words=['has', 'have', 'show', 'display', 'visible', 'draw'],
                relation='any') and 'i' in content and (checkListContinsWords(
                    list=content,
                    words=symptoms.extend(['symptom', 'coronavirus', 'covid']),
                    relation='any')):
            message = 'If you develop emergency warning signs for COVID-19 get medical attention immediately.'
            self.send(type, msg, message)
            return

        if (checkListContinsWords(
                list=content, words=['how', 'long', 'symptom'], relation='all')
                or checkListContinsWords(list=content,
                                         words=['how', 'long', 'sign'],
                                         relation='all')
                or checkListContinsWords(list=content,
                                         words=['when', 'will', 'symptom'],
                                         relation='all')
                or checkListContinsWords(list=content,
                                         words=['when', 'will', 'symptom'],
                                         relation='all')):
            message = 'The symptoms may appear 2-14 days after exposure to the virus:'
            self.send(type, msg, message)
            return

        if (content[index:index + 2] == ['what', 'is'
                                         ]) or content[index] == 'define':

            if (True
                    in [bool(re.match('.*coronavirus.*', i))
                        for i in content] or
                (True in [bool(re.match('.*covid.*', i)) for i in content])):
                message = 'Coronaviruses are a large family of viruses that are known to cause illness ranging from the common cold to more severe diseases such as Middle East Respiratory Syndrome (MERS) and Severe Acute Respiratory Syndrome (SARS).\nCoronaviruses are a large family of viruses that are known to cause illness ranging from the common cold to more severe diseases such as Middle East Respiratory Syndrome (MERS) and Severe Acute Respiratory Syndrome (SARS).'
                self.send(type, msg, message)
                return

            elif ('of' in content):
                i = content.index('of')
                word = content[i + 1]
                message = word + ': ' + self.dictionary.words(
                    word).capitalize()
                self.send(type, msg, message)
                return

            elif len(content) == index + 3:

                word = content[index + 2]
                message = word + ': ' + self.dictionary.words(
                    word).capitalize()
                self.send(type, msg, message)
                return

            elif len(content) == index + 2:

                word = content[index + 1]
                message = word + ': ' + self.dictionary.words(
                    word).capitalize()
                self.send(type, msg, message)
                return

            if checkListContinsWords(list=content,
                                     words=['incubation'],
                                     relation='any'):
                message = 'The symptoms may appear 2-14 days after exposure to the virus:'
                self.send(type, msg, message)
                return

        elif (content[index:index + 2] == ['what', 'are']):

            if checkListContinsWords(list=content,
                                     words=['symptom', 'sign'],
                                     relation='any'):
                message = 'Common signs include respiratory symptoms, fever, cough, shortness of breath, and breathing difficulties. In more severe cases, infection can cause pneumonia, severe acute respiratory syndrome, kidney failure and even death.'
                self.send(type, msg, message)
                return

            # if (True in [bool(re.match('.*coronavirus.*', i)) for i in content] or (True in [bool(re.match('.*covid.*', i)) for i in content])):
            # 	message = 'Coronaviruses are a large family of viruses that are known to cause illness ranging from the common cold to more severe diseases such as Middle East Respiratory Syndrome (MERS) and Severe Acute Respiratory Syndrome (SARS).\nCoronaviruses are a large family of viruses that are known to cause illness ranging from the common cold to more severe diseases such as Middle East Respiratory Syndrome (MERS) and Severe Acute Respiratory Syndrome (SARS).'
            # 	self.send(type, msg, message)

        elif (content[index] == 'can'):

            if (True in [bool(re.match('.*transmit.*', i)) for i in content]):
                message = 'The novel coronavirus can definitely be transmitted from an infected person to another person.'
                self.send(type, msg, message)
                return
            if (True in [bool(re.match('.*pet.*', i)) for i in content]
                    or True in [bool(re.match('.*dog.*', i)) for i in content]
                    or True in [bool(re.match('.*cat.*', i))
                                for i in content]):
                message += '\nThe answer for your pets varies. Visit: https://www.independent.co.uk/life-style/health-and-families/coronavirus-pet-dog-can-you-catch-it-transmission-a9376926.html'
                self.send(type, msg, message)
                return
            if ['no', 'symptom'
                ] in content or ['no', 'symptoms'] in content or (
                    'without' in content and True
                    in [bool(re.match('.*symptom.*', i)) for i in content]):
                message = 'It is possisble for you to have Covid-19 without showing any symptoms and be able to transmit it to others.'
                self.send(type, msg, message)
                return
            if ((True
                 in [bool(re.match('.*coronavirus.*', i)) for i in content] or
                 (True in [bool(re.match('.*covid.*', i))
                           for i in content]))) and 'get' in content:
                message = 'You can catch coronavirus from others who have the virus. This happens when an infected person sneezes or coughs, sending tiny droplets into the air. These can land in the nose, mouth, or eyes of someone nearby, or be breathed in. You also can get infected if they touch an infected droplet on a surface and then touch their own nose, mouth, or eyes.'
                self.send(type, msg, message)
                return

        if 'how' in content or len([
            (x, y) for x, y in zip(['am', 'i'][0:], ['am', 'i'][1:])
                if x in content and y in content
        ]) > 0 or len([(x, y) for x, y in zip(['i', 'am'][0:], ['i', 'am'][1:])
                       if x in content and y in content]) > 0:

            if (('likely' in content or 'risk' in content) and
                ((True
                  in [bool(re.match('.*coronavirus.*', i))
                      for i in content]) or
                 (True in [bool(re.match('.*covid.*', i)) for i in content])
                 or 'disease' in content or 'virus' in content
                 or len([(x, y)
                         for x, y in zip(['get', 'it'][0:], ['get', 'it'][1:])
                         if x in content and y in content]) > 0 or
                 len([(x, y)
                      for x, y in zip(['catch', 'it'][0:], ['catch', 'it'][1:])
                      if x in content and y in content]) > 0
                 or len([(x, y) for x, y in zip(['contract', 'it'][0:],
                                                ['contract', 'it'][1:])
                         if x in content and y in content]) > 0) and
                (True in [bool(re.match('get.*', i)) for i in content] or True
                 in [bool(re.match('.*contract.*', i)) for i in content]
                 or True in [bool(re.match('catch.*', i)) for i in content]
                 or 'from' in content)) or checkListContinsWords(
                     list=content, words=['safe'], relation='any'):

                if 'i' in content:
                    message = 'As long as you are inside your house and following quarantine.. you are safe'
                    self.send(type, msg, message)
                    return
                elif checkListContinsWords(list=content,
                                           words=['pet'],
                                           relation='any'):
                    message = 'The answer for your pets varies. Visit: https://www.independent.co.uk/life-style/health-and-families/coronavirus-pet-dog-can-you-catch-it-transmission-a9376926.html'
                    self.send(type, msg, message)
                    return
                #add health worker

            elif ((True
                   in [bool(re.match('.*coronavirus.*', i))
                       for i in content] or
                   (True in [bool(re.match('.*covid.*', i))
                             for i in content]))) and 'get' in content:
                message = 'You can catch coronavirus from others who have the virus. This happens when an infected person sneezes or coughs, sending tiny droplets into the air. These can land in the nose, mouth, or eyes of someone nearby, or be breathed in. You also can get infected if they touch an infected droplet on a surface and then touch their own nose, mouth, or eyes.'
                self.send(type, msg, message)
                return

        elif checkListContinsWords(
                list=content,
                words=['pet', 'dog', 'cat', 'mouse', 'hamster', 'monkey'],
                relation='any') and checkListContinsWords(
                    list=content,
                    words=['safe', 'danger', 'vulnerabl', 'risk'],
                    relation='any'):
            message = 'The answer for your pets varies. Visit: https://www.independent.co.uk/life-style/health-and-families/coronavirus-pet-dog-can-you-catch-it-transmission-a9376926.html'
            self.send(type, msg, message)
            return

        if (content[index:index + 2] in [['how', 'to'], ['how', 'often'],
                                         ['how', 'much'], ['how', 'is'],
                                         ['how', 'should'], ['how', 'can'],
                                         ['how', 'may'], ['how', 'could'],
                                         ['relative', 'it'],
                                         ['what', 'should'], ['what', 'can'],
                                         ['what', 'could'], ['is', 'there'],
                                         ['are', 'there'], ['should', 'i'],
                                         ['should', 'we']]):

            if checkListContinsWords(list=content,
                                     words=['travel', 'plan'],
                                     relation='all'):
                message = 'Is there any place abroad that’s safe to travel right now? In a nutshell, no. The novel coronavirus has spread to more than 100 countries and every continent except for Antarctica.\nI recommend you don\'t travel anywhere outside, even another state or town for atleast a few months. This is for your own safety'
                self.send(type, msg, message)
                return

            elif checkListContinsWords(list=content,
                                       words=['out'],
                                       relation='any'):
                message = 'You should not go outside for any purpose other than for groceries, washroom stuff or medical stuff and that too only once a day for a short time.\nAnd remember, don\'t go too far from your home and always carry your ID proof when out!'
                self.send(type, msg, message)
                return
            elif ('family' in content
                  or True in [bool(re.match('friend.*', i))
                              for i in content] or True
                  in [bool(re.match('relative.*', i)) for i in content]):
                if (True in [bool(re.match('symptom.*', i))
                             for i in content] or True
                        in [bool(re.match('sign.*', i))
                            for i in content]) or ('has' in content
                                                   or 'got' in content):
                    if (familyFlag == True):
                        familyFlag = False
                    else:
                        message = 'The symptoms of coronavirus (fever, cough, sore throat, and trouble breathing) can look a lot like illnesses from other viruses. If a family member or friend or relative has trouble breathing, go to the emergency room or call an ambulance right away.\n\nCall your doctor if someone in your family or friends or a relative has a fever, cough, sore throat, or other flu-like symptoms. If this person has been near someone with coronavirus or lived in or traveled to an area where lots of people have coronavirus, tell the doctor. The doctor can decide whether your family member:\n\n\t* can be treated at home\n\t* should come in for a visit\n\t* can have a telehealth visit\n\t* needs to be tested for coronavirus'
                        self.send(type, msg, message)
                        return
            if (True in [bool(re.match('.*prevent.*', i)) for i in content]
                    or True
                    in [bool(re.match('.*protect.*', i)) for i in content]):
                message = 'Standard recommendations to reduce exposure to and transmission of a range of illnesses include maintaining basic hand and respiratory hygiene, and safe food practices  and avoiding close contact, when possible, with anyone showing symptoms of respiratory illness such as coughing and sneezing. \n#StayHomeStaySafe'
                self.send(type, msg, message)
                return

            elif (
                (True in [bool(re.match('.*cure.*', i)) for i in content]
                 or True in [bool(re.match('.*treat.*', i)) for i in content]
                 or True in [bool(re.match('.*vaccin.*', i))
                             for i in content]) and
                (True
                 in [bool(re.match('.*coronavirus.*', i)) for i in content] or
                 (True in [bool(re.match('.*covid.*', i)) for i in content])
                 or len([(x, y) for x, y in zip(['the', 'disease'][0:],
                                                ['the', 'disease'][1:])
                         if x in content and y in content]) > 0
                 or len([(x, y)
                         for x, y in zip(['for', 'it'][0:], ['for', 'it'][1:])
                         if x in content and y in content]) > 0
                 or len([(x, y) for x, y in zip(['the', 'virus'][0:],
                                                ['the', 'virus'][1:])
                         if x in content and y in content]) > 0)):
                message = 'There is no specific treatment for disease caused by a novel coronavirus. However, many of the symptoms can be treated and therefore treatment based on the patient’s clinical condition. Moreover, supportive care for infected persons can be highly effective.'
                self.send(type, msg, message)
                return

        elif checkListContinsWords(list=content,
                                   words=['motivat'],
                                   relation='any'):
            message = self.motivate.get_quote()
            self.send(type, msg, message)
            return

        elif content[index] == "joke" or (content[index]
                                          in ["tell", "crack", "show"]
                                          and "joke" in content[index + 1:]):
            text = self.joke.tellJoke()
            self.send(type, msg, text)

        elif 'news' in content[index:]:
            print('news1')
            if len([x for x in content[index:] if re.match('corona.*', x)
                    ]) > 0 or len([
                        x for x in content[index:] if re.match('covid.*', x)
                    ]) > 0:

                print('news corona india')
                if 'india' in content[index:]:
                    news = self.hacknews.get_hackernews('coronavirus india')
                    self.send(type, msg, news)

                else:
                    print('news corona')
                    news = self.hacknews.get_hackernews('coronavirus')
                    self.send(type, msg, news)

        elif content[index] == "help" and len(content) == index + 1:
            message = self.help()
            self.send(type, msg, message)
        elif content[index] == "help" and len(content) > index + 1:
            subkey = content[index + 1]
            message = self.help_sub(subkey)
            self.send(type, msg, message)

        elif (
                len([x
                     for x in content[index:] if re.match('corona.*', x)]) > 0
                or
                len([x for x in content[index:] if re.match('covid.*', x)]) > 0
        ) and (len([x
                    for x in content[index:] if re.match('stat.*', x)]) > 0 or
               len([x for x in content[index:] if re.match('statistic.*', x)
                    ]) > 0):
            j = -1

            for i in content:
                if i in countries:
                    j = countries.index(i)
                    break

            if j >= 0:
                x = requests.get('https://api.covid19api.com/summary').json()
                x = x['Countries']
                for i in range(0, len(x)):
                    if (x[i]['CountryCode'] == list(
                            pycountry.countries)[j].alpha_2):
                        stats = 'New Confirmed: ' + str(
                            x[i]['NewConfirmed']
                        ) + '\nTotal Confirmed: ' + str(
                            x[i]['TotalConfirmed']) + '\nNew Deaths: ' + str(
                                x[i]['NewDeaths']) + '\nTotal Deaths: ' + str(
                                    x[i]['TotalDeaths']
                                ) + '\nNew Recovered: ' + str(
                                    x[i]['NewRecovered']
                                ) + '\nTotal Recovered: ' + str(
                                    x[i]['TotalRecovered'])
                        self.send(
                            type, msg, 'Here are ' + countries[j][0].upper() +
                            countries[j][1:] + '\'s statistics for today\n\n' +
                            stats)

            else:

                #call api
                x = requests.get(
                    'https://api.covid19api.com/world/total').json()
                stats = '\nTotal Confirmed: ' + str(
                    x['TotalConfirmed']) + '\nTotalDeaths: ' + str(
                        x['TotalDeaths']) + '\nTotalRecovered: ' + str(
                            x['TotalRecovered'])  #global
                # self.send(type, msg, stats)
                self.send(type, msg,
                          'Here are the global statistics \n\n' + stats)
                self.send(
                    type, msg,
                    '\n\ndone Global stats. \nFor countries stats, write: friday [country] covid19 stats'
                )

        elif "friday" in content:
            message = "Alas! Finally you called me :blush:"
            self.send(type, msg, message)

        else:
            return

    def process(self, msg):
        pprint.pprint(msg)

        print('\n\n' + msg['type'] + ' - ' +
              (msg['display_recipient'] + ' - ' +
               msg['subject'] if msg['type'] == 'stream' else '') + '\n' +
              msg['sender_full_name'] + ' sent a message: ' +
              str([x.lower() for x in msg['content'].split()]) + '\n\n')

        sender_email = msg["sender_email"]
        if sender_email == BOT_MAIL:
            return

        content = msg["content"].split()

        content = [x.lower() for x in content]
        content = [x.replace("?", "") for x in content]
        content = [x.replace(",", "") for x in content]
        content = [x.replace("!", "") for x in content]

        msg['content'] = content

        if (msg['type'] == 'private'):

            type = 'private'
            self.checkFriday(msg, type)

        else:
            type = 'stream'
            self.checkFriday(msg, type)
예제 #7
0
def main():

    # establish the path to the file
    essay = os.path.join("/home/ubuntu/workspace/text_assessment/samples",
                         "essay.txt")
    if not essay:
        sys.exit("Could not find the path")
    # instantiate the object
    analyzer = Analyzer(essay)

    # Our program uses Victoria University of Wellington's range software vocabulary list
    # For further information on that, please visit and download it at http://www.victoria.ac.nz/lals/resources/range
    # establish the paths for the dictionaries
    dict1 = os.path.join("/home/ubuntu/workspace/text_assessment/dicts",
                         "gsl_1st.txt")
    dict2 = os.path.join("/home/ubuntu/workspace/text_assessment/dicts",
                         "gsl_2nd.txt")
    dict3 = os.path.join("/home/ubuntu/workspace/text_assessment/dicts",
                         "gsl_3rd.txt")
    if not dict1 or not dict2 or not dict3:
        sys.exit("Could not find the path")
    # instantiate the object
    dictionary = Dictionary()
    loaded = dictionary.load(dict1, dict2, dict3)
    if not loaded:
        sys.exit("Could not load the dictionaries")
    # make the counters for each dictionary
    count1, count2, count3 = 0, 0, 0
    tokens = analyzer.return_tokens(essay)
    for token in tokens:
        if token not in string.punctuation:
            if dictionary.check(token) == 1:
                count1 = count1 + 1
            if dictionary.check(token) == 2:
                count2 += 1
            if dictionary.check(token) == 3:
                count3 += 1
            else:
                continue
    # twd stands for "total words detected"
    twd = count1 + count2 + count3

    # call the functions
    print 'Number of words analyzed: ', twd
    print 'Vocabulary level: \n' + "\tEasy: " + "{0:.2f}".format(
        count1 * 100.0 / twd) + '%'
    print '\tIntermediate: ' + "{0:.2f}".format(count2 * 100.0 / twd) + '%'
    print '\tAdvanced: ' + "{0:.2f}".format(count3 * 100.0 / twd) + '%'
    print 'Number of words: ', (analyzer.word_count())
    print 'Number of sentences: ', analyzer.number_of_sentences()
    print 'Number of complex words: ', analyzer.complex_words()
    print 'Number of syllables: ', analyzer.syllables()
    print 'Number of characters: ', analyzer.number_of_characters()
    print 'Number of unique words: ', analyzer.unique_word_count()
    print 'Characters per word: ', "{0:.2f}".format(
        analyzer.number_of_characters() / (analyzer.word_count() * 1.0))
    print 'Syllables per word: ', "{0:.2f}".format(
        analyzer.syllables() / (analyzer.word_count() * 1.0))
    print 'Words per sentence: ', "{0:.2f}".format(
        analyzer.word_count() / (analyzer.number_of_sentences() * 1.0))
    print '              DEEP ANALYSIS(in percent)          '
    analyzer.deep_analysis()