test_elasticsearch.ipynb
4.46 KB
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
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# based on: http://blog.tryolabs.com/2015/02/17/python-elasticsearch-first-steps/\n",
"\n",
"import requests\n",
"res = requests.get('http://localhost:9200')\n",
"print(res.content)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from elasticsearch import Elasticsearch\n",
"es = Elasticsearch([{'host': 'localhost', 'port': 9200}])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import codecs\n",
"import json\n",
"import logging\n",
"import sys\n",
"from util import ngrams\n",
"from collections import defaultdict\n",
"from filter import city_filter\n",
"from twitter.Tweet import Tweet\n",
"\n",
"# load cityNamesDict\n",
"cityNamesDict = city_filter.normalizeCityNames()\n",
"cities15000 = city_filter.loadCities15000(filename=\"resources/cities15000.txt\")\n",
"print len(cities15000)\n",
"\n",
"# input file\n",
"inputFile = \"../../../english-tweets-20151101.json.gz\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def wordcountPlain(tweet, onlyHashtags=False, ngram=1):\n",
" tweetText = tweet['text']\n",
" tokens = Tweet.tokenizeTweetText(tweetText)\n",
" \n",
" ngramsList = list()\n",
" tokenList = [t for t in tokens if (len(t) > 2 and (not ngrams.is_url_or_mention(t)))]\n",
" \n",
" if ngram > 1:\n",
" ngramsList = ngramsList + tokenList\n",
" for ng in range(1, ngram):\n",
" ngramsList = ngramsList + [ntoken for ntoken in ngrams.window(tokenList, ng + 1)]\n",
" # tokenList = [ntoken for ntoken in ngrams.window(tokenList, ng + 1)]\n",
" return ngramsList\n",
" else:\n",
" if onlyHashtags:\n",
" for token in tokenList: # len(token) > 2\n",
" if token.startswith('#'):\n",
" ngramsList.append(token)\n",
" return ngramsList\n",
" else:\n",
" return tokenList"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"tweetsAsDict = Tweet.getTweetAsDictionaryFromGZ(inputFile)\n",
"i = 0\n",
"# try:\n",
"for tweet in tweetsAsDict:\n",
" #i += 1\n",
" # get US city\n",
" city = city_filter.get_US_City(tweet, cityNamesDict, cities15000)\n",
" if city:\n",
" i += 1\n",
" ngramsList = wordcountPlain(tweet, True, 1)\n",
" tweet['city'] = city \n",
" tweet['ngrams'] = ngramsList\n",
" if len(ngramsList)>0: print i\n",
" es.index(index='100tweets', doc_type='tweets+', id=i, body=tweet)\n",
" print city, tweet['text'], ngramsList\n",
" if i%100==0:\n",
" break\n",
"# except:\n",
"# print \"looser!\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"es.get(index='100tweets', doc_type='tweets+', id=14)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"es.search(index=\"100tweets\", body={\"query\": {\"match\": {'text':'Thank you'}}})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"r = requests.get('http://localhost:9200')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2.0
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}