youssef1214 commited on
Commit
8e8bfef
·
1 Parent(s): c236cc7

Update Database.py

Browse files
Files changed (1) hide show
  1. Database.py +55 -7
Database.py CHANGED
@@ -12,6 +12,10 @@ from datetime import datetime
12
  import time
13
  import Cleaning
14
  from collections import Counter
 
 
 
 
15
  def current_milli_time():
16
  return round(time.time() * 1000)
17
 
@@ -30,12 +34,13 @@ feedbacks = db.collection("feedbacks").where("feedbacks","!=",'').get()
30
 
31
 
32
  documents=[]
 
33
  #get all documents for today that have no summary
34
- def get_all_document():
35
- for doc in documents_to_summarize:
36
- document =Classes.Shakwa.from_dict(source=doc.to_dict())
37
- documents.append(document)
38
- return documents
39
 
40
 
41
 
@@ -46,9 +51,11 @@ def get_num_of_words():
46
  def shakwa_common_words():
47
  allcomplaints = []
48
  most_common_words = {}
49
- for doc in documents_to_summarize:
 
50
  document = Classes.Shakwa.from_dict(source=doc.to_dict())
51
  allcomplaints.append(document.complaintbody)
 
52
  # print(allcomplaints)
53
  words_in_docs = " ".join(allcomplaints)
54
  words_in_docs = Cleaning.txt_preprocess(words_in_docs)
@@ -59,7 +66,7 @@ def shakwa_common_words():
59
  else:
60
  most_common_words[word] = 1
61
  most_common_words = sorted(most_common_words.items(), key=lambda x: x[1])
62
- return dict(most_common_words)
63
 
64
 
65
  def feedback_common_words():
@@ -89,4 +96,45 @@ def get_most_common_places():
89
  else:
90
  dic_place_count[document.governorate] += 1
91
  return dic_place_count
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
 
12
  import time
13
  import Cleaning
14
  from collections import Counter
15
+ import models
16
+ from datetime import datetime
17
+ date_format = '%d-%m-%Y'
18
+
19
  def current_milli_time():
20
  return round(time.time() * 1000)
21
 
 
34
 
35
 
36
  documents=[]
37
+ documnetskey={}
38
  #get all documents for today that have no summary
39
+ # def get_all_document():
40
+ # for doc in documents_to_summarize:
41
+ # document =Classes.Shakwa.from_dict(source=doc.to_dict())
42
+ # documents.append(document)
43
+ # return documents
44
 
45
 
46
 
 
51
  def shakwa_common_words():
52
  allcomplaints = []
53
  most_common_words = {}
54
+ global documents
55
+ for document in documents:
56
  document = Classes.Shakwa.from_dict(source=doc.to_dict())
57
  allcomplaints.append(document.complaintbody)
58
+ documents.clear()
59
  # print(allcomplaints)
60
  words_in_docs = " ".join(allcomplaints)
61
  words_in_docs = Cleaning.txt_preprocess(words_in_docs)
 
66
  else:
67
  most_common_words[word] = 1
68
  most_common_words = sorted(most_common_words.items(), key=lambda x: x[1])
69
+ db.collection("per_day_common_words").add({str(datetime.today().date()):most_common_words})
70
 
71
 
72
  def feedback_common_words():
 
96
  else:
97
  dic_place_count[document.governorate] += 1
98
  return dic_place_count
99
+ queuedUnSummurizedShawkas = []
100
+ semphoreShakwas=threading.Semaphore(0)
101
+ def summrizedshakwas():
102
+ global queuedUnSummurizedShawkas
103
+ global semphoreShakwas
104
+ global db
105
+ while True:
106
+ semphoreShakwas.acquire()
107
+ shawka=queuedUnSummurizedShawkas.pop(0)
108
+ tmpdict= models.modelsummary(shawka.complaintbody)
109
+
110
+ shawka.summary=tmpdict['summary']
111
+ db.collection("complaints").document(shawka.id).update({"summary":shawka.summary})
112
+
113
+ thread = threading.Thread(target=summrizedshakwas)
114
+ thread.start()
115
+
116
+ #lithening to changes of documnts
117
+ callback_done = threading.Event()
118
+ def on_snapshot(doc_snapshot, changes, read_time):
119
+ global queuedUnSummurizedShawkas
120
+ global semphoreShakwas
121
+ global documnetskey
122
+ global documents
123
+ for doc in doc_snapshot:
124
+ # print(doc.to_dict())
125
+ shakw = Classes.Shakwa.from_dict(source=doc.to_dict())
126
+ datetime_obj = datetime.strptime(shakw.date, date_format)
127
+ if datetime_obj.date() == datetime.today().date()&& shakw.id not in documnetskey:
128
+ documents.append(shakw)
129
+ documnetskey.add(document.id)
130
+
131
+
132
+ if shakw.summary==None:
133
+ queuedUnSummurizedShawkas.append(shakw)
134
+ semphoreShakwas.release()
135
+ threadofcommonwords = threading.Thread(target=summrizedshakwas)
136
+ threadofcommonwords.start()
137
+ callback_done.set()
138
+
139
+ docwatch= db.collection("complaints").on_snapshot(on_snapshot,)
140