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.
12 KiB
12 KiB
In [51]:
import random import nltk from newsapi import NewsApiClient import pprint pp = pprint.PrettyPrinter(indent=3)
In [53]:
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 [256]:
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 #HERE THE WTF #HOW CAN I APPEND TO THE DICTIONARY (i0) _ALL_ THE WORDS? IT APPENDS ONLY THE FIRSTS ONES #tried in different ways but :( t0 = [] i0 = {} for a, b in tagged0: if a not in b: i0.update({b:[a]}) for q, k in tagged0: if k not in i0: t0.append((k, s)) i0[k] = '[]' print(i0)
{'NNP': ['Appl…'], 'NN': ['story:'], 'VBZ': ['is'], 'VBN': ['been'], 'JJ': ['big'], 'NNS': ['employees'], 'IN': ['for'], 'CC': ['and'], 'JJR': ['more'], 'VBG': ['closing'], 'TO': ['to'], 'VB': ['fight'], 'DT': ['The'], 'PRP': ['it'], 'MD': ['can'], 'WDT': ['which'], 'VBD': ['used'], 'PRP$': ['your'], 'CD': ['2020.'], 'RB': ['embarrassingly'], 'RP': ['out'], 'VBP': ['come'], 'RBR': ['earlier'], 'WRB': ['when'], 'PDT': ['all'], 'WP': ['who'], 'EX': ['There'], 'NNPS': ['Republicans']}
In [270]:
d1 = '' for a,b in tagged0: d1 = {b : [a] for a,b in tagged0} if a not in b: d1.update({b:[a]}) d1
Out[270]:
{'NNP': ['Appl…'], 'NN': ['story:'], 'VBZ': ['is'], 'VBN': ['been'], 'JJ': ['big'], 'NNS': ['employees'], 'IN': ['for'], 'CC': ['and'], 'JJR': ['more'], 'VBG': ['closing'], 'TO': ['to'], 'VB': ['fight'], 'DT': ['The'], 'PRP': ['it'], 'MD': ['can'], 'WDT': ['which'], 'VBD': ['used'], 'PRP$': ['your'], 'CD': ['2020.'], 'RB': ['embarrassingly'], 'RP': ['out'], 'VBP': ['come'], 'RBR': ['earlier'], 'WRB': ['when'], 'PDT': ['all'], 'WP': ['who'], 'EX': ['There'], 'NNPS': ['Republicans']}
In [ ]:
In [ ]:
In [ ]:
In [271]:
#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 = articles[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 = " + s + ".join([pos for pos in keys])
In [272]:
output
Out[272]:
'NNP + s + NN + s + VBZ + s + VBN + s + JJ + s + NNS + s + IN + s + CC + s + JJR + s + VBG + s + TO + s + VB + s + DT + s + PRP + s + MD'
In [273]:
####################################################################################################################################################################################
In [ ]:
In [ ]:
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 he’d it’s 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 he’d it’s 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 [ ]: