Best Python code snippet using autotest_python
TweetsClassify.py
Source: TweetsClassify.py
1#!/usr/bin/python2#-*-coding:utf-8-*-3'''@author:duncan'''4import pickle5import config6import os7import TweetsClassifyTraining8project_path = config.project_path9data_folder_path = "/TweetsSamples/"10pickle_path = project_path + "/DocumentClassify/pickles/"11tf_transformer_path = pickle_path + "tf_transformer.picle"12categories_path = pickle_path + "categories.pickle"13count_vect_path = pickle_path + "count_vect.pickle"14def Classify(text,Classifier):15 '''16 :param text:å¾
åç±»çææ¬17 :param Classifier:åç±»å¨ç±»å«18 :return: è¿ååç±»ç»æ19 '''20 if text == "" or text == None:21 return "None"22 txt = []23 # print text24 txt.append(text)25 # å¦æ模åæä¹
åæ件ä¸åå¨åéè¦å
è®ç»26 if len(os.listdir(config.project_path + "/DocumentClassify/pickles/")) <= 6:27 TweetsClassifyTraining.Training()28 # æµè¯æ°æ®è½¬å为ç¹å¾åé29 open_file = open(count_vect_path)30 count_vect = pickle.load(open_file)31 open_file.close()32 x_test_counts = count_vect.transform(txt)33 open_file = open(tf_transformer_path)34 tf_transformer = pickle.load(open_file)35 open_file.close()36 x_test_tf = tf_transformer.transform(x_test_counts)37 # éæ©åç±»å¨38 classifier_path = pickle_path + Classifier + "_classifier.pickle"39 # åç±»40 open_file = open(classifier_path)41 clf = pickle.load(open_file)42 open_file.close()43 open_file = open(categories_path)44 target_names = pickle.load(open_file)45 open_file.close()46 result = target_names[clf.predict(x_test_tf.toarray())[0]]47 return result48# å¤æ¨¡åèå,ç»æ¯ä¸ªæ¨¡åç»æç¸åºçæé[0.4,0.3,0.1,0.1,0.1]49def Classify_MultiModels(text,classifiers,weight):50 if text == "" or text == None:51 return "None"52 txt = []53 # print text54 txt.append(text)55 # æµè¯æ°æ®è½¬å为ç¹å¾åé56 open_file = open(count_vect_path)57 count_vect = pickle.load(open_file)58 open_file.close()59 x_test_counts = count_vect.transform(txt)60 open_file = open(tf_transformer_path)61 tf_transformer = pickle.load(open_file)62 open_file.close()63 x_test_tf = tf_transformer.transform(x_test_counts)64 results = []65 results_set = []66 classify_result = {}67 for (Classifier,i) in zip(classifiers,range(len(classifiers))):68 # éæ©åç±»å¨69 classifier_path = pickle_path + Classifier + "_classifier.pickle"70 # åç±»71 open_file = open(classifier_path)72 clf = pickle.load(open_file)73 open_file.close()74 open_file = open(categories_path)75 target_names = pickle.load(open_file)76 open_file.close()77 result = target_names[clf.predict(x_test_tf.toarray())[0]]78 results.append((result,weight[i]))79 results_set.append(result)80 results_set = set(results_set)81 for res in results_set:82 value = 083 for tuple in results:84 if res == tuple[0]:85 value += tuple[1]86 classify_result[res] = value87 classify_result = sorted(classify_result.items(),key = lambda dic:dic[1],reverse = True)88 return classify_result[:1][0][0]89def Accuracy(resdic,users):90 '''91 :param resdic: æ ¼å¼: {screen_name:category}92 :param users: æ ¼å¼: {name,screen_name,category}93 :return:åç¡®ç94 '''95 correct = 096 if isinstance(users,list):97 for dickey in resdic.keys():98 for user in users:99 if dickey == user.screen_name and resdic[dickey] == user.category:100 correct += 1101 break102 else:103 for dickey in resdic.keys():104 for id in users.keys():105 if dickey == id:106 if resdic[dickey] == users[id]:107 correct += 1108 break109 return (correct * 1.0 / len(resdic))110# æµè¯åç±»å¨ææ111def test():112 # 读å20个å人screenname/name/æ 注åç±»113 # open_file = open(pickle_path + "20famous.pickle")114 # famous = pickle.load(open_file)115 # open_file.close()116 # famous_screen_name = os.listdir(famouse_tweets_folder_path)117 # # print famous_screen_name118 # correct = 0119 # for name in famous_screen_name:120 # filename = name121 # file_path = famouse_tweets_folder_path + filename122 # text = getTweets(file_path)123 # category = Classify(text)124 # for user in famous:125 # if name == user[0] and user[2] == category:126 # correct += 1127 # break128 # print "%s => %s" % (name,category)129 # print "以æ 注ç20个å人为ååç¡®ç为:"130 # print (correct * 1.0 / 20)131 text = """Even as China needs to reassure the international community that it has no aggressive intentions, which it is trying to do with its modest military budget increase this year, it is caught in a bit of a bind.China seeks to become a major world power, and one of the hallmarks of such a status is blue-water capability and the ability to project military might globally;"""132 print Classify(text)...
sentiment_load.py
Source: sentiment_load.py
1import nltk2import random3#from nltk.corpus import movie_reviews4from nltk.classify.scikitlearn import SklearnClassifier5import pickle6from sklearn.naive_bayes import MultinomialNB, BernoulliNB7from sklearn.linear_model import LogisticRegression, SGDClassifier8from sklearn.svm import SVC, LinearSVC, NuSVC9from nltk.classify import ClassifierI10from statistics import mode11from nltk.tokenize import word_tokenize12class VoteClassifier(ClassifierI):13 def __init__(self, *classifiers):14 self._classifiers = classifiers15 def classify(self, features):16 votes = []17 for c in self._classifiers:18 v = c.classify(features)19 votes.append(v)20 return mode(votes)21 def confidence(self, features):22 votes = []23 for c in self._classifiers:24 v = c.classify(features)25 votes.append(v)26 choice_votes = votes.count(mode(votes))27 conf = choice_votes / len(votes)28 return conf29documents_f = open("pickled_algos/documents.pickle", "rb")30documents = pickle.load(documents_f)31documents_f.close()32word_features5k_f = open("pickled_algos/word_features5k.pickle", "rb")33word_features = pickle.load(word_features5k_f)34word_features5k_f.close()35def find_features(document):36 words = word_tokenize(document)37 features = {}38 for w in word_features:39 features[w] = (w in words)40 return features41#featuresets_f = open("pickled_algos/featuresets.pickle", "rb")42#featuresets = pickle.load(featuresets_f)43#featuresets_f.close()44#random.shuffle(featuresets)45#print(len(featuresets))46#testing_set = featuresets[10000:]47#training_set = featuresets[:10000]48open_file = open("pickled_algos/originalnaivebayes5k.pickle", "rb")49classifier = pickle.load(open_file)50open_file.close()51open_file = open("pickled_algos/MNB_classifier5k.pickle", "rb")52MNB_classifier = pickle.load(open_file)53open_file.close()54open_file = open("pickled_algos/BernoulliNB_classifier5k.pickle", "rb")55BernoulliNB_classifier = pickle.load(open_file)56open_file.close()57open_file = open("pickled_algos/LogisticRegression_classifier5k.pickle", "rb")58LogisticRegression_classifier = pickle.load(open_file)59open_file.close()60open_file = open("pickled_algos/LinearSVC_classifier5k.pickle", "rb")61LinearSVC_classifier = pickle.load(open_file)62open_file.close()63open_file = open("pickled_algos/SGDC_classifier5k.pickle", "rb")64SGDC_classifier = pickle.load(open_file)65open_file.close()66voted_classifier = VoteClassifier(67 classifier,68 LinearSVC_classifier,69 MNB_classifier,70 BernoulliNB_classifier,71 LogisticRegression_classifier)72def sentiment(text):73 feats = find_features(text)...
sentiment_mod.py
Source: sentiment_mod.py
1import nltk2import random3#from nltk.corpus import movie_reviews4from nltk.classify.scikitlearn import SklearnClassifier5import pickle6from sklearn.naive_bayes import MultinomialNB, BernoulliNB7from sklearn.linear_model import LogisticRegression, SGDClassifier8from sklearn.svm import SVC, LinearSVC, NuSVC9from nltk.classify import ClassifierI10from statistics import mode11from nltk.tokenize import word_tokenize12class VoteClassifier(ClassifierI):13 def __init__(self, *classifiers):14 self._classifiers = classifiers15 def classify(self, features):16 votes = []17 for c in self._classifiers:18 v = c.classify(features)19 votes.append(v)20 return mode(votes)21 def confidence(self, features):22 votes = []23 for c in self._classifiers:24 v = c.classify(features)25 votes.append(v)26 choice_votes = votes.count(mode(votes))27 conf = choice_votes / len(votes)28 return conf29documents_f = open("pickled_algos/documents.pickle", "rb")30documents = pickle.load(documents_f)31documents_f.close()32word_features5k_f = open("pickled_algos/word_features5k.pickle", "rb")33word_features = pickle.load(word_features5k_f)34word_features5k_f.close()35def find_features(document):36 words = word_tokenize(document)37 features = {}38 for w in word_features:39 features[w] = (w in words)40 return features41featuresets_f = open("pickled_algos/featuresets.pickle", "rb")42featuresets = pickle.load(featuresets_f)43featuresets_f.close()44random.shuffle(featuresets)45testing_set = featuresets[10000:]46training_set = featuresets[:10000]47open_file = open("pickled_algos/originalnaivebayes5k.pickle", "rb")48classifier = pickle.load(open_file)49open_file.close()50open_file = open("pickled_algos/MNB_classifier5k.pickle", "rb")51MNB_classifier = pickle.load(open_file)52open_file.close()53open_file = open("pickled_algos/BernoulliNB_classifier5k.pickle", "rb")54BernoulliNB_classifier = pickle.load(open_file)55open_file.close()56open_file = open("pickled_algos/LogisticRegression_classifier5k.pickle", "rb")57LogisticRegression_classifier = pickle.load(open_file)58open_file.close()59open_file = open("pickled_algos/LinearSVC_classifier5k.pickle", "rb")60LinearSVC_classifier = pickle.load(open_file)61open_file.close()62open_file = open("pickled_algos/SGDC_classifier5k.pickle", "rb")63SGDC_classifier = pickle.load(open_file)64open_file.close()65voted_classifier = VoteClassifier(66 classifier,67 LinearSVC_classifier,68 MNB_classifier,69 BernoulliNB_classifier,70 LogisticRegression_classifier)71def sentiment(text):72 feats = find_features(text)...
cakebot.py
Source: cakebot.py
1import nltk2import random 3from nltk.classify.scikitlearn import SklearnClassifier4import pickle56from sklearn.naive_bayes import MultinomialNB, GaussianNB, BernoulliNB #7 different algorithms7from sklearn.linear_model import LogisticRegression, SGDClassifier8from sklearn.svm import SVC, LinearSVC, NuSVC9from nltk.classify import ClassifierI10from statistics import mode1112from nltk.tokenize import word_tokenize1314class VoteClassifier(ClassifierI):15 def __init__(self, *classifiers):16 self._classifiers = classifiers17 18 def classify(self, features):19 votes = []20 for c in self._classifiers:21 v = c.classify(features)22 votes.append(v)23 return mode(votes) #return most votes24 25 def confidence(self, features):26 votes = []27 for c in self._classifiers:28 v = c.classify(features)29 votes.append(v)3031 choice_votes = votes.count(mode(votes))32 conf = choice_votes / len(votes)33 return conf3435documents_f = open("pickles/documents.pickle", "rb")36documents = pickle.load(documents_f)37documents_f.close()3839word_features_over9k_f = open("pickles/word_features_over9k.pickle", "rb")40word_features = pickle.load(word_features_over9k_f)41word_features_over9k_f.close()4243def find_features(document):44 words = word_tokenize(document)45 features = {}46 for w in word_features:47 features[w] = (w in words)48 return features4950featuresets_f = open("pickles/featuresets.pickle", "rb")51featuresets = pickle.load(featuresets_f)52featuresets_f.close() 5354random.shuffle(featuresets)5556#positive data example:57training_set = featuresets[:10000]58testing_set = featuresets[10000:]5960#Loading the 7 pickled classifiers61open_file = open("pickles/orig_classifier.pickle", "rb")62classifier = pickle.load(open_file)63open_file.close()6465open_file = open("pickles/MNB_classifier.pickle", "rb")66MNB_classifier = pickle.load(open_file)67open_file.close()6869open_file = open("pickles/BernoulliNB_classifier.pickle", "rb")70BernoulliNB_classifier = pickle.load(open_file)71open_file.close()7273open_file = open("pickles/LogisticRegression_classifier.pickle", "rb")74LogisticRegression_classifier = pickle.load(open_file)75open_file.close()7677open_file = open("pickles/SGDClassifier_classifier.pickle", "rb")78SGDClassifier_classifier = pickle.load(open_file)79open_file.close()8081open_file = open("pickles/LinearSVC_classifier.pickle", "rb")82LinearSVC_classifier = pickle.load(open_file)83open_file.close()8485open_file = open("pickles/NuSVC_classifier.pickle", "rb")86NuSVC_classifier = pickle.load(open_file)87open_file.close()8889voted_classifier = VoteClassifier(classifier,MNB_classifier,BernoulliNB_classifier,LogisticRegression_classifier,SGDClassifier_classifier,LinearSVC_classifier,NuSVC_classifier)909192def sentiment(text):93 feats = find_features(text)94 return voted_classifier.classify(feats), voted_classifier.confidence(feats)95
...
Check out the latest blogs from LambdaTest on this topic:
Enterprise resource planning (ERP) is a form of business process management software—typically a suite of integrated applications—that assists a company in managing its operations, interpreting data, and automating various back-office processes. The introduction of a new ERP system is analogous to the introduction of a new product into the market. If the product is not handled appropriately, it will fail, resulting in significant losses for the business. Most significantly, the employees’ time, effort, and morale would suffer as a result of the procedure.
In today’s data-driven world, the ability to access and analyze large amounts of data can give researchers, businesses & organizations a competitive edge. One of the most important & free sources of this data is the Internet, which can be accessed and mined through web scraping.
Recently, I was going through some of the design patterns in Java by reading the book Head First Design Patterns by Eric Freeman, Elisabeth Robson, Bert Bates, and Kathy Sierra.
Collecting and examining data from multiple sources can be a tedious process. The digital world is constantly evolving. To stay competitive in this fast-paced environment, businesses must frequently test their products and services. While it’s easy to collect raw data from multiple sources, it’s far more complex to interpret it properly.
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!