Monday, 10 May 2021

Remove stop words and predict using Naive Bayes Classifier

 Hi all,

Use this code for NBC which removes stop words

-------------------------------------------------------------------------------

from nltk import NaiveBayesClassifier as nbc


from nltk.tokenize import word_tokenize


from itertools import chain


import csv
from gensim.parsing.preprocessing import remove_stopwords

from nltk.tokenize import word_tokenize



with open('trainingdata.csv','r'as csvinput:


    reader=csv.reader(csvinput,delimiter=",")


    rownum = 0 


    training_data = []



    for row in reader:
      old=row[0]
      sent=remove_stopwords(row[0])
      row[0]=sent
     
      training_data.append (row)
      rownum += 1
      print('hup original ',old)
      print('hup new ',sent)
      print('----------------')



vocabulary = set(chain(*[word_tokenize(i[0].lower()) for i in training_data]))



feature_set = [({i:(i in word_tokenize(sentence.lower())) for i in vocabulary},tag) for sentence, tag in training_data]



classifier = nbc.train(feature_set)



with open('testdata.csv','r'as csvinput:


    with open('data.csv''w'as csvoutput:


        writer = csv.writer(csvoutput, lineterminator='\n')


        reader1 = csv.reader(csvinput)



        all = []


        row = next(reader1)


        



        for row in reader1:


            test_sentence = row[1]


            featurized_test_sentence =  {i:(i in word_tokenize(test_sentence.lower())) for i in vocabulary}


            print ("test_sent:",test_sentence)


            print ("tag:",classifier.classify(featurized_test_sentence))


            row.append(classifier.classify(featurized_test_sentence))


            all.append(row)


        writer.writerows(all)

Wednesday, 28 October 2020

Sort in reverse order in Python

 

Hi,

Here one list is loaded with cubes. One element is edited intentionally. 


a=[]

for i in range(10):

a.append(i**3)

a[4]=10

print(a)

a.sort(reverse=True)

print(a)

Array using Numpy and List in PythonTuple

 Hi all

Array can be implemented in Python using two methods

####Numpy

import numpy as np

hup=np.arange(10)

hup=np.zeros(10)

for i in range(10):

hup[i]=i**2


print(hup)

############

#Using List

a=[]

for i in range(10):

a.append(i**3)

print(a)


a = np.array([[10],
              [01]])
b = np.array([[41],
              [22]])
c=np.matmul(a, b)
print(c)

Monday, 7 September 2020

DB Connection from Python and Serial Port connection in Python

 Hi 

Use this code for Python MySql connection. You need mysql.connector for this purpose. you can install this using pip command in Python.

Also install serial using pip for serial connection.

---------------------------------------------------------------------------------------------------------------------


import mysql.connector

import time;

import serial;

from mysql.connector import Error

from mysql.connector import errorcode

ser = serial.Serial('COM3', 9600, timeout=0,parity=serial.PARITY_EVEN, rtscts=1)


try:

   connection = mysql.connector.connect(host='localhost',

                             database='iot',

                             user='root',

                             password='')

   cur = connection.cursor()

   while True:

      s=ser.read();

      print(s);

      time.sleep(1);

      sql=("INSERT INTO timerecord (userid, recordtime, userrole ) VALUES (%s,%s,%s)")

      val=("1",s,"1")

      if s=="9":

         cur.execute(sql,val)

   connection.close()

except mysql.connector.Error as error :

    connection.rollback() #rollback if any exception occured

    print("Failed inserting record into python_users table {}".format(error))


Thursday, 7 May 2020

Text Analysis

Hi All,

Text Analysis in Python can be done with the help of so many functions. The various Types used are
List ,  Set etc.
Split is a function used with List. Len() function is also used.

Find some programs
1.
t='fine'
sentence='I am fine'
huparray=sentence.split()
flag=0
for word in huparray:
    if word==t:
        flag=1
if flag==1:
    print('Positive')
     
   
2.
p='fine good great'

sentence='I am fine and great but'
shuparray=sentence.split()

phuparray=p.split();
flag=0

pcount=0

for word in shuparray:
    for word1 in phuparray:
        if word==word1:
            pcount=pcount+1

        
if pcount>0:
    print('Positive')

print(pcount)
       
    
3. p='fine good great'
n='bad poor slow cry'
sentence='I am fine and great but slow cry'
shuparray=sentence.split()
nhuparray=n.split();
phuparray=p.split();
flag=0
ncount=0
pcount=0

for word in shuparray:
    for word1 in phuparray:
        if word==word1:
            pcount=pcount+1
            
for word in shuparray:
    for word1 in nhuparray:
        if word==word1:
            ncount=ncount+1
        
if pcount>ncount:
    print('Positive')
elif ncount>pcount:
    print('Negative')
else:
    print('Neutral')

print(ncount)
print(pcount)
       
    
4. p='fine good great'

sentence='I am fine and great but'
shuparray=sentence.split()

phuparray=p.split();
flag=0

pcount=0

plist=set(phuparray) & set(shuparray)

        
print(len(list(plist)))
       
    

Sunday, 12 April 2020

Encoding

Hi all,

Pls find the encoding program.

------------------------------------------------
//text=fileread(fullpathname);
text='HUP'
ASCII = double(text)
A=dec2bin(ASCII,8)

B= logical(A - 48)

 m=size(B,1)
 n=size(B,2)

p='A'
q='C'
r='G'
S= 'T'

k=1
 x=.4;
h=waitbar(x,'Process start');
for i=1:m
for j=1:2:n
if (B(i,j)==0 && B(i,j+1)==1)
sc(k)=p
elseif (B(i,j)==1 && B(i,j+1)==0)
sc(k)=q
elseif (B(i,j)==0 && B(i,j+1)==0)
sc(k)=r
else
sc(k)=S
end
k=k+1
end
end
p53nt = fastaread('p53nt.txt')
j=1
for i=1:4:length(sc)
k=strfind(p53nt.Sequence,sc(i:i+3))

code(j)=k(randi([1 length(k)]));
j=j+1;
end


fileID = fopen('Ciphertext.txt','w');
for (i=1:length(code))
fprintf(fileID,'%d ',code(i))
end

Create Environment and configure Tensorflow

Hi All,
We can configure Environment in Python and Configure Tensorflow using the below given statements.

1. install python
2. install virtual environment
pip install virtualenv
3. Create a floder c:\HUP
4. create virtual environment
python -m virtualenv c:\HUP
5. Activate virtual environment
c:\HUP\Scripts\activate

pip install https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.8.0-py3-none-any.whl