Random ...
 
December 2017
S M T W T F S
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Tags ...
Links



python+zope++:: Seeding tag clouds with Zope (II)
Posted at 15.Sep,2006 17:09  Comments 0 / Trackbacks 0 / Like this post!
Technorati tag(s):

We can extend the previous entry and make use of any RDB to seed tag clouds.

Here's one use case: record all search terms used in a site and store them in an RDB (postgresql, in my case). Now we can generate our own hot searches.

I used a simple schema; just the id and the terms. Nothing fancy:

 --
 -- PostgreSQL database dump
 --

 \connect - postgres

 SET search_path = public, pg_catalog;

 --
 -- TOC entry 2 (OID 767497)
 -- Name: searchtag; Type: TABLE; Schema: public; Owner: postgres
 --

 CREATE TABLE searchtag (
    searchid serial NOT NULL,
    tags character varying(255)
 );

 --
 -- TOC entry 3 (OID 767497)
 -- Name: searchtag; Type: ACL; Schema: public; Owner: postgres
 --

 REVOKE ALL ON TABLE searchtag FROM PUBLIC;
 GRANT ALL ON TABLE searchtag TO somebody;
 --
 -- TOC entry 4 (OID 767497)
 -- Name: searchtag_searchid_seq; Type: ACL; Schema: public;  Owner: postgres
 --

 REVOKE ALL ON TABLE searchtag_searchid_seq FROM PUBLIC;
 GRANT ALL ON TABLE searchtag_searchid_seq TO somebody;

 --
 -- TOC entry 5 (OID 767500)
 -- Name: tags_idx; Type: INDEX; Schema: public; Owner: postgres
 --

 CREATE INDEX tags_idx ON searchtag USING btree (tags);

Now we instantiate a ZPsycopgDA and enter the necessary (dbname, user, etc). After verifying that it works, we need to prepare three ZSQL Methods to insert, select distinct tags and select the number of times the tags appeared.

insertTags:

 insert into searchtag values (nextval('searchtag_searchid_seq'),
<dtml-sqlvar tags type=string>)

getDistinctTags:

  select distinct tags from searchtag

getTagCounts:

  select count(*) as num from searchtag where <dtml-sqltest tags type=string>

Now, we use the same tagAndPixels Script (Python) and modify a bit:

 zenbat = {}
 for tag in context.getDistinctTags(): #our zsql method
   tagkop = context.getTagCount(tags=tag[0])[0].num #same here
   if _.int(tagkop) > 45:
     zenbat[tag[0]] = _.float(tagkop)

 maxpx = 3.20  
 minpx = 0.85
 difpx = maxpx-minpx

 if zenbat.values():
    maxnum = max(zenbat.values())
    minnum = max(zenbat.values())

 else:
    maxnum = 0
    minnum = 0
 hiz = {}
 for k,v in zenbat.items():
    hiz[k] = float(difpx*v/maxnum)+minpx

 return hiz

We need another Script (Python) to actually insert the terms into Postgresql.

Let's call it insertTag:

 r=context.REQUEST
 try:
  if r.form['src_term']:
    s=context.insertTags
    s(tags=r.form['src_term'].lower().strip())
 except:
  pass  # bare pass is bad form. don't do this!

Now that everything's ready, call insertTag from somewhere; the action to search is probably a good place.

To display the tag cloud:

 <dtml-let pxk="tagsAndPixels()"
          pxks="pxk.keys()">
 <dtml-call "pxks.sort()">

 <dtml-in "pxks">
  <a  href="/result?src_term=<dtml-var "'+'.join(_['sequence-item'].split())">"  style="font-size:<dtml-var "pxk[_['sequence-item']]">em;" ><dtml-var sequence-item></a>&nbsp;
 </dtml-in>
 </dtml-let>

I have no demo for this site yet, so you guys just have to trust me ;)

Note: accessing Postgresql from zope may block if we have a query that takes too long to finish and you'll end up waiting. I know, I encountered that.

To counter that, let each ZSQL Method use different connection instance. Also, make use of RAM Cache and cache tagsAndPixels.

At first, I thought there was an increase in visitors. However, after I did the above, things went back to normal. So, loaded site and long query may make zope wait.


Bookmark and Share

Is this entry helpful? Comments/Donate/Click some google ads.  
Trackback is http://myzope.kedai.com.my/blogs/kedai/101/tbping 

Comments
Post a comment