Sunday, 29 September 2019

Classification in Python without Machine Learning

HI All,
See the code...

The input is trainingdata.csv  with the following content

high-temparature headache cough,fever
chest-pain high-pressure breathing-issue,heartattack
very-high-esr faint high-beta-count,cancer

-------------------------------------
import csv
list1=[]
class1=[]
stopwords="i am you we an in on where is are what which here"
slist=stopwords.split()
csvinput=open("trainingdata.csv","r")
reader=csv.reader(csvinput,delimiter=",")
for sym,label in reader:
  list1.append(sym)
  class1.append(label)
print(list1)
print(class1)
in1=input("Enter your symptoms")
inlist=in1.split()
newlist=list(set(inlist)-set(slist))
print(newlist)
j=0
for i in list1:
  #print(i)
  templist=i.split()
  commonlist=list(set(templist)&set(newlist))
  l1=len(commonlist)
  percentage=l1/len(templist)*100
  #print(templist)
  print(class1[j],percentage)
  j=j+1
  

Read tweets

Hi all
Use this code for reading tweets
____________________
import tweepy #https://github.com/tweepy/tweepy
import csv

#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = "-"
access_secret = ""


#def get_all_tweets(screen_name):
print("entered HUP")
#Twitter only allows access to a users most recent 3240 tweets with this method

#authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

#initialize a list to hold all the tweepy Tweets
alltweets = []

#make initial request for most recent tweets (200 is the maximum allowed count)
new_tweets = api.user_timeline(screen_name = "sumeesh96283695",count=200)
 
#get_all_tweets("sumeesh96283695")
alltweets.extend(new_tweets)

#update the id of the oldest tweet less one
oldest = alltweets[-1].id - 1

print ("...%s tweets downloaded so far" % (len(alltweets)))

#transform the tweepy tweets into a 2D array that will populate the csv
outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]















for i in outtweets:
  print(i)
  print("-----------")

Saturday, 13 July 2019

Complete Program

Hi All,

Please find the complete Sentimental Analysis program

trainingdata.csv

i am fine,neutral
its great,positive
he is good,positive
so bad,negative
you are waste,negative

testdata.csv

1,raj,i am fine
2,manu,he is great
3,Raji,it is bad

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



from nltk import NaiveBayesClassifier as nbc
from nltk.tokenize import word_tokenize
from itertools import chain
import csv

with open('trainingdata.csv','r') as csvinput:
    reader=csv.reader(csvinput,delimiter=",")
    rownum = 0 
    training_data = []

    for row in reader:
        training_data.append (row)
        rownum += 1

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[2]
            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)

Friday, 12 July 2019

Dear All,

Find the steps in Twitter Sentimental Analysis using Python

1. Import necessary packages

from nltk import NaiveBayesClassifier as nbc
from nltk.tokenize import word_tokenize
from itertools import chain
import csv


2. Read the input file using csv reader and generate a list of those tweets

3. Generate a vocabulary

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


4. Generate training data

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

5. Train the classifier

classifier = nbc.train(feature_set)

6. Generate output csv file

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

7. Generate Test Input

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

8. Classfiy and create output data

row.append(classifier.classify(featurized_test_sentence))
all.append(row)

9. Flush output data to an output csv file

writer.writerows(all)

Wednesday, 21 November 2018

PIP - How to install PIP in Python

PIP and Python

Hi All,

PIP is a recursive Acronym PIP Installs Packages.
This is used to install Python packages.

We can download the pip.py from internet and run to configure the same.
Once installation is done we can install packages in Python using pip.

Eg

pip install packagename

Wednesday, 18 July 2018

PIR Sensor

Hi All,

Using this PIR sensor we can sense the presence of Human beings.

Use these resources for PIR Sensor.

Video : for Connection

https://www.youtube.com/watch?v=YFjEXt5oBYE
[Search for 'How To Connect PIR Motion Detector Sensor [Arduino Tutorial] ']

Code : for execution

http://playground.arduino.cc/Code/PIRsense



Saturday, 21 April 2018

Simple Python Program

Hi All,

See some simple programs in Python.

#Program 1
# To Add 2 numbers

num1 = 1.2
num2 = 3.3
# Add numbers
result= float(num1) + float(num2)
# Display the result
print('The sum of {0} and {1} is {2}'.format(num1, num2, result))