Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add punctuation to text using python?

Tags:

python

nltk

I am playing with the IBM Watson Speech To Text Service API. For those who do not know this service is being used to transcribe audio. You upload an audio file to the service and it returns back the text. The service has been good so far, but the problem is that that returned text does not contain punctuation marks. I tried solving this problem with nltk, but no results.

Some nltk code I have tried.

#string is the text
string = """Hey guys today I'm gonna show you how to make bulletproof coffee so free guys and never heard a blooper coffee it's been around since like two thousand two been around for awhile but i think it's been a lot more popular now probably within the last maybe year two years so for me I recently just started doing bulletproof coffee say about a couple so basically regular coffee you know you get your regular coffee and then you put cream and you put sugar mix it up to get this nice little creamy sweet taste so bulletproof coffee the differences as they're putting cream and sugar your you play coffee you put butter grass fed butter and coconut oil so the difference is instead of using sugar you're using and healthy fats and the point of that is it's really good for if you're asked about the cato jenneke dieter basically low carb diet last you won't do is load up your coffee with how much sugar and roads are low carb diet and also a lot of people actually use bulletproof coffee as a way to replace their practice now i'm not saying to do that because albion is as of right now I still you might break I drink my coffee and at all times a wreck this but again though I do eat a lot so home a lot more than a lot of normal people so keep this in mind okay so this is how the rescue workers the book per copy you want to go ahead and put two cups of coffee so it's a here is we have one cup put it into our blender the trick here is the blending park you don't blend it doesn't come out right I use it not blended and then that coconut oil and the butter cut just floats on top when you got this oily coffee taste not the not the best now two cups of coffee you put two teaspoons of butter you wanna go and grass fed butter so let's say this is one teaspoon here yeah I just gonna estimated this will be two teaspoons every go two teaspoons of butter and how it I guess and crazy I remember doing this as a man sir put butter in there but it actually doesn't and it's really really good then you put about one to two teaspoons of cooking oil so for me i'm going to go with so here's about probably about one tablespoon and here's to get there you go throw on the cat and   so not only is a good to be honest i think on a local wow it works out really well sugar is my car but when it comes to drinking the i can't really put any pre sugar sugar so what i've basically been doing a lot and you know black take the paper personally i can enjoy the cream and sugar so i thought this to be a really good replacement it makes is reading the taste and i think it's leann when i used even though i'm not sugar at all so it tastes good good for you and it's a low carb no take a look got a little foam on the top uh man fiasco we smell this now the butter coconut so check this out you get this nice little cream uh this is a taste test so smooth so creamy and i never put any sugar and a lot of people come in and play like go you know so we can love or something the v. f. or something the person for me albeit like this is perfect you know it's creamy and it's not bitter like black coffee plus it fits nicely with a low carb diet so a lot people drink this morning they replace directors because this does have a lot of calories so here's something to look out for them he noticed two teaspoons of butter want to tease bozo coconut oil a lot of calories just give an example one tablespoon of coconut oil has but a hundred and thirty calories which means two of them it's like two hundred sixty calories the butter is likely to borrow from that so you're looking at sort of ballpark a four hundred calories plus so that's what a lot of people drink this as replacement for their breakfast and with the good fats they give them the energy that they need to go throughout the day and also because that you watched another video talk about the difference we fat and carbs are sugar that exhort slower and so the energy lasts longer it doesn't get any sort of super fast and gets burnt off versus when you let's say drink sugary coffee you know we put sugar and cream in your coffee that sugar gets exhort super fast so you get this spike in energy up in the morning and then it starts crashing down but i noticed drinking this you don't gain topic crashes because it's back they said sugar so exhort slowly and kinda sustains so this is how i make my coffee i love it i suggest give it a try guys especially if you're one of coffee drinkers replacing your cream in your sugar with some bulletproof coffee you try some coconut oil and grass fed butter so i tried our guys see i like more workouts mortician how to work out the ads the most is that you want sixpack shortcuts dot com piece"""

import nltk
n = nltk.tokenize.punkt.PunktSentenceTokenizer()
n.sentences_from_text(string)

Is there any way to solve my problem? What would you do if you were in my case?

like image 821
orthodoxpirate Avatar asked Jul 20 '15 10:07

orthodoxpirate


People also ask

How do you define punctuation in Python?

punctuation is a pre-initialized string used as string constant. In Python, string. punctuation will give the all sets of punctuation. Parameters : Doesn't take any parameter, since it's not a function.

Does Google Docs have auto punctuation?

But there's one punctuation mark that has a bizarre hold on writers—the humble em dash. And after years of waiting, Google Docs now automatically turns strings of hyphens into em dashes and en dashes. Yes, Microsoft Word implemented this feature over a decade ago.


1 Answers

Unless you've got some serious machine learning skills, you'll have to write your own "rules" to handle this. Rules are must easier for this use case to get started.

A sentence must adhere to specific "grammar rules". These rules are pretty basic hence the reason they can be taught in primary school. Problem is most people don't follow grammar rules when writing or speaking. You'll first need to focus on text that follows the rules. Based on this premise here is what I would do

  1. Run NLTK POS Tagger on string (once we know the Parts of Speech we can code rules)
  2. Identify tokens whose part of speech breaks the grammar rules of a sentence (sentences shouldn't end in a preposition)
  3. Identify tokens that a sentence could end with based on grammar rules (Nouns are a great start)
  4. Find a large well punctuated grammatically correct corpora of text and remove the punctuation.
  5. Try your "new rules" out on adding punctuation to the corpora. Adjust your rules. Rinse and Repeat
like image 57
django-d Avatar answered Nov 07 '22 12:11

django-d