Behavior Control
This control is developed by:
AvatarDJ Sures
Created
Updated
Help Index

Getlinecontainingfromarray

How To Download and Use This Control

  1. Make sure you have the latest version of EZ-Builder installed.
  2. Select Download This Control button below to download the file.
  3. Double click the downloaded file to open it.
  4. The installer will automatically add this control to EZ-Builder.
Download This Control

Adds a new EZ-Script function GetLineContainingFromArray() that will return an item from the array that contains the specified text. The syntax of the function is...

Code:


GetLineContainingFromArray("$array", "Txt to search for")


An example is as such...

Code:


# Fill an array with the word "Banana" 20 times
defineArray($array, 20, "Banana")

# set the 5th index to these words
$array[5] = "Apple face"

# get the first line containing the text "apple"
$x = GetLineContainingFromArray("$array", "apple")

print("Found: " + $x + " Among the bananas")


Source Code

You can download the source code here: GetLineContainingFromArray.zip



#1  
Hello All, 

I have a bit of an issue that I hope someone has a better solution for. I have a text file containing 66247 lines. Each line is a word followed by a phoneme string. I am using it to look up words and convert them to the appropriate phoneme string, which I then send to a votrax SC-01 speech synthesizer (the coolest, most 80s robot sounding speech synthesizer ever made!) My search string can be any English word (for example, if I want to use the rss reader function in ez-builder, and have my votrax read the article, I don't know a priori what words I will need to look up). This isn't a problem, since my dictionary is very complete (thanks CMU). However, I am finding that my search method (code below) is taking a very long time. Can anyone suggest a better method for searching this dictionary?

$lookUpIndex=0
$target="Hello"

:loopDictionary
$line = FileReadLine("C:\Documents\EZ-Builder\My Projects\dictionary.txt")
if(contains($line,$target)=TRUE)
  print("On line " +$lookUpIndex +", line reads: "+$line)
  FileReadClose("C:\Documents\EZ-Builder\My Projects\dictionary.txt")
  halt()
else
  $lookUpIndex++
  goto(loopDictionary)
endif

Basically, I read through the dictionary one line at a time looking for my word. Once I find it, I am printing out the line number and phoneme string. Only problem is for words buried deep in the dictionary, it can take several seconds to retrieve.

Thanks in advance!
Synthiam
#2  
Have you tried loading each line into an array? That way you can simply loop through the array rather than looping through the file. 

Reading from a file that way is indeed slow - as you’re not noticing:). Memory is much faster. Much much faster
#3   — Edited
Thanks DJ! I gave it a try and I think I must be doing something wrong, because according to the output timestamp, reading from an array isn't always faster than reading from the file. Here is my code (*note, "$dictionary_a" is an array containing 7236 strings, which I created in a different script):

$lookUpIndex=0
$target="axe"
$len=Length($target)

:loop
IF (contains($dictionary_a[$lookUpIndex],$target))
  $line=$dictionary_a[$lookUpIndex]
  $match=split($line, " ",0)
ELSE
  $lookUpIndex++
ENDIF
IF (Length($match)=Length($target))
  print("On line " +$lookUpIndex +", line reads: "+$line)
  halt()
ENDIF
IF ($lookUpIndex<7236)
  goto(loop)
ELSE
  print("Not in dictionary")
  halt()
ENDIF

_______________________________________
Timestamp for reading from array    (00:00:02.4314810)
Timestamp for reading from text file (00:00:01.7622398)

I know I must be doing something very inefficient, just not sure what!:)

#5   — Edited
**UPDATE,**

So this plugin really is a game changer for searching vast amounts of info stored in arrays. I reorganized the entire dictionary to rank order from shortest character length strings to longest, and alphabetically as a second order rank. Now when I use the search function, i immediately get the result i want from the array, and i don't need to do any matching or verification, which speeds it up even more. I initially ran into an issue where a shorter word like "space" was contained in a longer word, like "aerospace", but rank ordering the input solved that issue. My execution time went from ~1.5s for short strings "8 words", to 0.16s!!! Immense difference. Now the votrax is by far the slowest part of the system (as it should be).

@DJ Sures, you have created a fantastic product that continues to impress me the more I use it!! Thank you!!!
Synthiam
#6  
Hey GBot, thanks for the kind words! I'm really glad this plugin is working for you - and access to the source code means you can make any necessary changes to it:D