-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmemory_stream.py
69 lines (57 loc) · 2.1 KB
/
memory_stream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pandas as pd
import json
class MemoryStream:
def __init__(self):
self.columns = ["ID", "TargetDocID", "Question", "Reply", "Point"]
self.data = pd.DataFrame(columns=self.columns)
self.current_id = 1
def add_data(self, target_doc_id, question, reply, point):
ret_id = self.current_id
data = [[self.current_id, target_doc_id, question, reply, point]]
new_data = pd.DataFrame(data, columns=self.columns)
self.data = pd.concat([self.data, new_data], ignore_index=True)
self.current_id += 1
return ret_id
def get_data(self):
return self.data
def get_reply(self, index = None):
if (index == None):
index = len(self.data) - 1
if index >= 0 and index < len(self.data):
return self.data.loc[index, "Reply"]
else:
return None
def get_data(self, id: int):
filtered_data = self.data.loc[self.data["ID"] == id]
if filtered_data.empty:
return None
else:
return filtered_data.to_dict(orient="records")[0]
def get_point(self, index = None):
if (index == None):
index = len(self.data) - 1
if index >= 0 and index < len(self.data):
return self.data.loc[index, "Point"]
else:
return None
def save_to_json(self, file_path):
json_data = self.data.to_dict(orient="records")
with open(file_path, "w", encoding="utf-8") as f:
json.dump(json_data, f, indent=4, ensure_ascii=False)
def load_from_json(self, file_path):
with open(file_path, 'r') as file:
json_data = json.load(file)
self.data = pd.DataFrame(json_data)
if __name__ == "__main__":
memory_stream = MemoryStream()
while True:
target_doc_id = input("TargetDocID> ")
question = input("question> ")
reply = input("reply> ")
point = input("point> ")
memory_stream.add_data(target_doc_id, question, reply, int(point))
print(memory_stream.get_data())
else:
pass