Confused about example of creating a formal grammar - grammar

I'm reading Dick Grune's Parsing Techniques - A Practical Guide and I'm stuck on an example of what a formal grammar is (starting page 22). He starts with this:
0. Name may be replaced by “tom”
Name may be replaced by “dick”
Name may be replaced by “harry”
1. Sentence may be replaced by Name
2. Sentence may be replaced by Sentence, Name
3. “, Name” at the end of a Sentence must be replaced by “and Name”
before Name is replaced by any of its replacements
4. a sentence is finished only when it no longer contains non-terminals
5. we start our replacement procedure with Sentence
Then he re-writes this more formally as follows:
0. Name -> tom
Name -> dick
Name -> harry
1. Sentence -> Name
Sentence -> List End
2. List -> Name
List -> List , Name
3. , Name End -> and Name
4. the start symbol is Sentence
The re-write above (figure 2.2 in the book) introduces "List". I assume List is a non-terminal, but I can't figure out where it comes from or what it means.
Also, unless I mis-read the text, figure 2.2 is supposed to be the same as the first figure, but they don't exactly match. For example, where is rule #2 from the first figure ("Sentence may be replaced by Sentence, Name") in fig. 2.2?

You didn't copy the table properly.
2 must be:
List -> Name
List -> List , Name
List can either be a single name or a list, a comma and a name.
e.g. "dick , tom and harry" is a valid sentence because we construct it the following way:
Sentence (Replace Sentence)
List End (List will be replaced with "List , Name")
List , Name End (Replace List with "List , Name" again)
List , Name , Name End (Now replace List with Name)
Name , Name , Name End (Replace ", Name End" with "and Name")
Name , Name and Name (Replace the Names)
dick , tom and harry
I hope this will answer your second question as well.

Related

How will you split a full name when his/her name consisting of 2 or more words from his/her last name in "PANDAS? Is there an easy way to do it?

I tried to concatenate the two columns firstname and lastname into one columns. Now, how about to split it into two columns when his/her firstname consisting of 2, 3 and more words from his/her lastname? Is there any easy way to do it? I tried using str.split() methods. But it says "columns should be the same length."
We can use str.extract here:
df[["firstname", "lastname"]] = df["fullname"].str.extract(r'^(\w+(?: \w+)*) (\w+)$')
The regex pattern used above assigns as many name words from the start as possible, leaving only the final component for the last name. Here is a formal explanation of the regex:
^ from the start of the name
( open first capture group \1
\w+ match the first word
(?: \w+)* then match space and another word, together zero or more times
) close first capture group
match a single space
(\w+) match and capture the last word as the last name in \2
$ end of the name

Finding element with similar id of 2 fields - text could be random, Selection should be based on text exists or not

Please see the image for better understanding
My search is by ID (RIN)
ID could be either Legal Name (Corporation client) or Last Name (Individual Client)
Below code works:
driver.findElement(By.xpath("//td[contains(#id,'l_Name')]")).click(); - This one will click on Legal Name (Working)
driver.findElement(By.xpath("//td[contains(#id,'Last_Name')]")).click(); - This works for Last Name if the search ID (RIN) is given for Last Name
driver.findElement(By.xpath("//td[contains(#id,'Name')]")).click(); - This one also works for Legal Name as the first element displayed is Legal Name But not working for Last Name
If I do
String S = driver.findElement(By.xpath("//td[contains(#id,'l_Name')]")).getText();
System.out.println(S);
KAB GIHADO CARTAGE INC. - String is displayed
But I want something like this:
driver.findElement(By.xpath("//td[contains(#id,'Name')]")) --- (#id,'Name')where driver identify Legal name or Last name based on if text exists or not - Text will be random based on the ID (RIN)
Here is the logic that will get the text from the field contains Name in it's ID and contains some text (not empty).
String S = driver.findElement(By.xpath("//td[contains(#id,'Name')][not(.='')]")).getText();
System.out.println(S);

access) How can I match the name in the column from a new input data?

I have a "Name" column with a format of "last name first name",
however if there is a different format of name column in a new data which should be matched to my original data, how can I make it to be matched automatically??
For example ,
I have :
Name
Jane Carr
Tony Kwon
and the new data has :
Name
Ms. Jane or Jane, Carr
Mr. Tony or Tony, Kwon
how can these two names can be automatically matched to my original data of Jane Carr and Tony Kwon?
I use 'like' to match the strings if the entire string is unknown in the where clause after the column name.
select * from table_name where name like '%jane carr%';
This is a wild card.
% before the string means - search all which has name ending with mentioned string.
% after the string means - search all which has name starting with mentioned string.
% before and after means - search all which has matching string.

split full arabic name into pieces(SQL)

in SQL Server:
i want to split full name like (محمد محمد وائل أبو العز) which is arabic name thats name found in one column, and i want to put first name محمد in column name (FirstName) and second name محمد in column name (SecondName), and third name وائل in column name (ThirdName), and Fourth name أبو العزin column name (FourthName)
put note that fourth name contsist of tow pieces and this problem may found in first or second or third name
what's your openion in this case
when you execute the following statement
SELECT FirstArabicName,
PARSENAME(REPLACE(FirstArabicName,' ','.'),4) 'FName' ,
PARSENAME(REPLACE(FirstArabicName,' ','.'),3) 'SName' ,
PARSENAME(REPLACE(FirstArabicName,' ','.'),2) 'TName' ,
PARSENAME(REPLACE(FirstArabicName,' ','.'),1) 'Sur Name'
FROM gfodatabase2013.dbo.Students
it works well with names that have one word only, but
you still have a problem with names that have two words like "Abd AlRahman"
how we can solve this problem?

Oracle RegExp For Counting Words Occuring After a Character

I want to identify the number of words occurring after a comma, in a full name field in Oracle database table.
The name field contains format of "LAST, FIRST MIDDLE"
Some names may have up to 4 to 5 names, such as "DOE, JOHN A B"
For example, if the Name field = 'WILLIAMS JR, HANK' it would output 1 (for 1 word occurring after the comma.
If the Name field = 'DOE, JOHN A B' i want it to output 3.
I would like to use a regexp_count function to determine this count.
I am using the following code to identify how many words exist in the field and would like to modify it to include this functionality:
REGEXP_COUNT(REPLACE(fieldname, ',',', '), '[^]+')
It would likely have to remove the replace function in order to find the comma, but this was the best I could do so far.
Help is much appreciated!
How about the following:
REGEXP_COUNT( fieldname, "\\w", INSTR(fieldname, ",")+1)
I have updated the code as follows, which appears to be working as desired:
REGEXP_COUNT(fieldname, '[^ ]+', (INSTR(fieldname, ',')+1))