class DostN: def __init__(self): self.db = DB() self.tokenizer = RegexTokenizer() self.model = FastTextSocialNetworkModel(tokenizer=self.tokenizer) self.tokens = self.tokenizer.split('всё очень плохо') def ready_msg(self, messages, nnID): results = self.model.predict(messages, k=2) sentiments = [] for message, sentiment in zip(messages, results): sentiments.append(sentiment) try: pos = sentiments[0]['positive'] self.db.setPos(nnID, pos) except Exception as e: print(e, 22) pos = 0 self.db.setPos(nnID, pos) try: neg = sentiments[0]['negative'] self.db.setNegative(nnID, neg) except Exception as e: print(e, 29) neg = 0 self.db.setNegative(nnID, neg) try: ne = sentiments[0]['neutral'] self.db.setNeutral(nnID, ne) except Exception as e: print(e, 36) ne = 0 self.db.setNeutral(nnID, ne) return (sentiments)
def parse_senstiment(self, text): tokenizer = RegexTokenizer() tokens = tokenizer.split('всё очень плохо') # хз model = FastTextSocialNetworkModel(tokenizer=tokenizer) return model.predict([text], k=2)[0]
def graph(): file = filedialog.askopenfilename(filetypes=(("Text files", "*.txt"), ("all files", "*.*"))) f = open(file) raw = f.read() sentences = nltk.sent_tokenize(raw) command = 'download' arguments = ['fasttext-social-network-model'] if command == 'download': downloader = DataDownloader() for filename in arguments: if filename not in AVAILABLE_FILES: raise ValueError(f'Unknown package: {filename}') source, destination = AVAILABLE_FILES[filename] destination_path: str = os.path.join(DATA_BASE_PATH, destination) if os.path.exists(destination_path): continue downloader.download(source=source, destination=destination) else: raise ValueError('Unknown command') import dostoevsky from dostoevsky.tokenization import RegexTokenizer from dostoevsky.models import FastTextSocialNetworkModel tokenizer = RegexTokenizer() tokens = tokenizer.split( 'всё очень плохо') # [('всё', None), ('очень', None), ('плохо', None)] model = FastTextSocialNetworkModel(tokenizer=tokenizer) messages = sentences results = model.predict(messages, k=2) for message, sentiment in zip(messages, results): positive_values_all = [ sentiment.get('positive') for message, sentiment in zip(messages, results) ] positive_values = [ 0.0 if value == None else value for value in positive_values_all ] negative_values_all = [ sentiment.get('negative') for message, sentiment in zip(messages, results) ] negative_values = [ 0.0 if value == None else value for value in negative_values_all ] summary = (len(negative_values)) n_value = np.array(negative_values) p_value = np.array(positive_values) counts_value = np.arange(summary) plt.plot(counts_value, p_value, n_value) plt.show()
def begin(): file = filedialog.askopenfilename(filetypes=(("Text files", "*.txt"), ("all files", "*.*"))) f = open(file) raw = f.read() sentences = nltk.sent_tokenize(raw) command = 'download' arguments = ['fasttext-social-network-model'] if command == 'download': downloader = DataDownloader() for filename in arguments: if filename not in AVAILABLE_FILES: raise ValueError(f'Unknown package: {filename}') source, destination = AVAILABLE_FILES[filename] destination_path: str = os.path.join(DATA_BASE_PATH, destination) if os.path.exists(destination_path): continue downloader.download(source=source, destination=destination) else: raise ValueError('Unknown command') tokenizer = RegexTokenizer() tokens = tokenizer.split( 'всё очень плохо') # [('всё', None), ('очень', None), ('плохо', None)] model = FastTextSocialNetworkModel(tokenizer=tokenizer) messages = sentences results = model.predict(messages, k=2) for message, sentiment in zip(messages, results): analysis_line = '\n', message, '\n', '->', '\n', sentiment, '\n' text.insert(END, analysis_line)
from dostoevsky.tokenization import RegexTokenizer from dostoevsky.models import FastTextSocialNetworkModel tokenizer = RegexTokenizer() tokens = tokenizer.split( 'всё очень плохо') # [('всё', None), ('очень', None), ('плохо', None)] model = FastTextSocialNetworkModel(tokenizer=tokenizer) message = ['Волосы у девушки просто огонь!!! Красота!!!'] results = model.predict(message, k=len(message)) for message, sentiment in zip(message, results): # привет -> {'speech': 1.0000100135803223, 'skip': 0.0020607432816177607} # люблю тебя!! -> {'positive': 0.9886782765388489, 'skip': 0.005394937004894018} # малолетние дебилы -> {'negative': 0.9525841474533081, 'neutral': 0.13661839067935944}] print(message, '->', sentiment)