Inteligencia Artificial
Lenguaje Python: Procesamiento del Lenguaje Natural.
Universidad Nacional de Ingeniería
FACULTAD DE CIENCIAS Y SISTEMAS – DEPARTAMENTO DE INFORMÁTICA
Procesamiento del Lenguaje Natural con Python.
Recopilación de datos de archivos PDF.
!pip install PyPDF2
import PyPDF2
from PyPDF2 import PdfFileReader
pdf = open("file.pdf","rb")
pdf_reader = PyPDF2.PdfFileReader(pdf)
print(pdf_reader.numPages)
page = pdf_reader.getPage(0)
print(page.extractText())
pdf.close()
Recopilación de datos de archivos Word.
!pip install docx
from docx import Document
doc = open("file.docx","rb")
document = docx.Document(doc)
docu=""
for para in document.paragraphs:
docu += para.text
print(docu)
Recopilación de datos de archivos HTML.
!pip install bs4
import urllib.request as urllib2
from bs4 import BeautifulSoup
response = urllib2.urlopen('https://en.wikipedia.org/wiki/Natural_language_processing')
html_doc = response.read()
soup = BeautifulSoup(html_doc, 'html.parser')
strhtm = soup.prettify()
print (strhtm[:1000])
print(soup.title)
print(soup.title.string)
print(soup.a.string)
print(soup.b.string)
# Extrayendo todas las instancias de una etiqueta particular
for x in soup.find_all('a'): print(x.string)
# Extrayendo todo el texto de una etiqueta particular
for x in soup.find_all('p'): print(x.text)
Arañando texto desde la web.
Antes de “escarbar o arañar” cualquier sitio web, blog o comercio electrónico sitios web, asegúrese
de leer los términos y condiciones del sitio web sobre si otorga permisos para escarbar sus datos.
!pip install bs4
!pip install requests
from bs4 import BeautifulSoup
import requests
import pandas as pd
from pandas import Series, DataFrame
from ipywidgets import FloatProgress
from time import sleep
from IPython.display import display
import re
import pickle
url = 'http://www.imdb.com/chart/top?ref_=nv_mv_250_6'
result = requests.get(url)
c = result.content
soup = BeautifulSoup(c,"lxml")'
summary = soup.find('div',{'class':'article'})
# Creando listas vacías para agregar los datos extraídos.
moviename = []
cast = []
description = []
rating = []
ratingoutof = []
year = []
genre = []
movielength = []
rot_audscore = []
rot_avgrating = []
rot_users = []
# Extrayendo los datos requeridos del soup html.
rgx = re.compile('[%s]' % '()')
f = FloatProgress(min=0, max=250)
display(f)
for row,i in zip(summary.find('table').
findAll('tr'),range(len(summary.find('table').findAll('tr')))):
for sitem in row.findAll('span',{'class':'secondaryInfo'}):
s = sitem.find(text=True)
year.append(rgx.sub(", s))
for ritem in row.findAll('td',{'class':'ratingColumn imdbRating'}):
for iget in ritem.findAll('strong'):
rating.append(iget.find(text=True))
ratingoutof.append(iget.get('title').split(' ', 4)[3])
for item in row.findAll('td',{'class':'titleColumn'}):
for href in item.findAll('a',href=True):
moviename.append(href.find(text=True))
rurl = 'https://www.rottentomatoes.com/m/'+ href.
find(text=True)
try:
rresult = requests.get(rurl)
except requests.exceptions.ConnectionError:
status_code = "Connection refused"
rc = rresult.content
rsoup = BeautifulSoup(rc)
try:
rot_audscore.append(rsoup.find('div',
{'class':'meter-value'}).find('span',
{'class':'superPageFontColor'}).text)
rot_avgrating.append(rsoup.find('div',
{'class':'audience-info hidden-xs
superPageFontColor'}).find('div').contents[2].
strip())
rot_users.append(rsoup.find('div',
{'class':'audience-info hidden-xs
superPageFontColor'}).contents[3].contents[2].
strip())
except AttributeError:
rot_audscore.append("")
rot_avgrating.append("")
rot_users.append("")
cast.append(href.get('title'))
imdb = "http://www.imdb.com" + href.get('href')
try:
iresult = requests.get(imdb)
ic = iresult.content
isoup = BeautifulSoup(ic)
description.append(isoup.find('div',
{'class':'summary_text'}).find(text=True).strip())
genre.append(isoup.find('span',{'class':'itempr
op'}).find(text=True))
movielength.append(isoup.find('time',
{'itemprop':'duration'}).find(text=True).strip())
except requests.exceptions.ConnectionError:
description.append("")
genre.append("")
movielength.append("")
sleep(.1)
f.value = i
# Listar a pandas series
moviename = Series(moviename)
cast = Series(cast)
description = Series(description)
rating = Series(rating)
ratingoutof = Series(ratingoutof)
year = Series(year)
genre = Series(genre)
movielength = Series(movielength)
rot_audscore = Series(rot_audscore)
rot_avgrating = Series(rot_avgrating)
rot_users = Series(rot_users)
# creando un dataframe y analizándolo
imdb_df = pd.concat([moviename,year,description,genre,
movielength,cast,rating,ratingoutof,
rot_audscore,rot_avgrating,rot_users],axis=1)
imdb_df.columns = [ 'moviename','year','description','genre',
'movielength','cast','imdb_rating',
'imdb_ratingbasedon','tomatoes_audscore',
'tomatoes_rating','tomatoes_ratingbasedon']
imdb_df['rank'] = imdb_df.index + 1
imdb_df.head(1)
# Guardando el archivo como CSV.
imdb_df.to_csv("imdbdataexport.csv")
Generando N-gramas usando TextBlob.
Text = "Estoy aprendiendo PNL"
#Importando textblob
from textblob import TextBlob
#Para unigram : Use n = 1
TextBlob(Text).ngrams(1)
TextBlob(Text).ngrams(2)
Extraer frases nominales.
#Importando nltk
import nltk
from textblob import TextBlob
#Extrayendo elementos
blob = TextBlob("John está aprendiendo el procesamiento del lenguaje natural")
for np in blob.noun_phrases:
print(np)
Encontrar similitudes entre textos
documents = ("Me gusta la PNL",
"Estoy explorando PNL",
"Soy un principiante en PNL",
"Quiero aprender PNL",
"Me gusta la PNL avanzada")
#Importando bibliotecas
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
#Compute tfidf: método de ingeniería de características llamado matriz de coincidencia.
tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform(documents)
tfidf_matrix.shape
# Salida
# Calcular similitud para la primera oración con el resto de las oraciones
cosine_similarity(tfidf_matrix[0:1],tfidf_matrix)
# Salida
Correspondencia fonética
!pip install fuzzy
import fuzzy
soundex = fuzzy.Soundex(4)
soundex('natural')
#salida
'N364'
soundex('natuaral')
104
#salida
'N364'
soundex('language')
#salida
'L52'
soundex('processing')
#salida
'P625'
Etiquetar partes del lenguaje.
Text = "I love NLP and I will learn NLP in 2 month"
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize, sent_tokenize
stop_words = set(stopwords.words('english'))
# Tokenizar el texto
tokens = sent_tokenize(text)
#Genera etiquetado para todos los tokens usando loop
for i in tokens:
words = nltk.word_tokenize(i)
words = [w for w in words if not w in stop_words]
# Etiquetador.
tags = nltk.pos_tag(words)
tags
#salida:
[('I', 'PRP'),
('love', 'VBP'),
('NLP', 'NNP'),
('I', 'PRP'),
('learn', 'VBP'),
('NLP', 'RB'),
('2month', 'CD')]
• CC coordinating conjunction.
• CD cardinal digit.
• DT determiner.
• EX existential there (like: “there is” ... think of it like
• “there exists”.)
• FW foreign word.
• IN preposition/subordinating conjunction.
• JJ adjective ‘big’.
• JJR adjective, comparative ‘bigger’.
• JJS adjective, superlative ‘biggest’.
• LS list marker 1.)
• MD modal could, will.
• NN noun, singular ‘desk’.
• NNS noun plural ‘desks’.
• NNP proper noun, singular ‘Harrison’.
• NNPS proper noun, plural ‘Americans’.
• PDT predeterminer ‘all the kids’.
• POS possessive ending parent’s.
• PRP personal pronoun I, he, she.
• PRP$ possessive pronoun my, his, hers.
• RB adverb very, silently.
• RBR adverb, comparative better.
• RBS adverb, superlative best.
• RP particle give up.
• TO to go ‘to’ the store.
• UH interjection.
• VB verb, base form take.
• VBD verb, past tense took.
• VBG verb, gerund/present participle taking.
• VBN verb, past participle taken.
• VBP verb, sing. present, non-3d take.
• VBZ verb, 3rd person sing. present takes.
• WDT wh-determiner which.
• WP wh-pronoun who, what.
• WP$ possessive wh-pronoun whose.
• WRB wh-adverb where, when.
Extraer entidades del texto.
sent = "John is studying at Stanford University in California"
import nltk
from nltk import ne_chunk
from nltk import word_tokenize
ne_chunk(nltk.pos_tag(word_tokenize(sent)), binary=False)
#salida
Tree('S', [Tree('PERSON', [('John', 'NNP')]), ('is', 'VBZ'),
('studying', 'VBG'), ('at', 'IN'), Tree('ORGANIZATION',
[('Stanford', 'NNP'), ('University', 'NNP')]), ('in', 'IN'),
Tree('GPE', [('California', 'NNP')])])
Here "John" is tagged as "PERSON"
"Stanford" as "ORGANIZATION"
"California" as "GPE". Geopolitical entity, i.e. countries, cities, states.
#Usando SpaCy
import spacy
nlp = spacy.load('en')
# Leer o crear una sentencia
doc = nlp(u'Apple is ready to launch new phone worth $10000 in New york time square ')
for ent in doc.ents:
print(ent.text, ent.start_char, ent.end_char, ent.label_)
#salida
Apple 0 5 ORG
10000 42 47 MONEY
New york 51 59 GPE
Análisis de sentimientos.
Para hacer un análisis de sentimientos use TextBlob. El cual, básicamente dará 2 métricas.
1. Polaridad = La polaridad se encuentra en el rango de [-1,1] donde 1 significa un
enunciado positivo y -1 significa una sentencia negativa.
2. Subjetividad = Subjetividad se refiere a que principalmente es una opinión personal
y no una información objetiva [0,1].
review = "I like this phone. screen quality and camera clarity is really good."
review2 = "This tv is not good. Bad quality, no clarity, worst experience"
from textblob import TextBlob
#TextBlob tiene un modelo de predicción de sentimientos previamente entrenado
blob = TextBlob(review)
blob.sentiment
#salida
Sentiment(polarity=0.7, subjectivity=0.6000000000000001)
blob = TextBlob(review2)
blob.sentiment
#salida
Sentiment(polarity=-0.6833333333333332,
subjectivity=0.7555555555555555)
# Esta es una crítica negativa, ya que la polaridad es "-0.68".
Texto ambiguo.
Text1 = 'I went to the bank to deposit my money'
Text2 = 'The river bank was full of dead fishes'
#Instalar pywsd
!pip install pywsd
#Importar funciones
from nltk.corpus import wordnet as wn
from nltk.stem import PorterStemmer
from itertools import chain
from pywsd.lesk import simple_lesk
# Frases
bank_sents = ['I went to the bank to deposit my money', 'The river bank was full of dead
fishes']
# llamando a la función lesk e imprimiendo resultados tanto ambas frases
print ("Context-1:", bank_sents[0])
answer = simple_lesk(bank_sents[0],'bank')
print ("Sense:", answer)
print ("Definition : ", answer.definition())
print ("Context-2:", bank_sents[1])
answer = simple_lesk(bank_sents[1],'bank','n')
print ("Sense:", answer)
print ("Definition : ", answer.definition())
#Resultado:
Context-1: I went to the bank to deposit my money
Sense: Synset('depository_financial_institution.n.01')
Definition : a financial institution that accepts deposits and channels the money into
lending activities
Context-2: The river bank was full of dead fishes
Sense: Synset('bank.n.01')
Definition : sloping land (especially the slope beside a body of water)
Convertir voz a texto.
!pip install SpeechRecognition
!pip install PyAudio
import speech_recognition as sr
r=sr.Recognizer()
with sr.Microphone() as source:
print("Please say something")
audio = r.listen(source)
print("Time over, thanks")
try:
print("I think you said: "+r.recognize_google(audio));
#print("I think you said: "+r.recognize_google(audio, language ='en-EN'));
#except sr.UnknownValueError:
#print("Google Speech Recognition could not understand audio")
#except sr.RequestError as e:
#print("Could not request results from Google Speech
#Recognition service; {0}".format(e))
except:
pass;
#Salida
Please say something
Time over, thanks
I think you said: I am learning natural language processing
Convertir texto a voz.
!pip install gTTS
from gtts import gTTS
#elija el lenguaje, English('en')
convert = gTTS(text='I like this NLP book', lang='en', slow=False)
# Guardando el audio en un archivo mp3 llamado
myobj.save("audio.mp3")
#output: Por favor reproduzca el archivo audio.mp3 guardado localmente en su máquina
para escucharlo
Traducción de voz.
!pip install goslate
import goslate
text = "Bonjour le monde"
gs = goslate.Goslate()
translatedText = gs.translate(text,'en')
print(translatedText)
#Salida
Hi world
Procesamiento del lenguaje natural con python

Procesamiento del lenguaje natural con python

  • 1.
    Inteligencia Artificial Lenguaje Python:Procesamiento del Lenguaje Natural. Universidad Nacional de Ingeniería FACULTAD DE CIENCIAS Y SISTEMAS – DEPARTAMENTO DE INFORMÁTICA
  • 3.
    Procesamiento del LenguajeNatural con Python. Recopilación de datos de archivos PDF. !pip install PyPDF2 import PyPDF2 from PyPDF2 import PdfFileReader pdf = open("file.pdf","rb") pdf_reader = PyPDF2.PdfFileReader(pdf) print(pdf_reader.numPages) page = pdf_reader.getPage(0) print(page.extractText()) pdf.close() Recopilación de datos de archivos Word. !pip install docx from docx import Document doc = open("file.docx","rb") document = docx.Document(doc) docu="" for para in document.paragraphs: docu += para.text print(docu) Recopilación de datos de archivos HTML. !pip install bs4 import urllib.request as urllib2 from bs4 import BeautifulSoup response = urllib2.urlopen('https://en.wikipedia.org/wiki/Natural_language_processing') html_doc = response.read() soup = BeautifulSoup(html_doc, 'html.parser') strhtm = soup.prettify() print (strhtm[:1000]) print(soup.title) print(soup.title.string) print(soup.a.string) print(soup.b.string)
  • 4.
    # Extrayendo todaslas instancias de una etiqueta particular for x in soup.find_all('a'): print(x.string) # Extrayendo todo el texto de una etiqueta particular for x in soup.find_all('p'): print(x.text) Arañando texto desde la web. Antes de “escarbar o arañar” cualquier sitio web, blog o comercio electrónico sitios web, asegúrese de leer los términos y condiciones del sitio web sobre si otorga permisos para escarbar sus datos. !pip install bs4 !pip install requests from bs4 import BeautifulSoup import requests import pandas as pd from pandas import Series, DataFrame from ipywidgets import FloatProgress from time import sleep from IPython.display import display import re import pickle url = 'http://www.imdb.com/chart/top?ref_=nv_mv_250_6' result = requests.get(url) c = result.content soup = BeautifulSoup(c,"lxml")' summary = soup.find('div',{'class':'article'}) # Creando listas vacías para agregar los datos extraídos. moviename = [] cast = [] description = [] rating = [] ratingoutof = [] year = [] genre = [] movielength = [] rot_audscore = [] rot_avgrating = [] rot_users = [] # Extrayendo los datos requeridos del soup html. rgx = re.compile('[%s]' % '()')
  • 5.
    f = FloatProgress(min=0,max=250) display(f) for row,i in zip(summary.find('table'). findAll('tr'),range(len(summary.find('table').findAll('tr')))): for sitem in row.findAll('span',{'class':'secondaryInfo'}): s = sitem.find(text=True) year.append(rgx.sub(", s)) for ritem in row.findAll('td',{'class':'ratingColumn imdbRating'}): for iget in ritem.findAll('strong'): rating.append(iget.find(text=True)) ratingoutof.append(iget.get('title').split(' ', 4)[3]) for item in row.findAll('td',{'class':'titleColumn'}): for href in item.findAll('a',href=True): moviename.append(href.find(text=True)) rurl = 'https://www.rottentomatoes.com/m/'+ href. find(text=True) try: rresult = requests.get(rurl) except requests.exceptions.ConnectionError: status_code = "Connection refused" rc = rresult.content rsoup = BeautifulSoup(rc) try: rot_audscore.append(rsoup.find('div', {'class':'meter-value'}).find('span', {'class':'superPageFontColor'}).text) rot_avgrating.append(rsoup.find('div', {'class':'audience-info hidden-xs superPageFontColor'}).find('div').contents[2]. strip()) rot_users.append(rsoup.find('div', {'class':'audience-info hidden-xs superPageFontColor'}).contents[3].contents[2]. strip()) except AttributeError: rot_audscore.append("") rot_avgrating.append("") rot_users.append("") cast.append(href.get('title')) imdb = "http://www.imdb.com" + href.get('href') try: iresult = requests.get(imdb) ic = iresult.content
  • 6.
    isoup = BeautifulSoup(ic) description.append(isoup.find('div', {'class':'summary_text'}).find(text=True).strip()) genre.append(isoup.find('span',{'class':'itempr op'}).find(text=True)) movielength.append(isoup.find('time', {'itemprop':'duration'}).find(text=True).strip()) exceptrequests.exceptions.ConnectionError: description.append("") genre.append("") movielength.append("") sleep(.1) f.value = i # Listar a pandas series moviename = Series(moviename) cast = Series(cast) description = Series(description) rating = Series(rating) ratingoutof = Series(ratingoutof) year = Series(year) genre = Series(genre) movielength = Series(movielength) rot_audscore = Series(rot_audscore) rot_avgrating = Series(rot_avgrating) rot_users = Series(rot_users) # creando un dataframe y analizándolo imdb_df = pd.concat([moviename,year,description,genre, movielength,cast,rating,ratingoutof, rot_audscore,rot_avgrating,rot_users],axis=1) imdb_df.columns = [ 'moviename','year','description','genre', 'movielength','cast','imdb_rating', 'imdb_ratingbasedon','tomatoes_audscore', 'tomatoes_rating','tomatoes_ratingbasedon'] imdb_df['rank'] = imdb_df.index + 1 imdb_df.head(1) # Guardando el archivo como CSV. imdb_df.to_csv("imdbdataexport.csv")
  • 7.
    Generando N-gramas usandoTextBlob. Text = "Estoy aprendiendo PNL" #Importando textblob from textblob import TextBlob #Para unigram : Use n = 1 TextBlob(Text).ngrams(1) TextBlob(Text).ngrams(2) Extraer frases nominales. #Importando nltk import nltk from textblob import TextBlob #Extrayendo elementos blob = TextBlob("John está aprendiendo el procesamiento del lenguaje natural") for np in blob.noun_phrases: print(np) Encontrar similitudes entre textos documents = ("Me gusta la PNL", "Estoy explorando PNL", "Soy un principiante en PNL", "Quiero aprender PNL", "Me gusta la PNL avanzada") #Importando bibliotecas from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics.pairwise import cosine_similarity #Compute tfidf: método de ingeniería de características llamado matriz de coincidencia. tfidf_vectorizer = TfidfVectorizer() tfidf_matrix = tfidf_vectorizer.fit_transform(documents) tfidf_matrix.shape # Salida # Calcular similitud para la primera oración con el resto de las oraciones cosine_similarity(tfidf_matrix[0:1],tfidf_matrix) # Salida
  • 8.
    Correspondencia fonética !pip installfuzzy import fuzzy soundex = fuzzy.Soundex(4) soundex('natural') #salida 'N364' soundex('natuaral') 104 #salida 'N364' soundex('language') #salida 'L52' soundex('processing') #salida 'P625' Etiquetar partes del lenguaje. Text = "I love NLP and I will learn NLP in 2 month" import nltk from nltk.corpus import stopwords from nltk.tokenize import word_tokenize, sent_tokenize stop_words = set(stopwords.words('english')) # Tokenizar el texto tokens = sent_tokenize(text) #Genera etiquetado para todos los tokens usando loop for i in tokens: words = nltk.word_tokenize(i) words = [w for w in words if not w in stop_words] # Etiquetador. tags = nltk.pos_tag(words) tags #salida: [('I', 'PRP'), ('love', 'VBP'), ('NLP', 'NNP'), ('I', 'PRP'), ('learn', 'VBP'), ('NLP', 'RB'),
  • 9.
    ('2month', 'CD')] • CCcoordinating conjunction. • CD cardinal digit. • DT determiner. • EX existential there (like: “there is” ... think of it like • “there exists”.) • FW foreign word. • IN preposition/subordinating conjunction. • JJ adjective ‘big’. • JJR adjective, comparative ‘bigger’. • JJS adjective, superlative ‘biggest’. • LS list marker 1.) • MD modal could, will. • NN noun, singular ‘desk’. • NNS noun plural ‘desks’. • NNP proper noun, singular ‘Harrison’. • NNPS proper noun, plural ‘Americans’. • PDT predeterminer ‘all the kids’. • POS possessive ending parent’s. • PRP personal pronoun I, he, she. • PRP$ possessive pronoun my, his, hers. • RB adverb very, silently. • RBR adverb, comparative better. • RBS adverb, superlative best. • RP particle give up. • TO to go ‘to’ the store. • UH interjection. • VB verb, base form take. • VBD verb, past tense took. • VBG verb, gerund/present participle taking. • VBN verb, past participle taken. • VBP verb, sing. present, non-3d take. • VBZ verb, 3rd person sing. present takes. • WDT wh-determiner which. • WP wh-pronoun who, what. • WP$ possessive wh-pronoun whose. • WRB wh-adverb where, when.
  • 10.
    Extraer entidades deltexto. sent = "John is studying at Stanford University in California" import nltk from nltk import ne_chunk from nltk import word_tokenize ne_chunk(nltk.pos_tag(word_tokenize(sent)), binary=False) #salida Tree('S', [Tree('PERSON', [('John', 'NNP')]), ('is', 'VBZ'), ('studying', 'VBG'), ('at', 'IN'), Tree('ORGANIZATION', [('Stanford', 'NNP'), ('University', 'NNP')]), ('in', 'IN'), Tree('GPE', [('California', 'NNP')])]) Here "John" is tagged as "PERSON" "Stanford" as "ORGANIZATION" "California" as "GPE". Geopolitical entity, i.e. countries, cities, states. #Usando SpaCy import spacy nlp = spacy.load('en') # Leer o crear una sentencia doc = nlp(u'Apple is ready to launch new phone worth $10000 in New york time square ') for ent in doc.ents: print(ent.text, ent.start_char, ent.end_char, ent.label_) #salida Apple 0 5 ORG 10000 42 47 MONEY New york 51 59 GPE Análisis de sentimientos. Para hacer un análisis de sentimientos use TextBlob. El cual, básicamente dará 2 métricas. 1. Polaridad = La polaridad se encuentra en el rango de [-1,1] donde 1 significa un enunciado positivo y -1 significa una sentencia negativa. 2. Subjetividad = Subjetividad se refiere a que principalmente es una opinión personal y no una información objetiva [0,1]. review = "I like this phone. screen quality and camera clarity is really good." review2 = "This tv is not good. Bad quality, no clarity, worst experience" from textblob import TextBlob #TextBlob tiene un modelo de predicción de sentimientos previamente entrenado blob = TextBlob(review)
  • 11.
    blob.sentiment #salida Sentiment(polarity=0.7, subjectivity=0.6000000000000001) blob =TextBlob(review2) blob.sentiment #salida Sentiment(polarity=-0.6833333333333332, subjectivity=0.7555555555555555) # Esta es una crítica negativa, ya que la polaridad es "-0.68". Texto ambiguo. Text1 = 'I went to the bank to deposit my money' Text2 = 'The river bank was full of dead fishes' #Instalar pywsd !pip install pywsd #Importar funciones from nltk.corpus import wordnet as wn from nltk.stem import PorterStemmer from itertools import chain from pywsd.lesk import simple_lesk # Frases bank_sents = ['I went to the bank to deposit my money', 'The river bank was full of dead fishes'] # llamando a la función lesk e imprimiendo resultados tanto ambas frases print ("Context-1:", bank_sents[0]) answer = simple_lesk(bank_sents[0],'bank') print ("Sense:", answer) print ("Definition : ", answer.definition()) print ("Context-2:", bank_sents[1]) answer = simple_lesk(bank_sents[1],'bank','n') print ("Sense:", answer) print ("Definition : ", answer.definition()) #Resultado: Context-1: I went to the bank to deposit my money Sense: Synset('depository_financial_institution.n.01') Definition : a financial institution that accepts deposits and channels the money into lending activities Context-2: The river bank was full of dead fishes Sense: Synset('bank.n.01') Definition : sloping land (especially the slope beside a body of water)
  • 12.
    Convertir voz atexto. !pip install SpeechRecognition !pip install PyAudio import speech_recognition as sr r=sr.Recognizer() with sr.Microphone() as source: print("Please say something") audio = r.listen(source) print("Time over, thanks") try: print("I think you said: "+r.recognize_google(audio)); #print("I think you said: "+r.recognize_google(audio, language ='en-EN')); #except sr.UnknownValueError: #print("Google Speech Recognition could not understand audio") #except sr.RequestError as e: #print("Could not request results from Google Speech #Recognition service; {0}".format(e)) except: pass; #Salida Please say something Time over, thanks I think you said: I am learning natural language processing Convertir texto a voz. !pip install gTTS from gtts import gTTS #elija el lenguaje, English('en') convert = gTTS(text='I like this NLP book', lang='en', slow=False) # Guardando el audio en un archivo mp3 llamado myobj.save("audio.mp3") #output: Por favor reproduzca el archivo audio.mp3 guardado localmente en su máquina para escucharlo Traducción de voz. !pip install goslate import goslate text = "Bonjour le monde" gs = goslate.Goslate() translatedText = gs.translate(text,'en') print(translatedText)
  • 13.