You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

11 KiB

In [2]:
import random
import nltk
from newsapi import NewsApiClient
import pprint

pp  = pprint.PrettyPrinter(indent=3)
In [3]:
newsapi = NewsApiClient(api_key='0c00356f65df431ab394d179292075bd')
top0 = newsapi.get_everything(q='translation', language='en') #get json from NewsAPI
top1 = newsapi.get_everything(q='futuro', language='it')
top2 = newsapi.get_everything(q='futuro', language='es')
top3 = newsapi.get_everything(q='future', language='fr')

#pp.pprint(top0)
In [82]:
articles0 = top0['articles'] #get articles summary from NewsAPI
articles1 = top1['articles']
articles2 = top2['articles']
articles3 = top3['articles']

dtot0 = ''
dtot1 = ''
dtot2 = ''
dtot3 = ''

for x in range(20):
    a0 = articles0[x] #get articles' descriptions and store them to dtot
    a1 = articles1[x]
    a2 = articles2[x]
    a3 = articles3[x]
    d0 = a0['description']
    d1 = a1['description']
    d2 = a2['description']
    d3 = a3['description']
    dtot0 += d0
    dtot1 += d1
    dtot2 += d2
    dtot3 += d3
    
dtot0 = dtot0.split()
dtot1 = dtot1.split()
dtot2 = dtot2.split()
dtot3 = dtot3.split()

tagged0 = nltk.pos_tag(dtot0) #POSing the descriptions
tagged1 = nltk.pos_tag(dtot1) #POSing the descriptions
tagged2 = nltk.pos_tag(dtot2) #POSing the descriptions
tagged3 = nltk.pos_tag(dtot3) #POSing the descriptions

dictio = {} #append stuff in the dictionary

for x in range(745):
    if tagged0[x][1] not in dictio:
        dictio.update({tagged0[x][1]:[tagged0[x][0]]})
    elif tagged0[x][1] in dictio.keys():
        dictio[tagged0[x][1]].append(tagged0[x][0])     
In [132]:
#This is for prepare a grammar constructio based on a picked random description from the original NewsAPI json
s = ' '
r = random.randrange(0,19)
a_pos = articles0[r]
cont_pos= a_pos['description']
cont_pos = cont_pos.split()
tag_cont = nltk.pos_tag(cont_pos)

dat = {}

for word, tag in tag_cont:
    dat[tag] = word
    
keys = dat.keys()

output = [pos for pos in keys]

printing = ""
for x in range(len(output)):
    printing += f'''random.choice(dictio['{output[x]}'])  + s + '''
In [133]:
printing
Out[133]:
"random.choice(dictio['NNP'])  + s + random.choice(dictio['VBZ'])  + s + random.choice(dictio['VBG'])  + s + random.choice(dictio['RP'])  + s + random.choice(dictio['PRP$'])  + s + random.choice(dictio['NN'])  + s + random.choice(dictio['TO'])  + s + random.choice(dictio['JJR'])  + s + random.choice(dictio['NNS'])  + s + random.choice(dictio['CD'])  + s + random.choice(dictio['IN'])  + s + random.choice(dictio['CC'])  + s + random.choice(dictio['VBP'])  + s + random.choice(dictio['VBN'])  + s + random.choice(dictio['JJ'])  + s + random.choice(dictio['DT'])  + s + random.choice(dictio['VBD'])  + s + random.choice(dictio['RBR'])  + s + random.choice(dictio['WRB'])  + s + "
In [273]:
 
In [137]:
random.choice(dictio['NNP'])  + s + random.choice(dictio['VBZ'])  + s + random.choice(dictio['VBG'])  + s + random.choice(dictio['RP'])  + s + random.choice(dictio['PRP$'])  + s + random.choice(dictio['NN'])  + s + random.choice(dictio['TO'])  + s + random.choice(dictio['JJR'])  + s + random.choice(dictio['NNS'])  + s + random.choice(dictio['CD'])  + s + random.choice(dictio['IN'])  + s + random.choice(dictio['CC'])  + s + random.choice(dictio['VBP'])  + s + random.choice(dictio['VBN'])  + s + random.choice(dictio['JJ'])  + s + random.choice(dictio['DT'])  + s + random.choice(dictio['VBD'])  + s + random.choice(dictio['RBR'])  + s + random.choice(dictio['WRB'])
Out[137]:
'C…"Hope is According out your voice to More systems 100 that and make designed nice a revealed earlier how'
In [136]:
####################################################################################################################################################################################
In [293]:
res = ['The cable who is of technologies are going to be new Read also and','''no world's who tells from rules are figuring to use near Broomstick, ahead or''',
'the mechanism who seeks as owners are figuring to control new Science, again and',
'the presidency, who has in works carry flying to build hard Harry ever and',
'a repository. who PlantsPhysicists about microswimmers deliver... going to act it. Harry likely But',
'the lecture who PlantsPhysicists of issues deliver... flailing to be different Fluora, up and',
'a cable who has in poets think averting to be electric CEO half-jokingly and',
'the male who fits of submissions are helping to use fellow Texas half-jokingly and',
'the toy who is of hed its helping to get electric Black much and',
'a unease who represents orgasm. sensors think averting to act free PS5 also and',
'the more...Jennifer who fits of reveals believe offering to streaming. major Fluora, actually and',
'the sanitizer who represents in reorganizations are figuring to perform adjustable Tech, likely or']
In [294]:
res
Out[294]:
['The cable who is of technologies are going to be new Read also and',
 "no world's who tells from rules are figuring to use near Broomstick, ahead or",
 'the mechanism who seeks as owners are figuring to control new Science, again and',
 'the presidency, who has in works carry flying to build hard Harry ever and',
 'a repository. who PlantsPhysicists about microswimmers deliver... going to act it. Harry likely But',
 'the lecture who PlantsPhysicists of issues deliver... flailing to be different Fluora, up and',
 'a cable who has in poets think averting to be electric CEO half-jokingly and',
 'the male who fits of submissions are helping to use fellow Texas half-jokingly and',
 'the toy who is of hed its helping to get electric Black much and',
 'a unease who represents orgasm. sensors think averting to act free PS5 also and',
 'the more...Jennifer who fits of reveals believe offering to streaming. major Fluora, actually and',
 'the sanitizer who represents in reorganizations are figuring to perform adjustable Tech, likely or']
In [447]:
export = 'news.txt'
with open(export, 'w') as export:
    print('<head>harset=utf-8</head>')
    print('<h1>News from the future</h1>', file=export)
    print('<br><br><br><br><br><br><br><br><br><br><br><br>', file = export)
    for x in range(len(res)):
        print(f'''{res[x].lower().capitalize()}.
        ''',file=export)
        print('<br><br><br><br><br><br><br><br><br><br><br><br><br><br>', file = export)
<head>harset=utf-8</head>
In [448]:
!pandoc news.txt | weasyprint -s css.css  - newsfromthefuture.pdf
Fontconfig warning: ignoring UTF-8: not a valid region tag
In [ ]:
 
In [411]:
a_pos = open('language.txt').read()
cont_pos = a_pos.split()
tag_cont = nltk.pos_tag(cont_pos)
In [321]:
dat = {}

for word, tag in tag_cont:
    dat[tag] = word
    
keys = dat.keys()

output = " + s + ".join([pos for pos in keys])
In [330]:
 
Out[330]:
'Led and broom believe likely American of can test has called sensors the which There smallest 300 selected They who They to'
In [ ]: