how can i loop through the coming frequency of the keyword
This sounds very much like a homework assignment.
c.f. BashFAQ for better reads; keeping this simple to focus on what you asked for.
Rewritten for more precise formatting -
while read key # read each search key
do cnt=$(grep "$key" test|wc -l) # count the hits
pad="$key,searchall,$cnt," # build the "header" fields
while read line # read the input from grep
do if [[ "$line" =~ ^-- ]] # treat hits separately
then pad="$key,searchall,$cnt," # reset the "header"
echo # add the blank line
continue # skip to next line of data
fi
echo "$pad$line" # echo "header" and data
pad="${pad//?/ }" # convert header to spacving
done < <( grep -B1 -A1 "$key" test ) # pull hits for this key
echo # add blank lines between
done < word.txt # set stdin for the outer read
$: cat word.txt
course
red hat
$: ./tst
course,searchall,1,centos is my bro
red hat is my course
ubuntu is my OS
red hat,searchall,2,centos is my bro
red hat is my course
ubuntu is my OS
red hat,searchall,2,tom outsmart jerry
red hat is my boy
jerry is samall
Python: How to keep repeating a program until a specific input is obtained?
There are two ways to do this. First is like this:
while True: # Loop continuously
inp = raw_input() # Get the input
if inp == "": # If it is a blank line...
break # ...break the loop
The second is like this:
inp = raw_input() # Get the input
while inp != "": # Loop until it is a blank line
inp = raw_input() # Get the input again
Note that if you are on Python 3.x, you will need to replace raw_input
with input
.
How can I loop through variables in SPSS? I want to avoid code duplication
There are two easy solutions for looping through variables (easier compared to using Python in SPSS).
1) DO REPEAT-END REPEAT
The draw back is that you can use DO REPEAT-END REPEAT
mainly only for data transformations - for example COMPUTE
, RECODE
etc. Frequencies are not allowed. For example:
DO REPEAT R=REGION1 TO REGION5.
COMPUTE R=0.
END REPEAT.
2) DEFINE-!ENDDEFINE
(macro facility)
You can do Frequencies in a loop of variables using macro command. For example:
DEFINE macdef (!POS !CHAREND('/'))
!DO !i !IN (!1)
frequencies variables = !i.
!DOEND
!ENDDEFINE.
macdef VAR1 VAR2 VAR3 /.
Loop through text and extract pre-defined words and word pairs in Rails
description = "The quick brown fox jumped over the lazy dog, and another brown dog"
keywords = ["brown", "lazy", "apple"]
matches = []
keywords.each do |keyword|
matches << description.match(keyword).to_s if description.match(keyword)
end
puts matches
#=> ["brown", "lazy"]
matches.each do |keyword|
description.gsub!(Regexp.new(keyword), '')
end
description.gsub!(' ', ' ')
puts description
#=> "The quick fox jumped over the dog, and another dog"
How can I input the list of words in a loop that can be added to the url to get the results
Make a list for your keywords, iterate it and include the while loop into each iteration.
keywords = ['speakers','earbuds']
for k in keywords:
url = 'https://www.amazon.in/s?k='+k
while True:
geturl = getdata(url)
url = getnextpage(geturl)
if not url:
break
print(url)
Be aware that amazon does not like such automated access to its pages and recognises the patterns of access quite quickly. To reduce the frequency of the requests a bit, you should at least include some delay time.sleep()
. Of course, it would be even better to use an official api.
Example
from bs4 import BeautifulSoup as soup
import pandas as pd
import requests
import urllib
data =[]
def getdata (url):
header = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }
req = urllib.request.Request(url, headers=header)
amazon_html = urllib.request.urlopen(req).read()
a_soup = soup(amazon_html,'html.parser')
for e in a_soup.select('div[data-component-type="s-search-result"]'):
try:
title = e.find('h2').text
except:
title = None
data.append({
'title':title,
'url':'http://www.amazon.in' + e.h2.a['href']
})
return a_soup
def getnextpage(a_soup):
try:
page = a_soup.find('a',attrs={"class": 's-pagination-item s-pagination-next s-pagination-button s-pagination-separator'})['href']
url = 'http://www.amazon.in'+ str(page)
except:
url = None
return url
keywords = ['speakers','earbuds']
for k in keywords:
url = 'https://www.amazon.in/s?k='+k
while True:
geturl = getdata(url)
url = getnextpage(geturl)
if not url:
break
print(url)
Output (print)
http://www.amazon.in/s?k=speakers&page=2&qid=1649420352&ref=sr_pg_1
...
http://www.amazon.in/s?k=speakers&page=20&qid=1649420373&ref=sr_pg_19
http://www.amazon.in/s?k=earbuds&page=2&qid=1649420375&ref=sr_pg_1
...
http://www.amazon.in/s?k=earbuds&page=20&qid=1649420394&ref=sr_pg_19
Output (pd.DataFrame(data)
)
title | url | |
---|---|---|
0 | Echo Dot (3rd Gen) - #1 smart speaker brand in India with Alexa (Black) | http://www.amazon.in/gp/bestsellers/electronics/15765862031/ref=sr_bs_0_15765862031_1 |
1 | TimbreSonic Rhythm Speaker Wired Karaoke Ultimate Sound Party Portable Speaker | http://www.amazon.in/gp/slredirect/picassoRedirect.html/ref=pa_sp_atf_aps_sr_pg1_1?ie=UTF8&adId=A01688993VZM1IH2U6JB5&url=%2FTimbreSonic-Speaker-Karaoke-Ultimate-Portable%2Fdp%2FB096M2T346%2Fref%3Dsr_1_2_sspa%3Fkeywords%3Dspeakers%26qid%3D1649421227%26sr%3D8-2-spons%26psc%3D1%26smid%3DAK0P65LCJ5QQN&qualifier=1649421227&id=2899208110237385&widgetName=sp_atf |
2 | boAt Stone 180 5W Bluetooth Speaker with Upto 10 Hours Playback, 1.75" Driver, IPX7 and TWS Feature(Black) | http://www.amazon.in/boAt-Stone-Bluetooth-Speaker-Black/dp/B08JMC1988/ref=ice_ac_b_dpb?keywords=speakers&qid=1649421227&sr=8-3 |
3 | Speaker | http://www.amazon.in/Generic-Speaker/dp/B09X5M77MZ/ref=sr_1_omk_4?keywords=speakers&qid=1649421227&sr=8-4 |
4 | Zebronics Zeb-Warrior 2.0 Multimedia Speaker with Aux Connectivity,USB Powered and Volume Control | http://www.amazon.in/gp/bestsellers/computers/1375442031/ref=sr_bs_4_1375442031_1 |
... | ... | ... |
847 | Zebronics Zeb-Sound Bomb 5 TWS Earbuds with Bluetooth v5.0, up to 22H Backup, Flash Connect, Splash Proof, Voice Assistant, Touch Control, 10mm Driver, Built in Microphone and Type C(Black) | http://www.amazon.in/gp/slredirect/picassoRedirect.html/ref=pa_sp_mtf_aps_sr_pg20_1?ie=UTF8&adId=A09061362IHFGLF39FZ4K&url=%2FZebronics-Zeb-Sound-Bluetooth-Assistant-Microphone%2Fdp%2FB09NNNLBVD%2Fref%3Dsr_1_308_sspa%3Fkeywords%3Dearbuds%26qid%3D1649420939%26sr%3D8-308-spons%26psc%3D1&qualifier=1649420939&id=2014190349292195&widgetName=sp_mtf |
848 | boAt Airdopes 141 True Wireless Earbuds with 42H Playtime, Beast Mode(Low Latency Upto 80ms) for Gaming, ENx Tech, ASAP Charge, IWP, IPX4 Water Resistance, Smooth Touch Controls(Bold Black) | http://www.amazon.in/gp/slredirect/picassoRedirect.html/ref=pa_sp_mtf_aps_sr_pg20_1?ie=UTF8&adId=A08646093S9SKZXE3VDX4&url=%2FboAt-Airdopes-141-Wireless-Resistance%2Fdp%2FB09N3ZNHTY%2Fref%3Dsr_1_309_sspa%3Fkeywords%3Dearbuds%26qid%3D1649420939%26sr%3D8-309-spons%26psc%3D1&qualifier=1649420939&id=2014190349292195&widgetName=sp_mtf |
849 | Skyfly Xbot GE100 Wired in Ear Earphones with Mic (Black) | http://www.amazon.in/Skyfly-Xbot-Gaming-Earphones-Detachable/dp/B07ZYR78B3/ref=sr_1_310?keywords=earbuds&qid=1649420939&sr=8-310 |
850 | JBL C115 TWS, True Wireless Earbuds with Mic, Jumbo 21 Hours Playtime with Quick Charge, True Bass, Dual Connect, Bluetooth 5.0, Type C & Voice Assistant Support for Mobile Phones (Black) | http://www.amazon.in/gp/slredirect/picassoRedirect.html/ref=pa_sp_btf_aps_sr_pg20_1?ie=UTF8&adId=A0791293Y8WP49FN4EZU&url=%2FJBL-Wireless-Bluetooth-Assistance-Integration%2Fdp%2FB08L5ZC8R3%2Fref%3Dsr_1_311_sspa%3Fkeywords%3Dearbuds%26qid%3D1649420939%26smid%3DA14CZOWI0VEHLG%26sr%3D8-311-spons%26psc%3D1&qualifier=1649420939&id=2014190349292195&widgetName=sp_btf |
851 | Crossbeats Airpop Bluetooth Truly Wireless In Ear Earbuds With Mic, with 30Hrs Playtime Ultralight Bluetooth Earphone with Mic & Voice Assistant, Passive Noise Cancelling Headset, Type-C Fasting Charging - Blue | http://www.amazon.in/gp/slredirect/picassoRedirect.html/ref=pa_sp_btf_aps_sr_pg20_1?ie=UTF8&adId=A10368023R9B7RAUU82SP&url=%2FCrossbeats-Bluetooth-Ultralight-Assistant-Cancelling%2Fdp%2FB09PDSVQTW%2Fref%3Dsr_1_312_sspa%3Fkeywords%3Dearbuds%26qid%3D1649420939%26sr%3D8-312-spons%26psc%3D1&qualifier=1649420939&id=2014190349292195&widgetName=sp_btf |
iOS iPhone how to list all keywords in a UTextView by frequency of use?
I ended up going with the CFStringTokenizer
. I'm not sure if the bridged casts below are correct, but it seems to work
-(void)listAllKeywordsInString:(NSString*)text
{
if(text!=nil)
{
NSMutableDictionary* keywordsDictionary = [[NSMutableDictionary alloc] initWithCapacity:1024];
NSString* key = nil;
NSLog(@"%@",text);
NSLog(@"Started parsing: %@",[[NSDate date] description]);
CFStringRef string =(__bridge CFStringRef)text; // Get string from somewhere
CFStringTokenizerRef tokenizer = CFStringTokenizerCreate(kCFAllocatorDefault, (__bridge_retained CFStringRef) text, CFRangeMake (0,CFStringGetLength((__bridge_retained CFStringRef)text)), kCFStringTokenizerUnitWord, CFLocaleCopyCurrent());
unsigned tokensFound = 0; // or the desired number of tokens
CFStringTokenizerTokenType tokenType = kCFStringTokenizerTokenNone;
while(kCFStringTokenizerTokenNone != (tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer)) ) {
CFRange tokenRange = CFStringTokenizerGetCurrentTokenRange(tokenizer);
CFStringRef tokenValue = CFStringCreateWithSubstring(kCFAllocatorDefault, string, tokenRange);
// This is the found word
key =(__bridge NSString*)tokenValue;
//increment its count
NSNumber* count = [keywordsDictionary objectForKey:key];
if(count!=nil)
{
[keywordsDictionary setValue:[NSNumber numberWithInt:1] forKey:key];
}else {
[keywordsDictionary setValue:[NSNumber numberWithInt:count.intValue+1] forKey:key];
}
CFRelease(tokenValue);
++tokensFound;
}
NSLog(@"Ended parsing. tokens Found: %d, %@",tokensFound,[[NSDate date] description]);
NSLog(@"%@",[keywordsDictionary description]);
// Clean up
CFRelease(tokenizer);
}
}
Count frequency of words inside a list in a dictionary
You can use a list-comprehension along with collections.Counter
which does exactly what you want with the nested list. -
from collections import Counter
[{'name':i.get('name'),
'keywords':[dict(Counter([j for j in i.get('keywords')
if j in common_keywords]))]} for i in people]
[{'name': 'Bob', 'keywords': [{'dog': 2}]},
{'name': 'Kate', 'keywords': [{'cat': 1}]},
{'name': 'Sasha', 'keywords': [{'person': 1, 'cat': 1}]}]
- First, with the list comprehension you want to reconstruct the original list of dicts with keys separately defined along with
i.get('key')
. This will let to work with the nested list value for keywords. - Iterate over the list and filter only the ones in common_keywords
- Pass this list into collections.Counter to get your dict
- Return it as a list with a single dict inside as you expect it to be
Related Topics
Linux Newbie: Linux VS Posix Manual
Bash, Linux, Need to Remove Lines from One File Based on Matching Content from Another File
Best Way to Set Environment Variables in Calling Shell
Bash Sort - How to Sort Using Timestamp
Emulating Slurm on Ubuntu 16.04
How to Find Files Recursively by File Type and Copy Them to a Directory
Linux/Bash, Using Ps -O to Get Process by Specific Name
Where Is Path Variable Set in Ubuntu
What Are Meanings of Fields in /Proc/Net/Dev
How to Make One Linux Kernel Module Depend on Another External Module with Depmod
String Comparison Not Working Properly
How to Count the Number of Occurrences of a String in an Entire File
Set Filetype and Comment Key Map with .S File
Starting a Shell in the Docker Alpine Container
How to Request a File But Not Save It with Wget
What's the Difference of Redirect an Output Using ">", "&>", ">&" and "2&>"