Regexes for three column data to create a dictionary


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regexes for three column data to create a dictionary
# 1  
Old 04-23-2016
Regexes for three column data to create a dictionary

I am working on a multilingual dictionary and I have data in three columns. The data structure can be
Code:
word=word=gloss

or
Code:
word word=word word=gloss gloss

Code:
=

acts as a delimiter
The number of words separated by the delimiter can be up to 8 or 10. The structure is well defined in the sense that the number of words in the first column and the number of words in the second column are identical
An example will make this clear. For ease of comprehension I am using Latin script:
Code:
book=boook=bUk

Code:
hand book=hannd boook=hEnD bUk

and so on.
I need to map the gloss in column3 to the string in column1 and the string in column 2
Code:
book=bUK
boook=bUK

Code:
hand book=hEnD bUk
hannd boook=hEnD bUk

My query is how do I write a regex which will identify each of these types. Once I have the regex, I can write a script which will easily separate these out. I would appreciate a regex in Perl or Unix.
A script in either Perl or Awk would be the cherry on the cake. I work in a Windows environment
I hope to complete the mapper and put it up as a useful tool for multi-lingual transliteration across two languages. Many thanks.
# 2  
Old 04-23-2016
perl does hashes really well. Is there a reason to use regexes? Once the hash is built in memory you could serialize it to disk, or more importantly you could use the hashtable instead of searching your file for each lookup. This will only benefit you if the file has a reasonably large number of lines. > few thousand

FWIW: how does your translator handle homographs? In English:lead a horse to water, the metal was as heavy as lead
# 3  
Old 04-23-2016
Thanks for your comment. Insofar as homographs are concerned the gloss handles these as separate entries.
I am not very good at Perl and prefer to use Awk. If I am not asking too much how can this be handled in Perl.
# 4  
Old 04-23-2016
awk is fine - associative arrays are hashes.

However, most translators have lots of work to do, so however you do this you need to keep the hash in memory if performance is a criterion. So you may need to read through a bunch of "smallfile" with one read of bigfile -I don't know

You will need two field separators the "=" character and (for english tab and space).
The other possible field sep characters depend on your language.
files: bigfile (one with = separator) smallfile text to translate
Code:
awk 'FNR==NR {FS="="; one[$3]=$1 ; two[$3]=$2; next}  # lookup will be gloss
       FNR!=NR { FS="[ \t]"; $ do your lookup here} 
       ' bigfile smallfile

I do not understand enough to give you a good answer. Some points:
1. the 'do your lookup here' part is simply an associative array lookup,
read $0 from smallfile, which should have the gloss, right?
Code:
print one[$0]  #- prints the first field in the bigfile

2. you will probably need to create an awk function to lookup one[] and another for two[]
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 04-23-2016
Thanks a lot for your help with the script. You have grabbed what I have in mind. I will try and take on from here and suitably modify the script to accommodate other cases.
# 6  
Old 04-23-2016
Looking at the samples in post#1, I'm not sure if the distinction between word1 and word2 (resulting in two arrays) is really needed. Wouldn't
Code:
awk '
FNR==NR {TR[$1]=$3              # gloss indexed by the two words
         TR[$2]=$3
         next
        }
        {                       # whatever you need to do to your text
        }
' FS="=" transfile FS="..." text

do what you want?
This User Gave Thanks to RudiC For This Post:
# 7  
Old 04-23-2016
Sorry for the late reply. I was out and could not access my mail. Many thanks for the answer.
I see your point that the distinction between word1 and word2 may not be needed.
I will test it out and get back to you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

2. Shell Programming and Scripting

Script to create unique look-up for headers for a Dictionary

I have a text file in UTF-8 format which has the following data structure HEADWORD=gloss1,gloss2,gloss3 etc I want to convert it so that all the glosses of the HeadWord appear on separate lines HEADWORD=gloss1 HEADWORD=gloss2 HEADWORD=gloss3 An example will illustrate the requirement... (4 Replies)
Discussion started by: gimley
4 Replies

3. Shell Programming and Scripting

Compare 2 files and match column data and align data from 3 column

Hello experts, Please help me in achieving this in an easier way possible. I have 2 csv files with following data: File1 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012... (5 Replies)
Discussion started by: asnandhakumar
5 Replies

4. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

5. UNIX for Dummies Questions & Answers

What's the Diff Between These Two Regexes?

Trying to understand what's happening here, but I cannot figure it out. I'm reading Mastering Regular Expressions, by Friedl, and he uses this as an example of how to grab quoted text: egrep -o '"*"' ~/File.txt ...should pull in any quoted phrases. Match a literal double-quote, match anything... (11 Replies)
Discussion started by: sudon't
11 Replies

6. Homework & Coursework Questions

How to create a dictionary using cygwin

1. The problem statement, all variables and given/known data: Create a dictionary using cygwin. Display the following menu at the start of execution 1-add a word in the dictionary # specify the meaning 2-search a word # if word exists, show the meaning of the word 2-delete a word... (2 Replies)
Discussion started by: kpopfreakghecky
2 Replies

7. Shell Programming and Scripting

create a new file from data file from a column

I have a data file that has a list of data macthing by user. I am able to sort by user and there is multiple rows for each user. Ideally I would like to email only the user of the files they own. Would it be best to create a seperate file by user and all rows showing the files they own? (9 Replies)
Discussion started by: mykey242
9 Replies

8. Programming

How to create java based dictionary for mobile using data in microsoft excel?

i am having a ms excel file which contains 2 columns, I first column i added words, and in second column meaning to the word in the first column. I want to create a dictionary for mobile like nokia or any java based application running mobile. How it can be created as i, dont know the java... (1 Reply)
Discussion started by: Anna Hussie
1 Replies

9. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies

10. Shell Programming and Scripting

Question about working with data to create new column

Hello, I am having a problem with the script I am using to create a column from two columns I have in my file. I am needing to take column 5 and subtract it from column 2 to create column 6. I have included the script I am using and the rawdata I am using. Raw Data File:... (4 Replies)
Discussion started by: scottzx7rr
4 Replies
Login or Register to Ask a Question