本文整理汇总了Python中repository.Repository.save_many方法的典型用法代码示例。如果您正苦于以下问题:Python Repository.save_many方法的具体用法?Python Repository.save_many怎么用?Python Repository.save_many使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类repository.Repository
的用法示例。
在下文中一共展示了Repository.save_many方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TwitterDataProcess
# 需要导入模块: from repository import Repository [as 别名]
# 或者: from repository.Repository import save_many [as 别名]
class TwitterDataProcess():
def __init__(self, topics, file_name):
self.file_name = file_name
self.topics = topics
self.tweets = []
self.repository = Repository()
self.process_tweets()
@staticmethod
def compact_tweet_text(tweet_text):
return tweet_text.replace('\n', ' ').replace('\r', '').lower()
@staticmethod
def to_dictionary(coordinate):
keys = ["Lat", "Lng"]
return dict(zip(keys, coordinate))
def check_key_words(self, topic_key, tweet_text):
if re.search(topic_key, tweet_text):
for word in self.topics.get(topic_key):
if re.search(word, tweet_text):
return True
return False
def process_tweets(self):
self.tweets = []
with open(self.file_name, "r") as tweets_file:
for line in tweets_file:
tweet = dict()
tweet_valid = False
try:
if line.strip() != '':
raw_tweet = json.loads(line)
text = TwitterDataProcess.compact_tweet_text(raw_tweet['text'])
for topic_key in self.topics:
if self.check_key_words(topic_key, text):
tweet_valid = True
tweet[topic_key] = True
else:
tweet[topic_key] = False
if tweet_valid:
tweet['text'] = text
tweet['lang'] = raw_tweet['lang']
tweet['city'] = raw_tweet['place']['name'] if raw_tweet['place'] is not None else None
if raw_tweet['geo'] is None:
tweet['coordinates'] = False
else:
tweet['coordinates'] = True
tweet.update(TwitterDataProcess.to_dictionary(raw_tweet['geo']['coordinates']))
self.tweets.append(tweet)
if len(self.tweets) > 1000:
self.repository.save_many(self.tweets)
self.tweets = []
except Exception as e:
print(str(e))
continue
self.repository.save_many(self.tweets)