Reputation: 5
I have written a naive bayes classifier for text messages and it's script is as follows:
tester.py
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
df = pd.read_table('a.txt', sep='\t', header=None, names=['label', 'text'])
...
On testing, it worked correctly. Now I have created a django project where this script and the a.txt file are placed alongside views.py and urls.py. When user enters a comment, it is processed in views file as:
views.py
from .tester import predictor
def result(request):
content = request.POST['content']
res = predictor(content)
status = ''
if res == 0:
status = 'not spam'
else:
status = 'spam'
return render(request, 'spammer/result.html', {'status':status,})
Where predictor is a function I have added to tester.py:
def predictor(comment):
tester = [comment]
contest = count_vector.transform(tester) #count_vector=CountVectorizer()
a = naive_bayes.predict(contest) #naive_bayes=MultinomialNB()
return a[0]
However on running the server, there is an error:
File "pandas/_libs/parsers.pyx", line 384, in pandas._libs.parsers.TextReader.__cinit__
File "pandas/_libs/parsers.pyx", line 695, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: File b'a.txt' does not exist
which is not the case. Where am I going wrong? I have installed pandas,scipy,sklearn in virtual environment using pip and tester.py as well as a.txt are in the same directory as views.py,urls.py
Upvotes: 0
Views: 319
Reputation: 10403
Because your filesystem looks like:
yourproject
├── yourapp
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ └── a.txt
└── manage.py
And you run it like
python manage.py runserver
So your working directory is yourproject/
, while your file is relatively located at yourapp/a.txt
.
What counts it's your working directory, aka the directory from where you run the python command. Not the current Python file location.
Upvotes: 1