-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtext_extractor.py
160 lines (136 loc) · 4.83 KB
/
text_extractor.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import datetime
import json
import os
import re
from typing import Dict
import emoji
import requests
from termcolor import colored
from utils import create_dir, load_config, remove_existing_file
from dotenv import load_dotenv
def preprocess_text(text: str) -> str:
"""
Preprocesa el texto eliminando ciertos patrones y caracteres.
Args:
text (str): Texto a preprocesar.
Returns:
El texto preprocesado.
"""
text = re.sub(r"<[^>]*>", "", text)
text = re.sub(r"http\S+|www.\S+", "", text)
text = re.sub(r"Copyright.*", "", text)
text = text.replace("\n", " ")
text = emoji.demojize(text)
text = re.sub(r":[a-z_&+-]+:", "", text)
return text
def download_file(url: str, repo_info: dict, jsonl_file_name: str) -> None:
"""
Descarga un archivo desde una URL y lo guarda en un archivo JSONL.
Args:
url (str): URL desde donde se descarga el archivo.
repo_info (dict): Información sobre el repositorio desde donde se descarga el archivo.
jsonl_file_name (str): Nombre del archivo JSONL donde se guarda el archivo descargado.
"""
response = requests.get(url)
filename = url.split("/")[-1]
text = response.text
if text is not None and isinstance(text, str):
text = preprocess_text(text)
text = re.sub(r"\s+", " ", text)
text = text.strip()
file_dict = {
"title": filename,
"repo_owner": repo_info["owner"],
"repo_name": repo_info["repo"],
"text": text,
}
with open(jsonl_file_name, "a") as jsonl_file:
jsonl_file.write(json.dumps(file_dict) + "\n")
else:
print(f"Texto no esperado: {text}")
def process_directory(
path: str,
repo_info: Dict,
headers: Dict,
jsonl_file_name: str,
) -> None:
"""
Procesa un directorio de un repositorio de GitHub y descarga los archivos en él.
Args:
path (str): Ruta del directorio a procesar.
repo_info (Dict): Información sobre el repositorio que contiene el directorio.
headers (Dict): Headers para la petición a la API de GitHub.
jsonl_file_name (str): Nombre del archivo JSONL donde se guardarán los archivos descargados.
"""
# Si el nombre del directorio es 'zh', lo omite y retorna inmediatamente.
# Esta característica está implementada para no descargar las traducciones en chino.
if os.path.basename(path) == "zh":
print(
colored(
f"Se omite el directorio 'zh' (traducciones en chino): {path}", "yellow"
)
)
return
base_url = f"https://api.github.com/repos/{repo_info['owner']}/{repo_info['repo']}/contents/"
print(
colored(f"Procesando directorio: {path} del repo: {repo_info['repo']}", "blue")
)
response = requests.get(base_url + path, headers=headers)
if response.status_code == 200:
files = response.json()
for file in files:
if file["type"] == "file" and (
file["name"].endswith(".mdx") or file["name"].endswith(".md")
):
print(colored(f"Descargando documento: {file['name']}", "green"))
print(colored(f"Descarga URL: {file['download_url']}", "cyan"))
download_file(
file["download_url"],
repo_info,
jsonl_file_name,
)
elif file["type"] == "dir":
process_directory(
file["path"],
repo_info,
headers,
jsonl_file_name,
)
print(colored("Exito en extracción de documentos del directorio.", "green"))
else:
print(
colored(
"No se pudieron recuperar los archivos. Verifique su token de GitHub y los detalles del repositorio.",
"red",
)
)
def main():
"""
Función principal que se ejecuta cuando se inicia el script.
"""
config = load_config()
load_dotenv("../.env")
github_token = os.getenv("GITHUB_TOKEN")
print(github_token)
os.environ['OPENAI_API_KEY'] = github_token
if github_token is None:
raise ValueError(
"GITHUB_TOKEN no está configurado en las variables de entorno."
)
headers = {
"Authorization": f"Bearer {github_token}",
"Accept": "application/vnd.github.v3.raw",
}
current_date = datetime.date.today().strftime("%Y_%m_%d")
jsonl_file_name = f"data/docs_en_{current_date}.jsonl"
create_dir("data/")
remove_existing_file(jsonl_file_name)
for repo_info in config["github"]["repos"]:
process_directory(
repo_info["path"],
repo_info,
headers,
jsonl_file_name,
)
if __name__ == "__main__":
main()