-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguage_Detection_With_MultinomialNB.py
More file actions
58 lines (41 loc) · 1.62 KB
/
Language_Detection_With_MultinomialNB.py
File metadata and controls
58 lines (41 loc) · 1.62 KB
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
48
49
50
51
52
53
54
55
56
57
58
"""Language-Detection-With-MultinomialNB.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1JjhmKNEogG5CrVgLv_rVRwT3yrRxQNGH
**Language Detection With MultinomialNB**
In this project, i will try to detect the language of users text. I will use MultinomialNB model from sklearn. And i will use CountVectorizer method also from sklearn. Lets start with import libraries.
"""
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, accuracy_score
from sklearn.naive_bayes import MultinomialNB
# Created data frames. I just created a special Turkish data frame to see Turkish data.
df = pd.read_csv("text_language.csv")
#tr = df[df["language"] == "Turkish"]
#df.head()
#tr.head()
#df["language"].value_counts()
# Checked if there any missing observation
#df.isnull().sum()
"""**Modelling**"""
# Dependent and independent variables
x = np.array(df["Text"])
y = np.array(df["language"])
# Data divided into training and test
cv = CountVectorizer()
X = cv.fit_transform(x)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
# Model setup
model = MultinomialNB().fit(X_train, y_train)
# Models score
#model.score(X_test, y_test)
# Models accuracy
#acc = accuracy_score(model.predict(X_test), y_test)
#print(f"Accuracy: %{acc*100}")
# Example usage
#user_input = input("Type something: ")
#data = cv.transform([user_input]).toarray()
#output = model.predict(data)
#print(f"Detected language: {output}")