Spaces:
Running
Running
Update turbowarp-server/gpt.py
Browse files- turbowarp-server/gpt.py +12 -7
turbowarp-server/gpt.py
CHANGED
|
@@ -26,14 +26,20 @@ with open("turbowarp-server/n-chars.txt", encoding="utf8") as f:
|
|
| 26 |
for line in f:
|
| 27 |
chars.append(line.strip())
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def encode(text):
|
| 30 |
out = ""
|
| 31 |
for c in text:
|
| 32 |
-
if c
|
|
|
|
|
|
|
| 33 |
i = chars.index(c)
|
| 34 |
out += f"{i:02d}"
|
| 35 |
elif c == "\n":
|
| 36 |
-
out += "98"
|
| 37 |
return out
|
| 38 |
|
| 39 |
def decode(data):
|
|
@@ -43,19 +49,18 @@ def decode(data):
|
|
| 43 |
for i in range(0, len(data), 2):
|
| 44 |
num = int(data[i:i+2])
|
| 45 |
|
| 46 |
-
if num == 99:
|
| 47 |
tokens.append(current)
|
| 48 |
current = ""
|
| 49 |
-
|
| 50 |
-
elif num == 98: # 改行
|
| 51 |
current += "\n"
|
| 52 |
-
|
|
|
|
| 53 |
else:
|
| 54 |
if num < len(chars):
|
| 55 |
current += chars[num]
|
| 56 |
|
| 57 |
tokens.append(current)
|
| 58 |
-
|
| 59 |
return tokens
|
| 60 |
|
| 61 |
# ---------------------
|
|
|
|
| 26 |
for line in f:
|
| 27 |
chars.append(line.strip())
|
| 28 |
|
| 29 |
+
# ---------------------
|
| 30 |
+
# 文字エンコード / デコード
|
| 31 |
+
# ---------------------
|
| 32 |
+
|
| 33 |
def encode(text):
|
| 34 |
out = ""
|
| 35 |
for c in text:
|
| 36 |
+
if c == " ":
|
| 37 |
+
out += "96" # 半角スペースは固定で 96
|
| 38 |
+
elif c in chars:
|
| 39 |
i = chars.index(c)
|
| 40 |
out += f"{i:02d}"
|
| 41 |
elif c == "\n":
|
| 42 |
+
out += "98" # 改行
|
| 43 |
return out
|
| 44 |
|
| 45 |
def decode(data):
|
|
|
|
| 49 |
for i in range(0, len(data), 2):
|
| 50 |
num = int(data[i:i+2])
|
| 51 |
|
| 52 |
+
if num == 99: # 履歴区切り
|
| 53 |
tokens.append(current)
|
| 54 |
current = ""
|
| 55 |
+
elif num == 98: # 改行
|
|
|
|
| 56 |
current += "\n"
|
| 57 |
+
elif num == 96: # 半角スペース
|
| 58 |
+
current += " "
|
| 59 |
else:
|
| 60 |
if num < len(chars):
|
| 61 |
current += chars[num]
|
| 62 |
|
| 63 |
tokens.append(current)
|
|
|
|
| 64 |
return tokens
|
| 65 |
|
| 66 |
# ---------------------
|