Ricky Martin

Ricky Martin

1593829496

10 Pure Python Functions for Ad Hoc Text Analysis

Text****data is the most common format of the data out there. An abundance of articles, tweets, documents, books and else, fly around us every day. The amount of insights you can extract is immense and the tools to help on extracting them are improving continuously.

In Python, you can use external libraries to process text data. One issue with these libraries is that they are not lightweight and they do have a steep learning curve for starters. In many cases, functions written in pure Python can do great if your sole intention is to explore and get to know with the data.

Below I’ve provided a list of 10 such functions that I use mostly. These functions are mainly **heuristic functions **that work properly under assumptions made. These assumptions are stated at the points below at functions where they are considered.

#1 Get Sentences

Assumption: Everything that ends with a dot, question mark or exclamation mark, is taken as a sentence.

def get_sentences(text):
    import re
    pattern = r'([A-Z][^\.!?]*[\.!?])'
    pattern_compiled = re.compile(pattern, re.M)
    list_of_sentences = re.findall(pattern, text)
    return list_of_sentences


# Test
text = """This is the most frequent question we're asked by prospective students. And our response? Absolutely! We've trained people from all walks of life."""
get_sentences(text)


# [
#     "This is the most frequent questions we're asked by prospective students.",
#     'And our response?',
#     'Absolutely!',
#     "We've trained people from all walks of life."
# ]

#2 Get List of Items per Sentence

Assumption: A colon followed by a list of items separated with comma is taken.

def get_listed_items_with_colon(text):
    import re
    list_of_items = []
    list_of_sentences = re.split('\.|\?|\!', text)
    for sentence in list_of_sentences:
        if ':' in sentence:
            start_index = sentence.find(':')
            sub_sentence = sentence[start_index+1:]
            list_of_items.append([word.strip() for word in sub_sentence.split(',')])
    return list_of_items


# Test
text = """The house has everything I need: two bedrooms, a backyard, and a garage. I have several favorite genres of movies: drama, science fiction, and mystery."""
get_listed_items_with_colon(text)

# [ ['two bedrooms', 'a backyard', 'and a garage'], ['drama', 'science fiction', 'and mystery'] ]

#data-science #nlp #programming #text-mining #python

What is GEEK

Buddha Community

10 Pure Python Functions for Ad Hoc Text Analysis
Ray  Patel

Ray Patel

1619510796

Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

Ray  Patel

Ray Patel

1619518440

top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

1) swap two numbers.

2) Reversing a string in Python.

3) Create a single string from all the elements in list.

4) Chaining Of Comparison Operators.

5) Print The File Path Of Imported Modules.

6) Return Multiple Values From Functions.

7) Find The Most Frequent Value In A List.

8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners

Alec  Nikolaus

Alec Nikolaus

1599758100

Convert Text to Speech in Python

Learn how to convert your Text into Voice with Python and Google APIs

Text to speech is a process to convert any text into voice. Text to speech project takes words on digital devices and convert them into audio with a button click or finger touch. Text to speech python project is very helpful for people who are struggling with reading.

Project Prerequisites

To implement this project, we will use the basic concepts of Python, Tkinter, gTTS, and playsound libraries.

  • Tkinter is a standard GUI Python library that is one of the fastest and easiest ways to build GUI applications using Tkinter.
  • gTTS (Google Text-to-Speech) is a Python library, which is a very easy library that converts the text into audio.
  • The playsound module is used to play audio files. With this module, we can play a sound file with a single line of code.

To install the required libraries, you can use pip install command:

pip install tkinter
pip install gTTS
pip install playsound

Download Python Text to Speech Project Code

Please download the source code of Text to Speech Project: Python Text to Speech

Text to Speech Python Project

The objective of this project is to convert the text into voice with the click of a button. This project will be developed using Tkinter, gTTs, and playsound library.

In this project, we add a message which we want to convert into voice and click on play button to play the voice of that text message.

  • Importing the modules
  • Create the display window
  • Define functions

So these are the basic steps that we will do in this Python project. Let’s start.

#python tutorials #python project #python project for beginners #python text to speech #text to speech convertor #python

Ricky Martin

Ricky Martin

1593829496

10 Pure Python Functions for Ad Hoc Text Analysis

Text****data is the most common format of the data out there. An abundance of articles, tweets, documents, books and else, fly around us every day. The amount of insights you can extract is immense and the tools to help on extracting them are improving continuously.

In Python, you can use external libraries to process text data. One issue with these libraries is that they are not lightweight and they do have a steep learning curve for starters. In many cases, functions written in pure Python can do great if your sole intention is to explore and get to know with the data.

Below I’ve provided a list of 10 such functions that I use mostly. These functions are mainly **heuristic functions **that work properly under assumptions made. These assumptions are stated at the points below at functions where they are considered.

#1 Get Sentences

Assumption: Everything that ends with a dot, question mark or exclamation mark, is taken as a sentence.

def get_sentences(text):
    import re
    pattern = r'([A-Z][^\.!?]*[\.!?])'
    pattern_compiled = re.compile(pattern, re.M)
    list_of_sentences = re.findall(pattern, text)
    return list_of_sentences


# Test
text = """This is the most frequent question we're asked by prospective students. And our response? Absolutely! We've trained people from all walks of life."""
get_sentences(text)


# [
#     "This is the most frequent questions we're asked by prospective students.",
#     'And our response?',
#     'Absolutely!',
#     "We've trained people from all walks of life."
# ]

#2 Get List of Items per Sentence

Assumption: A colon followed by a list of items separated with comma is taken.

def get_listed_items_with_colon(text):
    import re
    list_of_items = []
    list_of_sentences = re.split('\.|\?|\!', text)
    for sentence in list_of_sentences:
        if ':' in sentence:
            start_index = sentence.find(':')
            sub_sentence = sentence[start_index+1:]
            list_of_items.append([word.strip() for word in sub_sentence.split(',')])
    return list_of_items


# Test
text = """The house has everything I need: two bedrooms, a backyard, and a garage. I have several favorite genres of movies: drama, science fiction, and mystery."""
get_listed_items_with_colon(text)

# [ ['two bedrooms', 'a backyard', 'and a garage'], ['drama', 'science fiction', 'and mystery'] ]

#data-science #nlp #programming #text-mining #python

Art  Lind

Art Lind

1602968400

Python Tricks Every Developer Should Know

Python is awesome, it’s one of the easiest languages with simple and intuitive syntax but wait, have you ever thought that there might ways to write your python code simpler?

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

Let’s get started

Swapping value in Python

Instead of creating a temporary variable to hold the value of the one while swapping, you can do this instead

>>> FirstName = "kalebu"
>>> LastName = "Jordan"
>>> FirstName, LastName = LastName, FirstName 
>>> print(FirstName, LastName)
('Jordan', 'kalebu')

#python #python-programming #python3 #python-tutorials #learn-python #python-tips #python-skills #python-development