本文整理匯總了Python中DBManager.DBManager.get_tweets_for_year方法的典型用法代碼示例。如果您正苦於以下問題:Python DBManager.get_tweets_for_year方法的具體用法?Python DBManager.get_tweets_for_year怎麽用?Python DBManager.get_tweets_for_year使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類DBManager.DBManager
的用法示例。
在下文中一共展示了DBManager.get_tweets_for_year方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from DBManager import DBManager [as 別名]
# 或者: from DBManager.DBManager import get_tweets_for_year [as 別名]
class Main:
"""
Main class, makes necessary function calls to necessary classes
"""
def __init__(self):
self.__db_manager = DBManager()
self.__helper = GeneralHelpers()
self.__plot_manager = PlotManager()
self.__import_manager = ImportManager()
self.__feature_manager = FeatureManager()
self.years = ("2012", "2013", "2014", "2015")
def retrieve_tweets(self, file_path_of_ids):
"""
Runs Import Manager to retrieve and import tweets
:param file_path_of_ids: String, file path of tweets to import
:return: void
"""
self.__import_manager.run(file_path_of_ids)
def extract_features_and_generate_arff(self, n=3, analyzer='char', year='2012'):
"""
Makes necessary function calls to extract features for given year and to generate arff file
:param n: int, ngram count
:param analyzer: string, word or char
:param year: string, 2012, 2013, 2014, 2015 or ALL
:return: string, path of generated arff file
"""
# Getting tweets with year
print("Getting tweets for year "+ year)
tweets_for_given_year = self.__db_manager.get_tweets_for_year(year)
print("Generating document and classes of tweets.")
document, classes = self.__feature_manager.create_document_and_classes_for_tweets(tweets_for_given_year, True)
print("Fitting the data, finding ngrams and frequencies.")
ngrams, arff_data, vectorizer, X = self.__feature_manager.fit_data(document, classes, n, analyzer)
print("Formatting the data for arff lib format.")
formatted_arff_data = self.__feature_manager.format_data_for_arff(ngrams, arff_data)
print("Generating file.")
# Experiment name, 1grams, 2grams, 3grams.. or words
experiment_name = str(n)+'Gram' if analyzer == 'char' else 'Word'
# File name, TTNet_3grams_2012
file_name = MODEL_NAME + '_' + experiment_name + '_' + year
# File name randomized TTNet_3grams_2012_asfas12.arff
file_name = self.__helper.generate_random_file_name(file_name, ARFF_FILE_EXTENSION)
# Arff file path ...../DataSet-ARFF/3Gram/TTNet/TTNet_3grams_2012_asfas12.arff
arff_file_path = PROJECT_ROOT_DIRECTORY + DATASET_ARFF_DIR_NAME + experiment_name + '/' + MODEL_NAME + '/'
# Generating the file with data
self.__helper.generate_arff_file(arff_file_path, file_name, formatted_arff_data)
print("Arff file generated at path:"+arff_file_path+file_name)
def run_experiment_with_scikit_learn(self, n=1, analyzer='word'):
"""
Makes necessary method calls to run the experiment on scikit learn.
:param n: int, count n in n-gram
:param analyzer: string, either 'word' or 'char'
:return: void
"""
# Retrieving all tweets from database
print("Retrieving all tweets from database.")
tweets_for_all_years = {}
# Iterating over all years
for year in self.years:
# Retrieving tweets for the year
tweets_for_year = self.__db_manager.get_tweets_for_year(year)
tweets_for_all_years[year] = tweets_for_year
# Creating a big list of tweets
print("Creating a big list of tweets.")
all_tweets = []
# Appending all tweets together
for year, tweets in tweets_for_all_years.iteritems():
all_tweets += tweets
# Generating document
print("Generating document and classes by preprocessing")
# Preprocessing and generation of document
document, classes = self.__feature_manager.create_document_and_classes_for_tweets(all_tweets, True)
# Getting years' tweets counts
print("Getting years' tweets counts.")
years_tweets_counts = {}
for year in self.years:
years_tweets_counts[year] = len(tweets_for_all_years[year])
all_processes = []
self.all_experiments_results = []
pool = Pool(cpu_count()-1 or 1)
#.........這裏部分代碼省略.........