how can i tokenize all text documents in a folder and list the results ordered by size,…
- User
how can i tokenize all text documents in a folder and list the results ordered by size, least to most
Assistant
Here is some Python code to tokenize all text documents in a folder and list the results ordered by the number of tokens, from least to most:
pythonimport os from nltk.tokenize import word_tokenize docs = [] for filename in os.listdir('documents'): if filename.endswith('.txt'): with open(os.path.join('documents', filename)) as f: text = f.read() tokens = word_tokenize(text) docs.append((len(tokens), filename)) docs.sort(key=lambda x: x[0]) print("Documents ordered by number of tokens:") for num_tokens, filename in docs: print(f"{filename}: {num_tokens} tokens")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.