How to use open_file method in molecule

Best Python code snippet using molecule_python

TweetsClassify.py

Source: TweetsClassify.py Github

copy

Full Screen

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)...

Full Screen

Full Screen

sentiment_load.py

Source: sentiment_load.py Github

copy

Full Screen

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)...

Full Screen

Full Screen

sentiment_mod.py

Source: sentiment_mod.py Github

copy

Full Screen

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)...

Full Screen

Full Screen

cakebot.py

Source: cakebot.py Github

copy

Full Screen

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 ...

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

Test Optimization for Continuous Integration

“Test frequently and early.” If you’ve been following my testing agenda, you’re probably sick of hearing me repeat that. However, it is making sense that if your tests detect an issue soon after it occurs, it will be easier to resolve. This is one of the guiding concepts that makes continuous integration such an effective method. I’ve encountered several teams who have a lot of automated tests but don’t use them as part of a continuous integration approach. There are frequently various reasons why the team believes these tests cannot be used with continuous integration. Perhaps the tests take too long to run, or they are not dependable enough to provide correct results on their own, necessitating human interpretation.

Acquiring Employee Support for Change Management Implementation

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.

Continuous delivery and continuous deployment offer testers opportunities for growth

Development practices are constantly changing and as testers, we need to embrace change. One of the changes that we can experience is the move from monthly or quarterly releases to continuous delivery or continuous deployment. This move to continuous delivery or deployment offers testers the chance to learn new skills.

How to increase and maintain team motivation

The best agile teams are built from people who work together as one unit, where each team member has both the technical and the personal skills to allow the team to become self-organized, cross-functional, and self-motivated. These are all big words that I hear in almost every agile project. Still, the criteria to make a fantastic agile team are practically impossible to achieve without one major factor: motivation towards a common goal.

How To Automate Mouse Clicks With Selenium Python

Sometimes, in our test code, we need to handle actions that apparently could not be done automatically. For example, some mouse actions such as context click, double click, drag and drop, mouse movements, and some special key down and key up actions. These specific actions could be crucial depending on the project context.

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run molecule automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful