-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwordcloud.py
More file actions
29 lines (23 loc) · 1.11 KB
/
wordcloud.py
File metadata and controls
29 lines (23 loc) · 1.11 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
from collections import Counter
from typing import Iterable, Dict
from addcorpus.models import CorpusConfiguration, Field
from visualization.termvectors import request_termvectors_batched, term_counts
from es import download as download
from es.client import elasticsearch
from es import download as download
def _wordcloud_search_field(corpus_name: str, field_name: str) -> bool:
corpus_config = CorpusConfiguration.objects.get(corpus__name=corpus_name)
field: Field = corpus_config.fields.get(name=field_name)
has_clean_field = 'clean' in field.es_mapping.get('fields', {})
if has_clean_field:
return field_name + '.clean'
return field_name
def make_wordcloud_data(hits: Iterable[Dict], field_name, corpus_name):
search_field = _wordcloud_search_field(corpus_name, field_name)
counts = Counter()
client = elasticsearch(corpus_name)
docs = request_termvectors_batched(hits, client, False, [search_field])
for _, doc in docs:
counts.update(term_counts(doc, search_field))
output = [{'key': word, 'doc_count': freq} for word, freq in counts.items()]
return output