# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
"""
![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTs2mQUEvVkSBa0j1z0LHkM6taXr-9BDXiMLA&usqp=CAU)home.hellodriven.com
"""
!pip install -U spacy
!python -m spacy download en_core_web_lg
!python -m spacy download en_core_web_sm
!pip install wordcloud
import spacy
nlp = spacy.load('en_core_web_sm')
#Code by Paul Mooney

friston_file = '../input/open-access-karl-fristons-papers-txt/23133414.txt'
with open(friston_file) as f: # The with keyword automatically closes the file when you are done
    print (f.read(3000))
import spacy
nlp = spacy.load("en_core_web_sm")

doc = nlp("So, why are you reading this paper?")
for token in doc:
    print(token.text, token.pos_, token.dep_)
# Create a nlp object
doc = nlp("The answer is fairly simple: you are compelled to selectively sample sensory input that conforms to your predictions")
nlp.pipe_names
nlp.disable_pipes('tagger', 'parser')
nlp.pipe_names
"""
#Tokenization
"""
import spacy

nlp = spacy.load('en_core_web_sm')
doc = nlp("in which behavior is cast as active Bayesian inference that is constrained by prior beliefs")
for token in doc:
    print(token.text)
"""
#Part-Of-Speech (POS) Tagging
"""
nlp = spacy.load('en_core_web_sm')

# Create an nlp object
doc = nlp("If behavior is optimal, then it maximizes value")
 
# Iterate over the tokens
for token in doc:
    # Print the token and its part-of-speech tag
    print(token, token.tag_, token.pos_, spacy.explain(token.tag_))
from spacy import displacy

doc = nlp("The same objective: to maximize the Bayesian model evidence averaged over time.")
displacy.render(doc, style="dep" , jupyter=True)
nlp = spacy.load('en_core_web_sm')

# Create an nlp object
doc = nlp("The principle of least action, the principle of maximum entropy, the principle of minimum redundancy and the principle of maximum information transfer.")
 
# Iterate over the tokens
for token in doc:
    # Print the token and its part-of-speech tag
    print(token.text, "-->", token.dep_)
spacy.explain("nsubj"), spacy.explain("ROOT"), spacy.explain("aux"),spacy.explain('nmod'), spacy.explain("advcl"), spacy.explain("dobj")
"""
#Lemmatization
"""
nlp = spacy.load('en_core_web_sm')

# Create an nlp object
doc = nlp("Cost functions that are used to guide action in optimal control can be absorbed into prior beliefs in active inference.")
 
# Iterate over the tokens
for token in doc:
    # Print the token and its part-of-speech tag
    print(token.text, "-->", token.lemma_)
"""
#Sentence Boundary Detection (SBD)
"""
nlp = spacy.load('en_core_web_sm')

# Create an nlp object
doc = nlp("Solutions based upon cost or reward functions that are an integral part of optimal control theory and reinforcement learning")
 
sentences = list(doc.sents)
len(sentences)
for sentence in sentences:
     print (sentence)
"""
#Named Entity Recognition (NER)
"""
nlp = spacy.load("en_core_web_sm")
doc = nlp("Solution to this variational problem is given by a stochastic controller called the Bayesian control rule, which implements adaptive behavior as a mixture of experts.")
#See the entity present
print(doc.ents)
for ent in doc.ents:
    print(ent.text, ent.start_char, ent.end_char, ent.label_)
"""
#Entity Detection
"""
from spacy import displacy
nlp = spacy.load("en_core_web_sm")
doc= nlp(u"""This work
illustrates the close connections between minimizing (relative)
entropy and the ensuing active Bayesian inference that we will appeal
to the later.In summary, current approaches to partially observed MDPs
and stochastic optimal control minimize cumulative cost using the same
procedures employed by maximum likelihood and approximate Bayesian
inference schemes. Indeed, the formal equivalence between optimal
control and estimation was acknowledged by Kalman at the inception of
Bayesian filtering schemes. """)

entities=[(i, i.label_, i.label) for i in doc.ents]
entities
displacy.render(doc, style = "ent",jupyter = True)
nlp = spacy.load("en_core_web_lg")
tokens = nlp("Relationship between entropy, surprise and Bayesian model evidence in Section “Bayes-optimal control without cost functions,” The motivation for free energy minimization in more detail.")

for token in tokens:
    print(token.text, token.has_vector, token.vector_norm, token.is_oov)
nlp = spacy.load("en_core_web_lg")  # make sure to use larger model!
tokens = nlp("Free energy principle states that the sufficient statistics of the conditional probability and action minimize free energy The first equality in Equation expresses free energy as a Gibbs energy (expected under the conditional distribution) minus the entropy of the conditional distribution.")

for token1 in tokens:
    for token2 in tokens:
        print(token1.text, token2.text, token1.similarity(token2))
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
nlp = spacy.load("en_core_web_lg")  # make sure to use larger model!
tokens = nlp("Free utility is fundamentally different from variational free energy,it is a functional of choice probabilities over hidden states. Variational free energy is a function of observed states. Crucially, free utility depends on a cost function, while free energy does not. The free energy principle is based on the invariant or ergodic solution 2012. In other words, value is (log) evidence or negative surprise.")

newText =''
for word in tokens:
 if word.pos_ in ['ADJ', 'NOUN']:
  newText = " ".join((newText, word.text.lower()))

wordcloud = WordCloud(stopwords=STOPWORDS, colormap='Reds', background_color="blue").generate(newText)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
"""
![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTvuNlT2OyjZ9vcUoNgz_20HfnL6tszy18lXw&usqp=CAU)youtube.com
"""