def alpha_words(name):
    # open dictionary
    source = 'Emotion\\'
    json_pattern = os.path.join(source, '*.json')
    file_list = glob.glob(json_pattern)

    # for each dictionary
    for file_emo in file_list:
        load_file = json.load(open(file_emo, 'r'))
        # print(load_file)
        # read in any json file that comes in/ using glob for filename pattern matching
        source = 'Tracker\\'+name
        json_pattern = os.path.join(source, '*.json')
        file_list = glob.glob(json_pattern)
        for file in file_list:
            # open each file in file list
            target_doc = open(file, 'r')
            Functions.num_of_tweets(file)

            res = []
            # for each line in document
            for lines in target_doc:
                word_list = []
                line = lines.lower()
                word = Functions.preprocess(line)
                for i in word:
                    # append to word list to be sorted
                    word_list.append(i)

                # sort each line in alphabetic order
                word_list.sort()
                #print(word_list)

                # check if the dictionary word is in the list
                dict_words = load_file["words"][0]
                if len(dict_words) == 0:
                    id_ = load_file["id"]
                    wrd = 0
                    di_ct = {id_: wrd}
                    res.append(di_ct)
                else:
                    for X in dict_words:
                        # print(X)
                        if Functions.binary_search(word_list, X) is True:
                            id_ = load_file["id"]
                            wrd = dict_words[X]
                            di_ct = {id_: wrd}
                            # print(di_ct)
                            res.append(di_ct)
                        else:
                            id_ = load_file["id"]
                            wrd = 0
                            di_ct = {id_: wrd}
                            res.append(di_ct)
        Functions.counting(res, file,"19-02-2016",200,name)
def emotion_measure(name):
    # read in any json file that comes in/ using glob for filename pattern matching
    source = 'Tracker\\'+name
    json_dir = source
    json_pattern = os.path.join(json_dir, '*.json')
    file_list = glob.glob(json_pattern)

    for file in file_list:

        print(file)
        target_doc = open(file, 'r')
        res = []
        time_stamp = Functions.time_stamp(file)
        num_tweets = Functions.num_of_tweets(file)
        Functions.num_of_tweets(file)
        for lines in target_doc:
            # print(lines)
            line = lines.lower()
            word = twitterstreamV2.preprocess(line)

            source = 'Emotion\\'
            json_dir = source
            json_pattern = os.path.join(json_dir, '*.json')
            file_list = glob.glob(json_pattern)

            for file_emo in file_list:
                emo_doc = open(file_emo, 'r')
                load_file = json.load(emo_doc)

                for wd in word:
                    # checks if the word exists in the dictionary and prints it out
                    dict_words = load_file["words"][0]
                    if wd in dict_words:
                        id_ = load_file["id"]
                        wrd = dict_words[wd]
                        di_ct = {id_: wrd}
                        res.append(di_ct)
                        #print(wd)
                        #print(di_ct)
                    else:
                        id_ = load_file["id"]
                        wrd = 0
                        di_ct = {id_: wrd}
                        res.append(di_ct)

        target_doc.close()
        #print(res)
        Functions.counting(res, file, time_stamp, num_tweets, name)
Beispiel #3
0
    def emotion_analysis(self,word_list,file,time_stamp,num_tweets):

        try:
            name = self.name
            # open dictionary and order words alphabetically
            dict_file = self.dictionary
            dictionary = open(dict_file, "r")
            di_ct = json.load(dictionary)
            ordered = Functions.word_order(di_ct)

            # obtain bigrams for detecting negation
            grams = list(bigrams(word_list))
            negative_flag = False
            intensifier_flag = False
            Word_found = False
            print(grams)
            intensi = []
            negate = []
            for i in grams:
                for n in negation_list:
                    if n in i:
                        negate.append(i)
                for n in intensifier_list:
                    if n in i:
                        intensi.append(i)
            print("this is x :", intensi)
            print(negate)
            print("________________________")
            # print(word_list)
            for x in word_list:
                # Check for any negative words or intensifiers
                # print(x, stemmer.stem(x))
                for i in grams:
                    if x in i:
                        # check for negation
                        for n in negation_list:
                            if n not in i:
                                pass
                            else:
                                negative_flag = True
                        # check for intensifier
                        for n in intensifier_list:
                            if n not in i:
                                pass
                            else:
                                intensifier_flag = True
                # check if word or stem of word is in dictionary
                if Functions.binary_search(self, ordered, x) is True:
                    if negative_flag is True:
                        count = 1
                        negation_counter.append(count)
                        pass
                    else:
                        if di_ct[x]:
                            i_d = di_ct[x][1]
                            if intensifier_flag is True:
                                wrd = di_ct[x][0] + 1
                                intensifier_flag = False
                            else:
                                wrd = di_ct[x][0]
                            new_d = {i_d: abs(wrd)}
                            res.append(new_d)
                            # print(new_d)

                elif Functions.binary_search(self,ordered,stemmer.stem(x))is True:
                    x_stem = stemmer.stem(x)
                    # handles negation and intensifier occurrence
                    if negative_flag is True:
                        # negation_counter += 1
                        pass
                    else:
                        if di_ct[x_stem]:
                            # print(x)
                            i_d = di_ct[x_stem][1]
                            if intensifier_flag is True:
                                wrd = di_ct[x_stem][0] + 1
                                intensifier_flag = False
                            else:
                                wrd = di_ct[x_stem][0]
                            new_d = {i_d: abs(wrd)}
                            res.append(new_d)
                            # print(new_d)
            # print("number of tweets :" , num_tweets)
            # Company.create_graph(self,name)
            # Company.create_csv(self, name)
            # dictionary.close()
            Functions.counting(self, res, file, time_stamp, num_tweets,name)
            return res

        except BaseException as e:

            print ("emotion analysis error: ", e)