You can do object-oriented programming in Python. I would like to make use of it to improve convenience and readability. This time, I recorded the process of making a spaghetti code program that I created object-oriented.
"Object-Oriented" is, quite roughly speaking, ** an object, a collection of data, Propaties, and related actions (Behavior, Method). The idea is to put it together in a pack. Applying this idea to programming requires two processes: Object-Oriented Analysis (OOA) and Object-Oriented Design (OOD).
Here, let's assume that the other day Ethical Baysean Classifier is object-oriented. The content of this program is basically a naive Bayes classifier. After reading the text, it is trained and the text entered by the user is labeled. Let's start with an object-oriented analysis of this program. What you should do at the "object-oriented analysis" stage is to ** understand the mechanics and structure of the system and interpret it as the behavior of a series of objects **. In this case, it can be understood that the object itself is only one "classifier", the learning text / judgment text is data, and the process of learning / judgment is an operation.
Once you understand how the program works with object-oriented analysis, it's time to define the ** requirements **. There is only one "classifier" for the object, the data can be narrowed down to four types of learning text, judgment text, each label, and shaped data after learning, and the operation can be narrowed down to three types: input, learning, and judgment. ..
Ethical_Baysean_Classifier-OOP.ipynb
import numpy as np
import matplotlib.pyplot as plt
from textblob import TextBlob
from textblob.classifiers import NaiveBayesClassifier
class Classifier():
def __init__(self):
self.act_pos = []
self.etc_pos = []
self.etc_neg = []
self.etc_user = []
self.etc_user_label = []
self.etc_user_label2 = []
self.trained1 = []
self.trained2 = []
print("Classifier has initiated.")
def input(self):
with open("analysed/actual_statement_pos.txt","r", encoding="utf8", errors='ignore')as f:
entire_txt = f.read()
self.act_pos = entire_txt.splitlines()
print("Text lines =", len(self.act_pos))
with open("analysed/ethical_statement_pos.txt","r", encoding="utf8", errors='ignore')as f:
entire_txt2 = f.read()
self.etc_pos = entire_txt2.splitlines()
print("Text lines =", len(self.etc_pos))
with open("analysed/ethical_statement_neg.txt","r", encoding="utf8", errors='ignore')as f:
entire_txt3 = f.read()
self.etc_neg = entire_txt3.splitlines()
print("Text lines =", len(self.etc_neg))
with open("analysed/ethical_statement_user.txt","r", encoding="utf8", errors='ignore')as f:
entire_txt4 = f.read()
self.etc_user = entire_txt4.splitlines()
print("Text lines =", len(self.etc_user))
def train(self):
for i,item in enumerate(self.act_pos):
self.trained1 = [
(self.act_pos[i], "act"),
(self.etc_pos[i], "etc"),
]
for i,item in enumerate(self.act_pos):
self.trained2 = [
(self.etc_pos[i], "etc_pos"),
(self.etc_neg[i], "etc_neg"),
]
print("\n Classifier has trained for Actual/Ethical,Correct/Wrong distinction.")
def classify(self):
trained1 = self.trained1
cl = NaiveBayesClassifier(trained1)
print("\n Actual/Ethical distinction")
for i,item in enumerate(self.act_pos):
self.etc_user_label.append(cl.classify(self.etc_user[i]))
print(self.etc_user[i],"<<- Text | Classified ->> ", self.etc_user_label[i])
trained2 = self.trained2
cl = NaiveBayesClassifier(trained2)
print("\n Correct/Wrong distinction")
for i,item in enumerate(self.act_pos):
self.etc_user_label2.append(cl.classify(self.etc_user[i]))
print(self.etc_user[i],"<<- Text | Classified ->> ", self.etc_user_label2[i])
clsf = Classifier()
clsf.input()
clsf.train()
clsf.classify()
Classifier has initiated. Text lines = 10 Text lines = 10 Text lines = 10 Text lines = 10
Classifier has trained for Actual/Ethical,Correct/Wrong distinction.
Actual/Ethical distinction We must not be late. <<- Text | Classified ->> etc We must fight against odds. <<- Text | Classified ->> etc We must take the evil with the good. <<- Text | Classified ->> etc We must consider the gravity of the situation. <<- Text | Classified ->> etc We must have high ideals. <<- Text | Classified ->> etc We must keep calm. <<- Text | Classified ->> etc We must try to find him, <<- Text | Classified ->> etc We must not forget that day. <<- Text | Classified ->> etc We must protect that. <<- Text | Classified ->> etc We must always provide against accidents. <<- Text | Classified ->> etc
Correct/Wrong distinction We must not be late. <<- Text | Classified ->> etc_neg We must fight against odds. <<- Text | Classified ->> etc_pos We must take the evil with the good. <<- Text | Classified ->> etc_pos We must consider the gravity of the situation. <<- Text | Classified ->> etc_pos We must have high ideals. <<- Text | Classified ->> etc_pos We must keep calm. <<- Text | Classified ->> etc_pos We must try to find him, <<- Text | Classified ->> etc_pos We must not forget that day. <<- Text | Classified ->> etc_neg We must protect that. <<- Text | Classified ->> etc_pos We must always provide against accidents. <<- Text | Classified ->> etc_pos
You can run any object-oriented program with just four lines of code.
Recommended Posts