Thumbnail

Get Line Containing From Array

by Synthiam

Adds a new ez-script function GetLineContainingFromArray() that will return an item from the array that contains the specified text.

Requires ARC v7 (Updated 11/1/2020) Source Code

How to add the Get Line Containing From Array robot skill

  1. Load the most recent release of ARC (Get ARC).
  2. Press the Project tab from the top menu bar in ARC.
  3. Press Add Robot Skill from the button ribbon bar in ARC.
  4. Choose the Scripting category tab.
  5. Press the Get Line Containing From Array icon to add the robot skill to your project.

Don't have a robot yet?

Follow the Getting Started Guide to build a robot and use the Get Line Containing From Array robot skill.

How to use the Get Line Containing From Array robot skill

This skill adds a new EZ-Script function GetLineContainingFromArray() that will return an item from the array that contains the specified text.

Main Window

User-inserted image

1. Start Button Once the Get Line Containing From Array Skill is loaded you will need a Script skill to access it's features. Once Get Line Containing From Array code is loaded into the Script skill use the Start button to activate it.

How to Use Get Line Containing From Array

  1. Add the Get Line Containing From Array Skill (Project -> Add Skill -> Scripting -> Get Line Containing From Array).

  2. Add the Script Skill (Project -> Add Skill -> Scripting -> Script).

  3. Enter code into the Script skill (see examples below) and click the Start Button.

Code Samples

The syntax of the function is:


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

An example is as such...


# 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")

Resources

You can download the source code here: GetLineContainingFromArray.zip


ARC Pro

Upgrade to ARC Pro

Stay at the forefront of robot programming innovation with ARC Pro, ensuring your robot is always equipped with the latest advancements.

PRO
USA
#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 ARC, 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!

PRO
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

PRO
USA
#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!:)

PRO
USA
#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!!!

PRO
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

#7  

How did I miss this! I need to update my EZ Script codes so badly.

I hope EZ Script never goes away. I'd be lost. I really need to learn Javascript just in case.

PRO
Synthiam
#8  

EZSCript isn't going away - we just don't add any new features to it or work on it. JavaScript is much easier to use and way more powerful and faster:)

Also, you can browse available plugin skills here: https://synthiam.com/Products/Controls

Or simply click ADD CONTROL in the ARC software and look through the tabs.

#9  

Good to hear. I'll be working on my (currently non existent) Javascripting skills soon.

Thanks for the info!!!