def search2(): results0=[] results2=[] loc=[] sr='' tmp='' mark=False if request.method == 'POST': try: initVM() directory = SimpleFSDirectory(File('albumIndex')) searcher2 = IndexSearcher(directory, True) analyzer = StandardAnalyzer(Version.LUCENE_CURRENT) if "Search" in request.form.values(): sr=request.form['text'] elif "Shuffle" in request.form.values(): mark=True while len(loc)<20: tmp=random.randint(0,searcher2.maxDoc()-1) if tmp not in loc: loc+=[tmp] if mark: print 'loc=',loc ct=0 for i in loc: doc = searcher2.doc(i) songs=doc.get('albumsongs') songs=songs.split('!@#$%') urls=doc.get("albumsongURLs") urls=urls.split('!@#$%') results2+=[{'albumnum': doc.get("albumnum"),\ 'albumname':doc.get('albumname'),\ 'albumartist':doc.get('albumartist'),\ 'albumintro': doc.get("albumintro"),\ 'albumsongs':songs,\ 'albumsongURLs': urls,\ 'albumpicURL':doc.get('albumpicURL'),\ 'albumartistURL':doc.get('albumartistURL'),\ 'albumURL':doc.get('albumURL'),\ 'rank':100}] else: print request.form.values() print 'sr=',sr if sr=='': return results0,results2,"" ## for i in sr: ## tmp+=i+" " ## print tmp scoreDocs=run2(searcher2, analyzer,sr,1) #search exact album if scoreDocs!=False: doc=scoreDocs songs=doc.get('albumsongs') songs=songs.split('!@#$%') urls=doc.get("albumsongURLs") urls=urls.split('!@#$%') results2+=[{'albumnum': doc.get("albumnum"),\ 'albumname':doc.get('albumname'),\ 'albumartist':doc.get('albumartist'),\ 'albumintro': doc.get("albumintro"),\ 'albumsongs':songs,\ 'albumsongURLs': urls,\ 'albumpicURL':doc.get('albumpicURL'),\ 'albumartistURL':doc.get('albumartistURL'),\ 'albumURL':doc.get('albumURL'),\ 'rank':100}] results0=results2 else: scoreDocs=run2(searcher2, analyzer,sr,20) #search 20 albums rank=100 for scoreDoc in scoreDocs: doc = searcher2.doc(scoreDoc.doc) songs=doc.get('albumsongs') songs=songs.split('!@#$%') urls=doc.get("albumsongURLs") urls=urls.split('!@#$%') results2+=[{'albumnum': doc.get("albumnum"),\ 'albumname':doc.get('albumname'),\ 'albumartist':doc.get('albumartist'),\ 'albumintro': doc.get("albumintro"),\ 'albumsongs':songs,\ 'albumsongURLs': urls,\ 'albumpicURL':doc.get('albumpicURL'),\ 'albumartistURL':doc.get('albumartistURL'),\ 'albumURL':doc.get('albumURL'),\ 'rank':rank}] rank-=5 conn = MySQLdb.connect(host='localhost', user='******',passwd='1234',charset="utf8") # conn = MySQLdb.connect(host='localhost', user='******',passwd='ee208',charset="utf8") conn.select_db('coversearch'); cursor = conn.cursor() for i in results2: try: cursor.execute("select zan from albums where id="+i['albumnum']) zan=cursor.fetchone()[0] i['zan']=zan i['rank']+=int(zan) except: i['zan']=0 results2.sort(key=lambda x:x['rank'],reverse=True) results2=resortalbum(results2,sr) conn.commit() cursor.close() conn.close() searcher2.close() except Exception,e: print 2,e
def search(): results=[] results3=[] loc=[] sr='' tmp='' mark=False if request.method == 'POST': try: initVM() directory = SimpleFSDirectory(File("songIndex")) searcher = IndexSearcher(directory, True) directory = SimpleFSDirectory(File("artistIndex")) searcher3 = IndexSearcher(directory, True) analyzer = StandardAnalyzer(Version.LUCENE_CURRENT) if "Search" in request.form.values(): sr=request.form['text'] elif "Shuffle" in request.form.values(): mark=True while len(loc)<20: tmp=random.randint(0,searcher.maxDoc()-1) if tmp not in loc: loc+=[tmp] ## if request.form['action']=="Search": ## sr=request.form['text'] ## elif request.form['action']=="Shuffle": ## sr='1' if mark: print 'loc=',loc for i in loc: doc = searcher.doc(i) results+=[{'songname':doc.get("songname"),\ 'songurl':doc.get('songurl'),\ 'albumname':doc.get('songalbum'),\ 'songartist':doc.get('songartist'),\ 'albumurl': doc.get("songalbumURL"),\ 'picPath':doc.get('songpicURL'),\ }] else: print request.form.values() print 'sr=',sr if sr=='': return results,results3,"" for i in sr: tmp+=i+" " ## print tmp ## scoreDocs=run2(searcher2, analyzer,sr) ## if len(scoreDocs)!=0: ## doc=searcher2.doc(scoreDocs[0].doc) ## results2+=[{'albumnum:', doc.get("albumnum"),\ ## 'albumname:',doc.get('albumname'),\ ## 'albumartist:',doc.get('albumartist'),\ ## 'albumintro:', doc.get("albumintro"),\ ## 'albumsongs:',doc.get('albumsongs'),\ ## 'albumsongURLs:', doc.get("albumsongURLs"),\ ## 'albumpicURL:',doc.get('albumpicURL')}] ## else: scoreDocs=run3(searcher3,analyzer,sr) if scoreDocs == False: scoreDocs=run(searcher, analyzer,sr) for scoreDoc in scoreDocs: doc = searcher.doc(scoreDoc.doc) results+=[{'songname':doc.get("songname"),\ 'songurl':doc.get('songurl'),\ 'albumname':doc.get('songalbum'),\ 'songartist':doc.get('songartist'),\ 'albumurl': doc.get("songalbumURL"),\ 'picPath':doc.get('songpicURL')\ }] else: doc=scoreDocs singeralbums=doc.get('singeralbums') singeralbums=singeralbums.split('!@#$%') singeralbumURLs=doc.get("singeralbumURLs") singeralbumURLs=singeralbumURLs.split('!@#$%') results3+=[{'singername': doc.get("singername"),\ 'singerplace':doc.get('singerplace'),\ 'singerintro':doc.get('singerintro'),\ 'singeralbums': singeralbums,\ 'singeralbumURLs':singeralbumURLs,\ 'singerpicURL': doc.get("singerpicURL")\ }] searcher.close() except Exception,e: print 1,e
# print ngram import itertools list_of_NNPs.sort() unique_NNPs = list(list_of_NNPs for list_of_NNPs,_ in itertools.groupby(list_of_NNPs)) return unique_NNPs if __name__ == '__main__': reload(sys) sys.setdefaultencoding('iso-8859-1') STORE_DIR = None while STORE_DIR == None: path = raw_input("What's the path to the index :") if path != '' and path != None : STORE_DIR = path initVM() print 'lucene', VERSION directory = SimpleFSDirectory(File(STORE_DIR)) searcher = IndexSearcher(directory, True) print searcher.maxDoc() analyzer = StandardAnalyzer(Version.LUCENE_CURRENT) run(searcher, analyzer) searcher.close()