-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
162 lines (124 loc) · 3.77 KB
/
utils.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
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
import streamlit as st
from habanero import Crossref
import numpy as np
import random
cr = Crossref()
class Article:
def __init__(self, _authors, _title, _date, _abstract=None):
self.authors = _authors
self.title = _title
self.date = _date
self.abstract = _abstract
def parse_authors(crossref_return):
try:
author_list = crossref_return['author']
authors = []
for a in author_list:
authors.append(f"{a['given']}, {a['family']}")
except:
authors = None
return authors
def parse_abstract(crossref_return):
try:
abstract = crossref_return['abstract']
except:
abstract = " "
return abstract
def parse_date(crossref_return):
return crossref_return['created']['date-time']
def parse_title(crossref_return):
try:
title = crossref_return['title'][0]
except:
title = " "
return title
def search_crossref(query):
works = cr.works(query=query, limit=100)['message']['items']
articles = []
for w in works:
authors = parse_authors(w)
title = parse_title(w)
abstract = parse_abstract(w)
date = parse_date(w)
articles.append(Article(authors, title, date, abstract))
return articles
def make_pairs(corpus):
for i in range(len(corpus)-1):
yield (corpus[i], corpus[i+1])
def get_corpus(article_list):
strings = []
titles = []
for a in article_list:
if a.title != " ":
strings.append(a.title.lower())
titles.append(a.title.lower())
if a.abstract != " ":
strings.append(a.abstract.lower())
corpus = " ".join(strings)
corpus_titles = " ".join(titles)
#vec = CountVectorizer()
#X = vec.fit_transform(strings)
return corpus, corpus_titles
def generate_poem(search_string, length):
corpus, titles = get_corpus(search_crossref(search_string))
corpus = corpus.split()
titles = titles.split()
title_pairs = make_pairs(titles)
title_fw = np.random.choice(titles)
pairs = make_pairs(corpus)
first_word = np.random.choice(corpus)
word_dict = {}
for word_1, word_2 in pairs:
if word_1 in word_dict.keys():
word_dict[word_1].append(word_2)
else:
word_dict[word_1] = [word_2]
title_dict = {}
for t_word_1, t_word_2 in title_pairs:
if t_word_1 in title_dict.keys():
title_dict[t_word_1].append(t_word_2)
else:
title_dict[t_word_1] = [t_word_2]
title_chain = [title_fw]
title_len = np.random.choice(list(range(2,6)))
for i in range(title_len):
choice = np.random.choice(title_dict[title_chain[-1]])
if i == title_len-1:
if choice in ["and", "the", "or", "of", "a", "is"]:
i -=1
else:
title_chain.append(choice)
else:
title_chain.append(choice)
#title_chain.append()
chain = [first_word]
n_words = length
for i in range(n_words):
if i == n_words-1:
choice = np.random.choice(word_dict[chain[-1]])
if choice in ["and", "the", "or", "of", "a", "is"]:
i -=1
else:
chain.append(np.random.choice(word_dict[chain[-1]]))
else:
chain.append(np.random.choice(word_dict[chain[-1]]))
poem = ' '.join(jitter_poem_string(chain))
title = ' '.join(title_chain)
return poem, title
from random import random
def jitter_poem_string(chain):
poem = []
space = "\t"
newline = "\n "
for i in range(0, len(chain)):
# whitespace = randint(1, 10)
# linebreaks = randint(0, 3)
which_one = random()
poem.append(chain[i])
if which_one >= 0.34 and which_one <= 0.66:
for n in range(np.random.randint(0,3)):
poem.append(space)
elif which_one >= 0.77:
for n in range(np.random.randint(1,2)):
poem.append(newline)
return poem