- IF
- IF ELIF
- IF ELIF ELSE
- WHILE
- BREAK
- CONTINUE
- ELSE
- FOR
- FOR ... IN
- FOR ... IN RANGE
- ELSE
Most of the examples below used Lambda to iterate through Iterable Object values and process them. Some results are then passed into Map, Filter, and Reduce functions. Further reading: (1) Lambda, (2) Iterable Objects, (3) Map, Filter, and Reduce functions.
print ("\n====================\n") print ("This program displays list in vertical order.") def printListVertically(list): for item in list: print (item) print ("\n====================\n") text='''called cust care but no response so far so good :-) very low power processor there is no option to activate in the website no problem at all i don't know why it is so complicated :( don't know how to contact them now :( customer service is also good i don't think they allow it these days :-( didn't know that you can do it that's really good to know very happy now :) so happy with the services but I found that the rates were very low''' listSentence=text.split("\n") print ("Sample sentences:") printListVertically(listSentence) print ("\n====================\n") dictEmoticon={":-)":"pos",":)":"pos",":-(":"neg",":(":"neg"} print ("Emoticons and their polarities:") # use zip function to convert dictionary to list object listEmoticon=list(zip(dictEmoticon.keys(), dictEmoticon.values())) printListVertically(listEmoticon) print ("\n====================\n") # use lambda to iterate through object item, filter their values and convert to dict dictPositiveEmoticon = dict(filter(lambda x:x[1] in ['pos'],listEmoticon)) print ("Positive Emoticons:") printListVertically(dictPositiveEmoticon) # alternative: # dictfilt =lambda x,y: dict([ (i,x[i]) for i in x if x[i]=='pos' ]) # dictPositiveEmoticon= dictfilt(dictEmoticon,"pos") # print(dictPositiveEmoticon) print ("\n====================\n") # (1) IF def findEmoticon(sentence): found=False # for each emoticon in dictionary, find them in sentence for emoticon in set(dictEmoticon.keys()): if (emoticon in sentence): found=True return found # use lambda to iterate through object item and then filter their values listEmoticonFound= filter(lambda element:findEmoticon(element)==True,listSentence) print ("Sentences containing emoticons:") printListVertically(listEmoticonFound) print ("\n====================\n") # (2) IF ELIF def findHappyToken(sentence): found=False # tokenize sentence listWord=sentence.split() #print(listWord) # if there is any token in listWord matches with token in positive emoticon dictionary if (any(substring in listWord for substring in dictPositiveEmoticon)): found=True #if there is any token in listWord matches with token in happy list elif (any(substring in listWord for substring in ['happy'])): found=True return found # use lambda to iterate object item and then filter their values listHappyTokenFound= filter(lambda element:findHappyToken(element)==True,listSentence) print ("Sentences containing happy tokens:") printListVertically(listHappyTokenFound) print ("\n====================\n") # (3) IF ELIF ELSE def findHappySentence(sentence): emotion='' # tokenize sentence listWord=sentence.split() # if there is any token in listWord matches with token in positive emoticon dictionary if (any(substring in listWord for substring in dictPositiveEmoticon)): emotion='happy' #if there is any token in listWord matches with token in happy list elif (any(substring in listWord for substring in ['happy'])): emotion='happy' else: emotion='unknown' return "["+emotion+"]"+sentence # use lambda to iterate through object item and then map their values listHappySentence=map( lambda element:findHappySentence(element),listSentence) print ("Sentences and their happy emotions:") printListVertically(sorted(listHappySentence)) print ("\n====================\n")
~
python, colab, trinket, jupyter, control structures, lambda, text analysis python, colab, trinket, jupyter, control structures, lambda, text analysis python, colab, trinket, jupyter, control structures, lambda, text analysis python, colab, trinket, jupyter, control structures, lambda, text analysis python, colab, trinket, jupyter, control structures, lambda, text analysis python, colab, trinket, jupyter, control structures, lambda, text analysis python, colab, trinket, jupyter, control structures, lambda, text analysis python, colab, trinket, jupyter, control structures, lambda, text analysis python, colab, trinket, jupyter, control structures, lambda, text analysis
0 Comments