Categories
Python Answers

How to compute the similarity between two text documents with Python?

Sometimes, we want to compute the similarity between two text documents with Python.

In this article, we’ll look at how to compute the similarity between two text documents with Python.

How to compute the similarity between two text documents with Python?

To compute the similarity between two text documents with Python, we can use the scikit-learn library.

To install it, we run

pip install -U scikit-learn

Then we use by writing

from sklearn.feature_extraction.text import TfidfVectorizer

documents = [open(f).read() for f in text_files]
tfidf = TfidfVectorizer().fit_transform(documents)
pairwise_similarity = tfidf * tfidf.T

to open the files with the paths in the text_files list.

Then we create a TfidfVectorizer object and call fit_transforms with the strings returned by read.

And then we get their pairwise similarity with tfidf * tfidf.T.

Conclusion

To compute the similarity between two text documents with Python, we can use the scikit-learn library.

Categories
Python Answers

How to fix WebDriverException: unknown error: cannot find Chrome binary error with Selenium in Python for older versions of Google Chrome?

Sometimes, we want to fix WebDriverException: unknown error: cannot find Chrome binary error with Selenium in Python for older versions of Google Chrome.

In this article, we’ll look at how to fix WebDriverException: unknown error: cannot find Chrome binary error with Selenium in Python for older versions of Google Chrome.

How to fix WebDriverException: unknown error: cannot find Chrome binary error with Selenium in Python for older versions of Google Chrome?

To fix WebDriverException: unknown error: cannot find Chrome binary error with Selenium in Python for older versions of Google Chrome, we set the path of the Chrome binary.

For instance, we write

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.binary_location = "C:\\Program Files\\Chrome\\chrome64_55.0.2883.75\\chrome.exe"
driver = webdriver.Chrome(chrome_options = options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get('http://example.com/')
driver.quit()

to set create an Options object.

Then we set options.binary_location to the path of the Chrome binary.

And then we set the executable_path to the path of the Chrome driver.

Then we call get to open a page at the URL and call quit to exit.

Conclusion

To fix WebDriverException: unknown error: cannot find Chrome binary error with Selenium in Python for older versions of Google Chrome, we set the path of the Chrome binary.

Categories
Python Answers

How to fix installation issues for PyAudio, PortAudio: “fatal error C1083: Cannot open include file: ‘portaudio.h’: No such file or directory” with Python?

Sometimes, we want to fix installation issues for PyAudio, PortAudio: "fatal error C1083: Cannot open include file: ‘portaudio.h’: No such file or directory" with Python.

In this article, we’ll look at how to fix installation issues for PyAudio, PortAudio: "fatal error C1083: Cannot open include file: ‘portaudio.h’: No such file or directory" with Python.

How to fix installation issues for PyAudio, PortAudio: "fatal error C1083: Cannot open include file: ‘portaudio.h’: No such file or directory" with Python?

To fix installation issues for PyAudio, PortAudio: "fatal error C1083: Cannot open include file: ‘portaudio.h’: No such file or directory" with Python, we install pipwin and use it to install pyaudio.

To fix this, we run

pip install pipwin
python -m pipwin install pyaudio

to install pipwin.

And then we use pipwin to install pyaudio with

python -m pipwin install pyaudio

on Windows.

On Ubuntu distributions, we run

sudo apt install portaudio19-dev

to install the portaudio19-dev package before installing pyaudio.

Conclusion

To fix installation issues for PyAudio, PortAudio: "fatal error C1083: Cannot open include file: ‘portaudio.h’: No such file or directory" with Python, we install pipwin and use it to install pyaudio.

Categories
Python Answers

How to download NLTK data with Python?

Sometimes, we want to download NLTK data with Python.

In this article, we’ll look at how to download NLTK data with Python.

How to download NLTK data with Python?

To download NLTK data with Python, we use the download method.

For instance, we write

import nltk

nltk.download('punkt')

to call download to download the punkt sentence tokenizer.

We can also download a basic list of data with

nltk.download('popular')

Conclusion

To download NLTK data with Python, we use the download method.

Categories
Python Answers

How to extract the n-th elements from a list of tuples with Python?

Sometimes, we want to extract the n-th elements from a list of tuples with Python.

In this article, we’ll look at how to extract the n-th elements from a list of tuples with Python.

How to extract the n-th elements from a list of tuples with Python?

To extract the n-th elements from a list of tuples with Python, we can use list comprehension.

For instance, we write

elements = [(1,1,1),(2,3,7),(3,5,10)]
n = 1
l = [x[n] for x in elements]

to get the 2nd element from each tuple x in the elements list and assign the list to l.

Conclusion

To extract the n-th elements from a list of tuples with Python, we can use list comprehension.