1

I have a python file that consists of one functions, machineLearning(issue).

In my PHP code, to run the python script, I have the following:

'category' => exec("python C:/xampp/htdocs/ci/assets/machineLearning.py $temp 2>&1"),

which runs the machineLearning function as expected. However, I need to include another function called updateTrain, which takes the parameters issue, category. Here is the current python file:

from textblob import TextBlob;
from textblob.classifiers import NaiveBayesClassifier;
import sys;

train = [
    ('I cannot enter or exit the building','idCard'),
    ('Enter or exit', 'idCard'),
    ('Card needs replacing','idCard'),
    ('ID card not working','idCard'),
    ('Printing', 'printer'),
    ("My ID Card doesn't sign me into the printers", "printer"),
    ('Mono', "printer"),
    ('Colour Printing',"printer"),
    ('I cannot connect my phone to the wifi', 'byod'),
    ('I do not get emails on my phone, tablet, or laptop', 'byod'),
    ('Cannot send emails','email'),
    ('Do not recieve emails','email'),
    ('Cannot sign in to moodle', 'email'),
    ('Cannot sign in to computer','desktop_pc'),
    ("Software isn't working",'desktop_pc'),
    ('Scratch disks are full', 'desktop_pc'),
    ('I have forgotten my password for the computers', 'desktop_pc'),
    ('forgotten password','forgotten_password'),
    ('My password does not work','forgotten_password'),
    ('Applications fail to launch and I get an error', 'desktop_pc'),
    ('My ID card does not allow me to print. I go to scan my card on the printer, and it comes up with a message saying that I need to associate my card every day I want to print. Other peoples cards work fine?','idCard'),
    ("I am having trouble connecting my android phone to my emails. I open the gmail app and select exchange account. I then enter my email and password and click okay on the message that asks if its alright to use the autoconfigure.xml. It then gives me all the server details, I click okay and then it tells me that I cannot connect to the server.", 'byod'),
    ('I cannot find my work', 'lost_work'),
    ('My work has been corrupted', 'lost_work'),
    ('My work has been lost', 'lost_work'),
    ('Coursework gone missing', 'lost_work'),
    ('my id card isnt working','idCard'),
    ('I cannot print in colour', 'printer')
]

cl = NaiveBayesClassifier(train);

def updateTrain(issue, category):
    new_data = [(issue,category)];
    cl.update(new_data);


def machineLearning(issue):
    test = [
        ("My id card does not allow me to go through the gates, the light flashes orange and then goes red. The gate doesn't open", "idCard"),
        ('My id card has snapped', 'idCard'),
        ('When printing, swiping my ID card does not automatically sign me in, I have to manually register my card', 'printer'),
        ('I am getting errors when trying to sign into my email account on the website', 'email'),
        ('Microsoft applications were not working','desktop_pc'),
        ('Software is missing','desktop_pc'),
        ('software keeps crashing','desktop_pc'),
        ('my phone refuses to connect to the internet','byod'),
        ('I cannot sign in to the computers, moodle or emails because I have forgotten my password','desktop_pc'),
        ('I cannot get my emails on my device I bought from home','byod'),
        ('My ID card does not allow me to print. I go to scan my card on the printer, and it comes up with a message saying that I need to associate my card every day I want to print. Other peoples cards work fine?','idCard'),
        ('My password does not allow me to sign in to the computers, laptops, moodle or emails','forgotten_password'),
        ('I have forgotten my password','forgotten_poassword'),
        ('My work has been corrupted', 'lost_work'),
        ('My work has been lost', 'lost_work'),
        ('Coursework gone missing', 'lost_work'),
        ('I cannot print in colour', 'printer'),
    ];
    print(cl.classify(issue));

if __name__ == "__main__": 
    machineLearning(sys.argv[1]);

As updateTrain(issue, category) takes 2 arguments, how can I change the above exec command so that it ignores the update train?

Similarly, how can I call updateTrain, ignoring the machineLearning function?

1 Answer 1

1

Considering the way you are calling your Python script from you PHP code, I think the best solution (or at least the simplest from the current situation) would be to pass arguments to this script.

In the body of your script (if __name__ == "__main__"), you will want to test what parameters are passed to the script. For instance, if you want to call the updateTrain method, ignoring the machineLearning method:

if sys.argv[1] == "updateTrain":
    updateTrain()
elif sys.argv[1] == "machineLearning":
    machineLearning()

Then, when you call this script from PHP, you will need to pass either updateTrain or machineLearning as a parameter, which is easy since you are running a Bash command:

'category' => exec("python C:/xampp/htdocs/ci/assets/machineLearning.py updateTrain 2>&1")

In a more complex situation, when you need to pass arguments to the functions you call, you will need to pass them to the command as well. For instance, since updateTrain takes two arguments, say two integers:

if sys.argv[1] == "updateTrain":
    issue = int(sys.argv[2])
    category = int(sys.argv[3])
    updateTrain(issue, category)

And your PHP call will become:

'category' => exec("python C:/xampp/htdocs/ci/assets/machineLearning.py updateTrain 12 42 2>&1")

In the end, your script will look something like:

if __name__ == "__main__":
    if sys.argv[1] == "updateTrain":
        if len(sys.argv) >= 4:
            issue = sys.argv[2]      # convert it to the right type
            category = sys.argv[3]   # convert it too
            updateTrain(issue, category)

    elif sys.argv[1] == "machineLearning":
        if len(sys.argv) >= 3:
            issue = sys.argv[2]   # convert it here as well
            machineLearning(issue)

    else:
        print("Unknown command")

The script will then be typically called by the following commands, that will need to be passed to the exec in PHP:

python machineLearning.py updateTrain 12 42
python machineLearning.py machineLearning 25

As a side note, remember that this is a quick solution, while giving an actual and complete Bash command-like behaviour to the script would be way more complicated. Besides, there exist several libraries, like argparse, that make it easier writing complete commands.

Sign up to request clarification or add additional context in comments.

5 Comments

Can you please explain why finding the length of the sys.argv is needed?
@RobbieWhite It is a very, very, very basic input verification. You want to access sys.argv[2], so you want to know if you can. Although "trying and then excusing" is considered more pythonic than "asking for permission first", I think it is better, in this particular case, to test the length of sys.argv before accessing its elements, rather than trying to access its elements and then handle a IndexError exception.
@RobbieWhite But anyway, it's not needed, especially if the only caller is you. I just like to give myself some guards.
Ahh I see, thanks! I'm fairly new to PHP and, although I can do fairly complex things in python, I'm not familiar with sys.argv.
@RobbieWhite Well, it's just a list, so it should be handled as such. Especially regarding the indices, but also the types of the elements, because every element is a string (since it comes from a command line). Thing is, you might expect a command-like script to behave differently from say a "classical" script, concerning for example exceptions handling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.