-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.visidatarc
326 lines (256 loc) · 8.45 KB
/
.visidatarc
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# Options
options.disp_column_sep = "│"
options.disp_keycol_sep = "║"
options.clean_names = True
options.disp_menu_fmt = ""
options.disp_sidebar = False
options.clipboard_copy_cmd = "pbcopy"
options.clipboard_paste_cmd = "pbpaste"
options.color_default = ""
options.quitguard = True
options.color_key_col = 110
options.color_note_type = 8
options.scroll_incr = -3
options.some_selected_rows = True
options.disp_histogram = "*"
options.undo = True
options.motd_url = ""
options.save_filetype = "jsonl"
options.motd_url=''
# Keybindings
vd.bindkey("0", "go-leftmost")
vd.bindkey("4", "go-rightmost")
vd.unbindkey("Up")
vd.unbindkey("Down")
vd.bindkey("Up", "setcol-precision-more")
vd.bindkey("Down", "setcol-precision-less")
# Toggle numeric binning
Sheet.addCommand("B", "toggle-binning", "options.numeric_binning = not options.numeric_binning")
import regex
import functools
import tldextract
import emoji
import urlexpander
import requests
import datetime
import fugashi
#import ucluster.vd.plugin
import deepl
from urllib.parse import urlparse
from ast import literal_eval
from nltk import word_tokenize
from nltk.util import ngrams
from nltk.corpus import stopwords
from dateutil import tz
from dateutil import parser
from geopy.geocoders import Nominatim
from snownlp import SnowNLP
from lingua import Language, LanguageDetectorBuilder
detector = LanguageDetectorBuilder.from_all_languages().with_preloaded_language_models().build()
translator = deepl.Translator("")
# In case you're running your own instance
geolocator = Nominatim(user_agent="SIO", domain="localhost:8080", scheme="http")
# geolocator = Nominatim(user_agent="SIO")
# Unshorten URLs with unshrtn. If the URL is shortened, return the best
# expanded URL we have, along with a bool saying it was a shortened URL.
# Otherwise, return the original, with bool and placeholders so we can compare
# 1:1 with links that weren't short to begin with.
@functools.lru_cache
def unshrtn(url):
if urlexpander.is_short(url):
resp = requests.get("http://localhost:3000", params={"url": url}).json()
for key in ["canonical", "long", "short"]:
if resp.get(key) is not None:
return (resp[key], True, resp["status"], resp["title"])
return (url, False, None, None)
# Do a cached geocode lookup via Nominatim
@functools.lru_cache
def geo(column):
if column and column != "null":
location = geolocator.geocode(column, timeout=5, language="en")
if location is not None:
latlong = str(location.latitude) + " " + str(location.longitude)
return (latlong, location.address, location.address.split(",")[-1].strip())
# Convert to local time
def ltime(column):
return parser.parse(column).astimezone(tz.tzlocal())
# Strip HTML tags
def nohtml(column):
tag_re = regex.compile(r"(<!--.*?-->|<[^>]*>)")
return tag_re.sub("", column)
# Convert abbreviated numbers like 1.1k to actual numbers
def unk(x):
if x.isdigit():
return int(x)
else:
if len(x) > 1:
if x[-1] == "k":
y = float(x[:-1]) * 1000
elif x[-1] == "m":
y = float(x[:-1]) * 1000000
return int(y)
# Convert emoji to :this: representation, in memory of Chloe Price
def noemoji(column):
if column:
text = emoji.demojize(column)
return text
# Extract all emoji into a list
def gemoji(column):
emoji_list = []
if column:
data = regex.findall(r"\X", column)
for word in data:
if word in emoji.EMOJI_DATA:
emoji_list.append(word)
return emoji_list
# Extract all emoji into a text-based list
def temoji(column):
emoji_list = gemoji(column)
temoji_list = []
for char in emoji_list:
temoji = emoji.demojize(char)
temoji_list.append(temoji)
return temoji_list
def otherchars(column):
if column:
return [c for c in column if regex.match("[^\u0020-\u0370]", c)]
# Twitter helper functions (v1 or v2)
def hashtags(column):
if column:
if column.get("hashtags", []):
try:
return [hashtag["tag"] for hashtag in column["hashtags"]]
except:
return [hashtag["text"] for hashtag in column["hashtags"]]
else:
return None
def mentions(column):
if column:
if column.get("mentions", []):
try:
return [name["username"] for name in column["mentions"]]
except:
return [name["screen_name"] for name in column["user_mentions"]]
else:
return None
def annotations(column):
if column:
try:
return [
(anno["probability"], anno["type"], anno["normalized_text"])
for anno in column["annotations"]
]
except:
return None
def urls(column):
results = []
try:
for url in column["urls"]:
if "unwound_url" in url:
results.append(url["unwound_url"])
elif url.get("expanded_url"):
results.append(url["expanded_url"])
elif url.get("url"):
results.append(url["url"])
return results
except:
return None
def domains(column):
if column:
if type(column) is list:
doms = []
for elem in column:
doms.append(tldextract.extract(elem).registered_domain)
return doms
else:
return tldextract.extract(column).registered_domain
# Sometimes we get data in a CSV or something that's a literal string,
# but with array notation.
def strtoarray(column):
if column:
return literal_eval(column)
# These re- functions are for plain text fields where we don't have
# these as separate metadata/columns
def rementions(column):
if column:
return regex.findall(r"\B@\w\w+", column)
def rehashtags(column):
if column:
return regex.findall(r"\B#\w\w+", column)
def loras(column):
if column:
return regex.findall(r"(<[Ll]ora:.*:???>)", column)
def reurls(column):
r = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
if column:
url = regex.findall(r, column)
return [x[0] for x in url]
else:
return ""
# Normalize URLs for cleaner freq analysis
def normurl(column):
if column:
url = urlparse(column)[1:]
url = "".join(url)
if url.endswith("/"):
url = url[:-1]
if url.startswith("www."):
url = url[4:]
return url
# Detect language and probability
@functools.lru_cache
def ld(column):
if column and len(column) >= 5:
confidence_values = detector.compute_language_confidence_values(column)
return (confidence_values[0].language.iso_code_639_1.name, confidence_values[0].value)
return ""
@functools.lru_cache
def translate(column):
if column and len(column) >= 10:
return str(translator.translate_text(column, target_lang="EN-US"))
return ""
# read in stubs.txt from tmp. If the stub matches the first part of string passed in as column followed by a capital letter, return True.
def stubs(column):
if column:
with open("/tmp/stubs.txt") as f:
stubs = f.read().splitlines()
for stub in stubs:
if column.startswith(stub) and column[len(stub)].isupper():
return True
return False
# Sloppy ngram filter
def grams(column, lang, n):
if column:
stop = stopwords.words(lang)
stop.extend(["http", "https", "RT"])
w = word_tokenize(column)
alnum = [word for word in w if word.isalnum()]
clean = [word for word in alnum if not word in stop]
tokens = ngrams(clean, n)
return [" ".join(thegrams) for thegrams in tokens]
# Chinese language helper to pull out words
@functools.lru_cache
def cnwords(column):
if column:
s = SnowNLP(column)
return s.words
# or sentences
@functools.lru_cache
def cnsent(column):
if column:
s = SnowNLP(column)
return s.sentences
# or keywords
@functools.lru_cache
def cnkw(column, count):
if column:
s = SnowNLP(column)
return s.keywords(count)
# And now for jp
tagger = fugashi.Tagger()
@functools.lru_cache
def jpgrams(column):
if column:
s = [word.surface for word in tagger(column)]
return s
# vim: syntax=python