RedHitMark commited on
Commit
de0995c
·
1 Parent(s): 219a208

corpus scripts

Browse files
Files changed (6) hide show
  1. .gitignore +1 -0
  2. corpus.csv +3 -0
  3. corpus.tsv +3 -0
  4. corpus.xlsx +3 -0
  5. document_to_corpus.py +41 -0
  6. requirements.txt +2 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .venv
corpus.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8555bf11c153be9a1f3a24219557a25a8ce385aa43b53ce3ef9829ca81edf730
3
+ size 92900
corpus.tsv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7bd289635c8b6d6a899cd8c76592338786957b4f272ed6918ae13d03a6485114
3
+ size 92886
corpus.xlsx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cc98b8e4db4c0f8e56e47a28824211aa6ad35afe876b98a1fd449c587d538ab3
3
+ size 36438
document_to_corpus.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import pandas as pd
4
+
5
+ BASE_DIR = os.path.join(".")
6
+
7
+ if __name__ == "__main__":
8
+ chapters = []
9
+
10
+ for root, dirs, files in os.walk(os.path.join(BASE_DIR, "documents")):
11
+ for file in files:
12
+ if ".md" in file:
13
+ with open(os.path.join(root, file), "r", encoding="utf-8") as f:
14
+ full_path = os.path.join(root, file)
15
+ text = f.read()
16
+ lines = text.split("\n")
17
+ chapters_indices = [i for i in range(len(lines)) if lines[i].startswith("# ")]
18
+ for i in range(len(chapters_indices)):
19
+ next_index = chapters_indices[i + 1] if i < len(chapters_indices) - 1 else len(lines)
20
+
21
+ title = lines[chapters_indices[i]][2:].strip()
22
+
23
+ content = '\n'.join(lines[chapters_indices[i] + 1:next_index])
24
+ content = '\n'.join(line.rstrip() for line in content.split("\n"))
25
+ content = content.rstrip()
26
+
27
+ print(full_path)
28
+ chapters.append({
29
+ 'region': full_path.split('\\')[1],
30
+ 'topic': full_path.split('\\')[2],
31
+ 'progress': i+1,
32
+ "title": title,
33
+ "content": content
34
+ })
35
+
36
+ print(chapters[0])
37
+
38
+ df = pd.DataFrame(chapters).sort_values(by=['region', 'topic', 'progress'])
39
+ df.to_csv(os.path.join(BASE_DIR, 'corpus.csv'), index=False)
40
+ df.to_csv(os.path.join(BASE_DIR, 'corpus.tsv'), sep='\t', index=False)
41
+ df.to_excel(os.path.join(BASE_DIR, 'corpus.xlsx'), index=False)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ pandas==2.2.3
2
+ openpyxl==3.1.5