-1

I have been implemented successfully recommendation engine but having a problem with that if I put any unrelated value still giving output it must be shown that " you have entered wrong value"

Here is output correct smiles output

If I put wrong smiles or anything which does not belong to the training dataset then it must be given a message that please enter correct smiles.

Wrong output

I have entered any random text so if I enter wrong smiles it must come result as

result: "Please enter correct smiles"

I am putting my code I try If else but not working.

from rdkit import Chem
from rdkit.Chem import Draw

import pandas as pd
from flask import Flask, jsonify, request, abort
import json
import sys
import random
import unicodedata
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

data = pd.read_csv("clean_o2h.csv", sep=",")


app = Flask(__name__)

@app.route('/', methods=["POST"])



def predict_word():
    print(request.get_json())
    sent = request.get_json()['smiles']
    reactants = data["reactants"].tolist()
    targets = data["targets"].tolist()
    error = ("plese enter correct smiles") 

# TFIDF vector representation
    vectorizer = TfidfVectorizer()
    X = vectorizer.fit_transform(targets)

    test = vectorizer.transform([sent])

#test = vectorizer.transform(["NC1=CC=C2C(COC(N[C@H]3C4=C(CC3)C=CC=C4)=N2)=C1"])

    cosine_similarities = cosine_similarity(test, X).flatten()
    l = []
   # n = ["Result 1","Result 2", "Result 3","Result 4"]

# Extract top 5 similarity records
    similarity = cosine_similarities.argsort()[:-5:-1]
     #print("Top 5 recommendations...")
    for sim in similarity:
    #print(reactants[sim])
       result = reactants[sim]
       l.append(result)
       print(l)

      # output = dict(zip(l,n))
       res = { i : l[i] for i in range(0, len(l) ) }



   # return  jsonify({"Recommendation": res})

    if(sent == targets):
      return jsonify({"Recommendation": res})
    else:
          return jsonify({"Error": error})




if __name__ == '__main__':
    app.run(port='8080')

Please help me with correct logic here target variable is the smiles and reactants variable is a recommendation.

1 Answers1

0

You can check whether the SMILES are valid by parsing the molecule using RDKit. This should answer part of your question, but I'm sorry I don't understand what else you are trying to achieve here.

from rdkit import Chem

error = 'something is wrong'
smiles = request.get_json()['smiles']
m = Chem.MolFromSmiles(smiles)
if m is None:
    return jsonify({"Error": error})
else:
    # you have valid smiles
    pass
JoshuaBox
  • 735
  • 1
  • 4
  • 16