-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathltr-with-bees-demo.sh
executable file
·190 lines (146 loc) · 6.51 KB
/
ltr-with-bees-demo.sh
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
echo_f()
{
echo "================================================================================"
echo "DEMO: ${1}"
echo "================================================================================"
sleep 2
}
if [[ -z "$API_TWITTER_ACCESS_TOKEN" ]]
then
echo_f "Obtaining Twitter Access Token for API_TWITTER_CONSUMER_KEY/API_TWITTER_CONSUMER_SECRET environment variables"
if [[ -n "$API_TWITTER_CONSUMER_KEY" && -n "$API_TWITTER_CONSUMER_SECRET" ]]
then
./ltr-with-bees.py twitter-api-login "$API_TWITTER_CONSUMER_KEY" "$API_TWITTER_CONSUMER_SECRET"
exit $?
else
echo "ERROR: Please set and export API_TWITTER_CONSUMER_KEY and API_TWITTER_CONSUMER_SECRET environment variables."
exit 1
fi
else
echo_f "Using Twitter Access Token from API_TWITTER_ACCESS_TOKEN environment variable"
fi
delete_create_skip_collection=
for collection in tweets tweet_features tweet_clicks
do
if [[ "$collection" == "$delete_create_skip_collection" ]]
then
continue
fi
echo_f "Deleting ${collection} collection (if it still exists)"
solr-7.0.0/bin/solr delete -c ${collection}
done
echo_f "Stopping solr (if it's still running)"
solr-7.0.0/bin/solr stop
echo_f "Starting solr"
solr-7.0.0/bin/solr start
for collection in tweets tweet_features tweet_clicks
do
if [[ "$collection" == "$delete_create_skip_collection" ]]
then
continue
fi
echo_f "Creating ${collection} collection using configs/${collection}_config config"
solr-7.0.0/bin/solr create_core -c ${collection} -d configs/${collection}_config
done
echo_f "Indexing demo tweets into Solr"
./ltr-with-bees.py index-tweets-by-id
echo_f "Gathering external file field values"
./ltr-with-bees.py refresh-user-data
echo_f "Updating external file field values"
for field in followers_count following_count
do
dst="solr-7.0.0/server/solr/tweets/data/"
for file in `\ls -t external/external_${field}.* | head -1`
do
echo "Copying $file to $dst"
cp $file $dst/
done
done
echo_f "Restarting solr (simpler to demo than ExternalFileFieldReloader config and use)"
solr-7.0.0/bin/solr restart
echo_f "TODO: 'sleep 5' should not be needed here?"
sleep 5
echo "Slept for 5 seconds."
for field in followers_count following_count
do
echo_f "Retrieving updated ${field} values"
curl --silent "http://localhost:8983/solr/tweets/select?q=*:*&fl=handle,field(${field})"
done
for candidateFeatureFile in `\ls features/*.json`
do
echo_f "Uploading candidate feature ($candidateFeatureFile)"
curl --silent -XPUT 'http://localhost:8983/solr/tweets/schema/feature-store' --data-binary "@$candidateFeatureFile" -H 'Content-type:application/json'
done
echo_f "Running sample feature extraction"
curl --silent "http://localhost:8983/solr/tweets/select?q=*:*&fl=source,tweet,\[fv\]&rows=100"
search_and_click_f()
{
user="$1" ; shift
word="$1" ; shift
clicked_result="$1" ; shift
if [[ "$user" == "Bianca" ]]
then
efi_option="--efi-from-desktop=True"
fi
if [[ "$user" == "Harry" ]]
then
efi_option="--efi-from-mobile=True"
fi
export SOLR_QUERY_ID="queryId_$(date -u '+%Y%m%d_%H%M%S')"
sleep 1 # hack: sleep to ensure demo query id uniqueness
echo_f "User '$user' is searching for tweets with the word '$word' using '$SOLR_QUERY_ID'"
./ltr-with-bees.py solr-search-and-log-features tweet:$word $efi_option
echo_f "Logging click for result $clicked_result"
./ltr-with-bees.py solr-log-click $SOLR_QUERY_ID $clicked_result
}
BEE_STING_CAKE_RESULT_ID=827139009277227008
RESULT_ID_STING_IN_THE_TALE_RESULT_ID=334232443563433985
ARCTIC_BUMBLE_BEE_RESULT_ID=784468395420946432
MILES_PER_HONEY_RESULT_ID=777947618311860224
BLUEBERRY_BEE_RESULT_ID=568450251612233728
BLUEBERRY_TART_RESULT_ID=761328145995751425
search_and_click_f Bianca sting $BEE_STING_CAKE_RESULT_ID
search_and_click_f Harry sting $BEE_STING_CAKE_RESULT_ID
search_and_click_f Bianca miles $ARCTIC_BUMBLE_BEE_RESULT_ID
search_and_click_f Harry miles $MILES_PER_HONEY_RESULT_ID
search_and_click_f Bianca blueberry $BLUEBERRY_BEE_RESULT_ID
search_and_click_f Harry blueberry $BLUEBERRY_TART_RESULT_ID
search_and_rerank_f()
{
model="$1"
for word in sting miles blueberry
do
for efi_from in desktop mobile
do
echo_f "model=$model efi.from_${efi_from}=1 tweet:${word}"
curl --silent "http://localhost:8983/solr/tweets/select?q=tweet:${word}&fl=tweet,id,score&rq=\{!ltr+model=$model+efi.from_${efi_from}=1\}"
done
done
for efi_from in desktop mobile
do
echo_f "model=$model efi.from_${efi_from}=1 tweet:* fq=-tweet:sting fq=-tweet:miles fq=-tweet:blueberry"
curl --silent "http://localhost:8983/solr/tweets/select?q=tweet:*&fl=tweet,id,score&rq=\{!ltr+model=$model+efi.from_${efi_from}=1\}&fq=-tweet:sting&fq=-tweet:miles&fq=-tweet:blueberry"
done
}
echo_f "Training model ($LINEAR_MODEL_NAME)"
LINEAR_FEATURE_NAMES="hashtagCount,honeyContent,fromMobile"
LINEAR_MODEL_NAME="linearModel$(date -u '+%Y%m%d%H%M%S')"
./ltr-with-bees.py train-linear-model --feature-names $LINEAR_FEATURE_NAMES --model-name $LINEAR_MODEL_NAME --model-file-name models/$LINEAR_MODEL_NAME.json --inputs-file-name models/$LINEAR_MODEL_NAME-inputs.txt --outputs-file-name models/$LINEAR_MODEL_NAME-outputs.txt
echo_f "Uploading model ($LINEAR_MODEL_NAME)"
curl --silent -XPUT 'http://localhost:8983/solr/tweets/schema/model-store' --data-binary "@models/$LINEAR_MODEL_NAME.json" -H 'Content-type:application/json'
search_and_rerank_f $LINEAR_MODEL_NAME
echo_f "Training model ($TREES_MODEL_NAME)"
TREES_FEATURE_NAMES="byVerifiedAccount,containsHashtag,fromDesktop,fromMobile,hashtagCount,tweetLength"
TREES_MODEL_NAME="treesModel$(date -u '+%Y%m%d%H%M%S')"
RANKLIB_OPTIONS="--ranklib-tree=1 --ranklib-leaf=4"
./ltr-with-bees.py train-trees-model $RANKLIB_OPTIONS --feature-names $TREES_FEATURE_NAMES --model-name $TREES_MODEL_NAME --model-file-name models/$TREES_MODEL_NAME.json --inputs-file-name models/$TREES_MODEL_NAME-inputs.txt --outputs-file-name models/$TREES_MODEL_NAME-outputs.txt
echo_f "Uploading model ($TREES_MODEL_NAME)"
curl --silent -XPUT 'http://localhost:8983/solr/tweets/schema/model-store' --data-binary "@models/$TREES_MODEL_NAME.json" -H 'Content-type:application/json'
search_and_rerank_f $TREES_MODEL_NAME
echo_f "optional: command for stopping solr"
echo "solr-7.0.0/bin/solr stop"
if [[ -n "$API_TWITTER_ACCESS_TOKEN" ]]
then
echo_f "optional: command to invalidate Twitter Access Token"
echo "./ltr-with-bees.py twitter-api-logout \"\$API_TWITTER_CONSUMER_KEY\" \"\$API_TWITTER_CONSUMER_SECRET\""
fi