66import ujson as json
77
88
9- class EmptyFilenameException (Exception ):
9+ class JSONLogParserEmptyData (Exception ):
1010 pass
1111
1212
1313class JSONLogParser :
14- def __init__ (self , file_name : AnyStr ):
15- if not file_name :
16- raise EmptyFilenameException ("You should to put correct filename!" )
14+ def __init__ (self , file_name : AnyStr = None , data_dict : dict = None ):
15+ if not any ([file_name , data_dict ]):
16+ raise JSONLogParserEmptyData (
17+ "You should put data into class: file_name or data_dict"
18+ )
1719 self .filename : AnyStr = file_name
18- self .data : Dict = JSONLogParser .parse (file_name )
20+ if self .filename :
21+ # parse file from disk
22+ self .data : Dict = JSONLogParser .parse (file_name )
23+ else :
24+ # get data from dict
25+ self .data = data_dict
1926 self .name : Union [AnyStr , None ] = self .data .get ("name" )
2027 self .type : Union [AnyStr , None ] = self .data .get ("type" )
2128 self .id : Union [int , None ] = self .data .get ("id" )
@@ -144,6 +151,7 @@ def get_user_stats(self):
144151 self .get_user_messages ()
145152
146153 return {
154+ "sociable_users" : self .sociable_users ,
147155 "users" : self .users ,
148156 "messages" : self .messages_by_users ,
149157 }
@@ -193,14 +201,20 @@ def clear_text(sentence: str) -> str:
193201 messages .extend (text )
194202 return messages
195203
196- def get_words_stats (self , words : List [ str ] ):
204+ def get_words_stats (self ):
197205 counter = Counter ()
206+ words = self .get_all_words ()
198207 morph = pymorphy2 .MorphAnalyzer ()
199208 for word in words :
200209 # TODO: threads here?>
201- counter [morph .parse (word )[0 ].normal_form ] += 1
210+ parsed_word = morph .parse (word )[0 ]
211+ if "NOUN" in parsed_word .tag :
212+ counter [parsed_word .normal_form ] += 1
213+ elif "VERB" in parsed_word .tag :
214+ counter [parsed_word .normal_form ] += 1
202215
203216 self .words_stats = counter
217+ return {"word_stats" : self .words_stats }
204218
205219 # most replied user
206220 def get_most_replied_user (self ):
@@ -222,15 +236,23 @@ def get_most_replied_user(self):
222236 message ["id" ]
223237 ]
224238
225- authors_items = []
239+ authors_items_users = {}
226240 for from_id , values in authors_replied_messages .items ():
227- authors_items .append (
228- {"from_id" : from_id , "count" : len (values ), "from" : users [from_id ]}
229- )
230- authors_items = sorted (authors_items , key = lambda x : - x ["count" ])
231- self .users_replies_stats : List [Dict ] = authors_items
241+ authors_items_users [users [from_id ]] = len (values )
242+ self .users_replies_stats : List [Dict ] = authors_items_users
232243 self .users_replies_data : List [Dict ] = authors_replied_messages
233244
245+ return {"most_replies" : self .users_replies_stats }
246+
247+ # main function
248+ def generate_stats (self ):
249+ return {
250+ ** self .tags_stats (),
251+ ** self .get_user_stats (),
252+ ** self .get_words_stats (),
253+ ** self .get_most_replied_user (),
254+ }
255+
234256
235257if __name__ == "__main__" :
236258 filename = "result.json"
0 commit comments