-
Notifications
You must be signed in to change notification settings - Fork 3
/
classify_documents.py
47 lines (36 loc) · 1.3 KB
/
classify_documents.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
import requests
import os
from sklearn.extract_features_from_text.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
def get_document_classification(document):
"""
Classifies a document into a particular category.
Args:
document: The document to be classified.
Returns:
The category of the document.
"""
document_category = "some_category"
# Convert the document to a vector.
vectorizer = TfidfVectorizer()
vectorizer.fit([document])
document_vector = vectorizer.transform([document])
# Train a logistic regression model.
model = LogisticRegression()
model.fit(vectorizer.fit_transform([document]), [document_category])
# Classify the document.
prediction = model.predict(document_vector)
return prediction
def main():
# Iterate through the txt files in C:\python\autoindex\txt_output.
for filename in os.listdir("C:\\python\\autoindex\\txt_output"):
# Read the file.
with open(f"C:\\python\\autoindex\\txt_output\\{filename}", "r") as f:
document = f.read()
# Classify the document.
prediction = get_document_classification(document)
# Write the classification to a file.
with open(f"C:\\python\\autoindex\\document_classification\\{filename}", "w") as f:
f.write(prediction)
if __name__ == "__main__":
main()