-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
118 lines (96 loc) · 3.62 KB
/
app.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from flask import Flask, jsonify
from flask import request
import lucene
import sys
from ConfigParser import *
from lucene import \
SimpleFSDirectory, System, File, \
Document, Field, StandardAnalyzer, IndexWriter, Version
# imports related to lucene
indexDir = "/tmp/luceneindex"
app = Flask(__name__)
# lucene_dir = "/tmp/luceneindex"
@app.route('/' , methods=['POST','GET'])
def index():
if request.method == 'POST':
return request.form['product'].upper()
else:
return 'Flask is good with Get Method..:P'
@app.route('/_get_data' , methods=['POST','GET'])
def names():
lst = []
search = "spax"#request.form['product']
lucene.initVM()
dir = SimpleFSDirectory(File(indexDir))
analyzer = StandardAnalyzer(Version.LUCENE_30)
searcher = IndexSearcher(dir)
query = QueryParser(lucene.Version.LUCENE_CURRENT, "text", analyzer).parse(search)
MAX = 1000
hits = searcher.search(query, MAX)
print "Found %d document(s) that matched query '%s':" % (hits.totalHits, query)
for hit in hits.scoreDocs:
if hit.score >= 1:
print hit.score, hit.doc, hit.toString()
doc = searcher.doc(hit.doc)
print doc.get("text").encode("utf-8")
items = doc.get("text").encode("utf-8").split(',')
for item in items:
if item == search:
pass
elif item not in lst:
lst.append(item)
#print lst
data = {"products": lst}
if request.method == 'POST':
return jsonify(data)
else:
return jsonify(data)
@app.route('/demo' , methods=['POST','GET'])
def index1():
if request.method == 'POST':
query_var = request.form['product'].upper()
# search for it using lucene retriever
#op = lucene_retriever(query_var) # Handle exception
#return op
return query_var
else:
return request.form['product'].upper()
#def lucene_retriver(text):
# search for that text in index
# return line
# Before all requests, build lucene index
@app.before_first_request
def configure_lucene():
f = open('clique.txt','r')
lucene.initVM()
print 'Inside Function'
#indexDir = "/tmp/luceneindex"
dir = SimpleFSDirectory(File(indexDir))
analyzer = StandardAnalyzer(lucene.Version.LUCENE_CURRENT)
writer = IndexWriter(dir, analyzer, True, IndexWriter.MaxFieldLength(512))
print >> sys.stderr, "Currently there are %d documents in the index..." % writer.numDocs()
print >> sys.stderr, "Reading lines from sys.stdin..."
for line in f:
line = line.replace('\t','')
line = line.replace('\r','')
line = line.replace('\n','')
line = line.replace('^','')
line = line.strip()
doc = Document()
doc.add(Field("text", line, Field.Store.YES, Field.Index.ANALYZED))
writer.addDocument(doc)
print >> sys.stderr, "Indexed lines from stdin (%d documents in index)" % (writer.numDocs())
print >> sys.stderr, "About to optimize index of %d documents..." % writer.numDocs()
writer.optimize()
print >> sys.stderr, "...done optimizing index of %d documents" % writer.numDocs()
print >> sys.stderr, "Closing index of %d documents..." % writer.numDocs()
writer.close()
#print >> sys.stderr, "...done closing index of %d documents" % writer.numDocs()
# Map reduce output path (cliques) -> local file
# indexer initialize (as given in indexer)
#with open(map_reduce_op, 'r') as inf:
# for line in inf:
# add line to lucene index
# Lucene index directory should be a global
if __name__ == '__main__':
app.run()