MAKS_Enjoyer

Shitty simple sentiment analysis AI program

Apr 14th, 2023
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | Source Code | 0 0
  1. from sklearn.feature_extraction.text import CountVectorizer
  2. from sklearn.naive_bayes import MultinomialNB
  3. import glob
  4.  
  5. # Load the data
  6. filepaths = glob.glob('C:/Users/Public/Operational_Functional_Block/Python_Projects/Rocket Engine Generator/GenFiles/GenData/FullFolders/*.txt')
  7. texts = []
  8. labels = []
  9. for path in filepaths:
  10.     with open(path, 'r') as file:
  11.         text = file.read()
  12.         texts.append(text)
  13.         # Get the label from the filename or directory
  14.         label = path.split('/')[-1].split('.')[0]
  15.         labels.append(label)
  16.  
  17. # Transform the text into a numerical representation
  18. vectorizer = CountVectorizer()
  19. X = vectorizer.fit_transform(texts)
  20.  
  21. # Train the model
  22. clf = MultinomialNB()
  23. clf.fit(X, labels)
  24.  
  25. # Test the model
  26. test_text = ''
  27. X_test = vectorizer.transform([test_text])
  28. predicted_label = clf.predict(X_test)[0]
  29.  
  30. print('Predicted label:', predicted_label)
  31.  
Advertisement
Add Comment
Please, Sign In to add comment