Auto define word in clipboard (Text2Speech) [+SOURCE] - Adolf Hipster - Sep 1, 2015
Here's a little program I created today.
The code is quite shitty BUT I dont think anyone has released anything like this so here we go.
What this basically does is lets say you're on reddit and reading something and you see a word you dont know what it means. You simply highlight the word in your browser and copy it, and the program will automatically look up the definition of the word and read it out loud for you via text 2 speech (and so on with all other words you copy)
Here's the code copy pastable
Code Imports System.Text.RegularExpressions
Public Class Form1
Dim Word As String
Dim PrevWord As String
Dim URL As String = "http://services.aonaware.com/DictService/Default.aspx?action=define&dict=wn&query=" & Word
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Function GetBetween(ByVal input As String, ByVal str1 As String, ByVal str2 As String, ByVal index As Integer) As String
Dim temp As String = System.Text.RegularExpressions.Regex.Split(input, str1)(index + 1)
Return System.Text.RegularExpressions.Regex.Split(temp, str2)(0)
End Function
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Log("-----------------")
Log("Timer1 Ticked")
Word = Clipboard.GetText()
If Word = "" Or Word.Length > 64 Then
'long or empty
Log("Word is too long or empty")
Else
'Word is not empty and is less than 10 chars
Log("Word from clipboard is " & Word)
'preparing to look up word, setting variable prevword to our current word
PrevWord = Word
Log("Set PrevWord to " & Word)
Log("Looking up word: " & Word)
Dim Source As String
Dim Definition As String
Dim URL As String = "http://services.aonaware.com/DictService/Default.aspx?action=define&dict=wn&query=" & Word
Using WC As New Net.WebClient
Source = WC.DownloadString(URL)
If Source.Contains("No definitions found for") Then
'couldnt find def
Log("No definition for word: " & Word)
Else
Definition = GetBetween(Source, ": ", ":", 0).Replace(" [syn", String.Empty).Replace(""", String.Empty)
Definition = CleanString(Definition)
Log("Definition: " & Definition)
ListBox2.Items.Add("Word: " & Word & " - Definition: " & Definition)
Log("Reading the definition to the user")
Dim SAPI
SAPI = CreateObject("SAPI.spvoice")
SAPI.Speak(Definition)
Log("Done reading definition to user")
End If
End Using
Timer1.Enabled = False
Timer1.Stop()
'lookup here
Word = ""
Log("Word Checkup Done, setting word to BLANK and waiting for new word")
Clipboard.Clear()
Timer1.Enabled = True
Timer1.Start()
End If
End Sub
Function Log(ByVal Message As String)
ListBox1.Items.Add("[" & DateTime.Now.ToLongTimeString() & "] - " & Message)
Try
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
Catch ex As Exception
End Try
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Enabled = False
Timer1.Stop()
ListBox1.Items.Clear()
Timer1.Enabled = True
Timer1.Start()
End Sub
Function CleanString(ByVal Str As String)
'remove all excessive spaces
Dim cleaned As String = Regex.Replace(Str, "\s{2,}", " ")
'remove all numerals
Dim RemovedNumerals As String = Regex.Replace(cleaned, "[\d-]", String.Empty)
Return Regex.Replace(RemovedNumerals, "<.*?>", "")
End Function
End Class
Here's an Image of the program (nothing fancy)
![[Image: 59e0671cf56487ae36f21d73825dec42.png]](https://i.gyazo.com/59e0671cf56487ae36f21d73825dec42.png)
First three ticks I had no word in my clipboard, and then the last one I copied the word Bird into my clipboard and it read out the definition.
The upper listbox is for defined words only to get listed, the listbox down under is like a logger for my program (showing everything that happens)
SOME IDEAS FOR FURTHER DEVELOPMENT!- Hook the keyboard and wait for a certan set of keys to be pressed and then define the word (instead of using a timer like im doing now)
- The service Im using is offering several definitions of words and synonyms, why not parse out them also and read/show it to the user
- Allow the user to chose from other dictionaries to use (currently using Wordnet , check the URL im downloading in the code)
Hope anyone finds use of this.
* - ze-slade - Sep 1, 2015
That's actually pretty cool op, nice job.
* - Odus - Sep 1, 2015
I like the idea. You could use a clipboard hook though and ditch the timer. Might write my own version of this, there's a lot I would do differently.
* - Adolf Hipster - Sep 1, 2015
(Sep 1, 2015 11:33 AM)Odus Wrote: I like the idea. You could use a clipboard hook though and ditch the timer. Might write my own version of this, there's a lot I would do differently.
Yeah I understand there are plenty of other (and better) ways to do this.
This was just something I threw together in 30-40 minutes.
(Sep 1, 2015 11:33 AM)Odus Wrote: I like the idea. You could use a clipboard hook though and ditch the timer. Might write my own version of this, there's a lot I would do differently.
Hmm I looked into clipboard hooking and it was fairly simple to set up.
I'll experiment with it and release a proper version.
Thanks for telling me about clipboard hooking.
|