Python Forum
Need Help with this peace of code.PLease Exlain Each line of the code.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help with this peace of code.PLease Exlain Each line of the code.
#1
#find sentences containing HTML tags
i=0;
for sent in final['Text'].values:
    if (len(re.findall('<.*?>', sent))):
        print(i)
        print(sent)
        break;
    i += 1;   
Reply
#2
import re
import string
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.stem.wordnet import WordNetLemmatizer

stop = set(stopwords.words('english')) #set of stopwords
sno = nltk.stem.SnowballStemmer('english') #initialising the snowball stemmer

def cleanhtml(sentence): #function to clean the word of any html-tags
    cleanr = re.compile('<.*?>')
    cleantext = re.sub(cleanr, ' ', sentence)
    return cleantext
def cleanpunc(sentence): #function to clean the word of any punctuation or special characters
    cleaned = re.sub(r'[?|!|\'|"|#]',r'',sentence)
    cleaned = re.sub(r'[.|,|)|(|\|/]',r' ',cleaned)
    return  cleaned
print(stop)
print('************************************')
print(sno.stem('tasty'))
Reply
#3
#Code for implementing step-by-step the checks mentioned in the pre-processing phase
# this code takes a while to run as it needs to run on 500k sentences.
i=0
str1=' '
final_string=[]
all_positive_words=[] # store words from +ve reviews here
all_negative_words=[] # store words from -ve reviews here.
s=''
for sent in final['Text'].values:
    filtered_sentence=[]
    #print(sent);
    sent=cleanhtml(sent) # remove HTMl tags
    for w in sent.split():
        for cleaned_words in cleanpunc(w).split():
            if((cleaned_words.isalpha()) & (len(cleaned_words)>2)):    
                if(cleaned_words.lower() not in stop):
                    s=(sno.stem(cleaned_words.lower())).encode('utf8')
                    filtered_sentence.append(s)
                    if (final['Score'].values)[i] == 'positive': 
                        all_positive_words.append(s) #list of all words used to describe positive reviews
                    if(final['Score'].values)[i] == 'negative':
                        all_negative_words.append(s) #list of all words used to describe negative reviews reviews
                else:
                    continue
            else:
                continue 
    #print(filtered_sentence)
    str1 = b" ".join(filtered_sentence) #final string of cleaned words
    #print("***********************************************************************")
    
    final_string.append(str1)
    i+=1
Reply
#4
I have merge your 3 threads,it was a close call with delete Dodgy
We are not gone explain what each line dos.
You have to do the effort,it there are certain lines you wonder about ask about them.
Reply
#5
Then, Please give an idea of what all the above three code snippets do briefly explaining each code snippet in detail.

(Mar-08-2018, 03:49 PM)snippsat Wrote: I have merge your 3 threads,it was a close call with delete Dodgy
We are not gone explain what each line dos.
You have to do the effort,it there are certain lines you wonder about ask about them.

Then, Please give an idea of what all the above three code snippets do briefly explaining each code snippet in detail.
Reply
#6
The code is already very well documented as to what it does. As was pointed out, we are not going to explain the purpose of each line. You might want to start with finding a tutorial for Python beginners to learn the basics of the language.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to understand the meaning of the line of code. jahuja73 0 299 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 985 Nov-15-2023, 06:56 PM
Last Post: jst
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,657 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  python multiple try except block in my code -- can we shorten code mg24 10 6,091 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  faster code for my code kucingkembar 19 3,198 Aug-09-2022, 09:48 AM
Last Post: DPaul
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,395 Feb-13-2022, 07:13 PM
Last Post: menator01
  how long can a line of code be? Skaperen 2 2,205 Jun-09-2021, 06:31 PM
Last Post: Skaperen
  Putting code into a function breaks its functionality, though the code is identical! PCesarano 1 1,978 Apr-05-2021, 05:40 PM
Last Post: deanhystad
  HackerRank Problem: Code works on VS Code but not on the HackerRank site Pnerd 3 2,633 Feb-28-2021, 07:12 PM
Last Post: Pnerd
  I need a code line to spam a keyboard key | Image detection bot Aizou 2 3,114 Dec-06-2020, 10:10 PM
Last Post: Aizou

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020