Populate a DAT table with the content of two tables

Hello!

I’m a beginner in Touch but I am trying to create my school project on it. So, what i want to do is populate a table with the contet from two other tables, meaning:

table 1 would be made of words like:
apple
tree
car
plane
ground

I want to sort the words into separate tables. So if table 2 has “apple, orange, kiwi” listed as reference, then I want to pick the word “apple” from the first table and list in the the 2nd one

I want to repeat this with other tables and sort each word from the first table into separate ones

Hope I wasn’t too confusing,
thank you!

Hello,
Its difficult to understand your project, perhaps give an example with real table with the start and the result you want.
There is many tools to work with table, merge Dat, evaluate Dat etc. But more open way is to use Python to read the different cells, analyse the content and write the result in another table.

hello @jacqueshoepffner!

What i want to do is take a transcript of a speech and analyze the words used. I should categorize the words into groups and these groups have other words as a reference. I need to be able to analyze the transcript and see if a word used is found in the reference and if so, display it in a table. Overall I need to do this with 4 to 5 reference groups. I’ll try to simplify the above example and see if it helps,

Transcript table “apple,orange,kiwi,car,plane”
reference table 1 = “apple, orange, kiwi, banana” ; reference 2 = “car, plane, helicopter”.
analyzed final table1: fruit = “apple, orange, kiwi” ;
analyzed final table2: object = “car, plane”

Hello,
I made a quick project with your direction. Here is the Python code:

# table as var
tr = op('transcript')
r1 = op('ref1')
r2 = op('ref2')
res1 = op('res1')
res2 = op('res2')
# table as list
trLength = tr.numRows
trList = []
for i in range (0, trLength):
	trList.append(tr[i,0].val)
r1Length = r1.numRows
r1List = []
for i in range (0, r1Length):
	r1List.append(r1[i,0].val)
r2Length = r2.numRows
r2List = []
for i in range (0, r2Length):
	r2List.append(r2[i,0].val)
# compare lists and fill tables
res1.clear()
for i in trList:
	for j in r1List:
		if i == j:
			res1.appendRow(i)
res2.clear()
for i in trList:
	for j in r2List:
		if i == j:
			res2.appendRow(i)

Also the capture and the .toe.
If you doesn’t need to visualize the word in table you can store the word as list inside one operator, using store/fetch and using Examine Dat to visualize. For very long list of words, it can be faster.
Hope it helps.
parser.1.toe (4.4 KB)

Thank you very much for your time! this is really helpful!

Here’s a slightly different approach to the code that might scale up a bit more easily as the number of reference tables increases (my fruits and objects are your ref1 and ref2 tables):

def onTableChange(dat):
	# convert the column of cells into a list of values
	transcript = [word.val for word in dat.col(0)]
	fruits = [fruit.val for fruit in op('fruits').col(0)]
	objects = [object.val for object in op('objects').col(0)]

	# debug
	#print("transcript:", transcript)
	#print("fruits", fruits)
	#print("objects", objects)

	# make list of fruit and object words found in transcript
	fruitlist = list(filter(lambda word: word in fruits, transcript))
	objectlist = list(filter(lambda word: word in objects, transcript))

	# resize the dats that will receive new lists
	op('fruitlist').setSize(len(fruitlist),1)
	op('objectlist').setSize(len(objectlist),1)

	# and update the columns with the filtered lists
	op('fruitlist').replaceCol(0, fruitlist)
	op('objectlist').replaceCol(0, objectlist)

	return

I’m surprised that TD doesn’t have a Filter DAT. Is there a better way than those list comprehensions at the top to get a list of actual cell contents from a DAT?

Here’s a version which is using only nodes, so perhaps a bit easier to understand.
You can use the Select DAT to filter values. In the example it selects values based on the values it reads from the reference Table DATs
select_DAT_values_using_reference_table.tox (558 Bytes)

Thank you! for a beginner who doesn’t know how to code this is definitely handy!

Thank you @antoinedurr, I will surely scale it way more so this helps for sure!

That’s just too damn simple!

1 Like

@nettoyeur, bravo, so elegant…

So how/why can you use

op('reference1').col(0)

in a select DAT’s rownames field, but if I’m a DAT Execute op I have to do a list comprehension to turn that list of cells into a list of cell contents, e.g.

[fruit.val for fruit in op('fruits').col(0)]