Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6383437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:38:58+00:00 2026-05-25T02:38:58+00:00

Let’s suppose I’ve got a text with morphological tags, and a similar one with

  • 0

Let’s suppose I’ve got a text with morphological tags, and a similar one with no tags at all. Both texts are merged in an interlinear fashion, one line below the other. Thus (additional carriage returns added for clarity’s sake):

The(Art) day(N) started(V) well(Adv), windy(Adj) and(C) humid(Adj),
here(Adv) in(P) London(PN),

The day was windy and quite humid here in London,

but(P) we(Pr) did(AuxV) not(Adv) mind(V), because(P) we(Pr) had(AuxV)
planned(V) to(P) stay(V) indoors(Adv)

but no problem at all, mate! We had planned to stay at home anyway!

Second line (i.e., the untagged text) is always preceded by a space and a tab.

Besides, punctuation and case-sensitivity can be safely ignored. Additionally, it could be the case that some words in the first line are not tagged.

So, from this kind of pseudo-code, and given that my knowledge of Perl is somewhat limited, I decide to build a series of regexes to extract the tags (always in parentheses) on line 1 and insert them on line 2, provided the words are the same.

My current code looks like this:

use strict;
use warnings;

while ( <DATA> )
{
s/(^\w+)(\(\w+\))?(.+\r)(\s\t)(\1)/$1$2$3$4$5$2/g; #Tag 1st word on line 2 (if it's the same one as the 1st on line 1).
s/(^\w+)(\(\w+\))?\s(\w+)(\(\w+\))?(.+\r)(\s\t)(\1\2)\s(\3)/$1$2 $3$4$5$6$7 $8$4/g; #Tag 2nd word on line 2 (if it's the same one as the 2nd on line 1).
# And so on...

print;
}


__DATA__
The(Art) day(N) started(V) well(Adv), windy(Adj) and(C) humid(Adj), here(Adv) in(P) London(PN),
    The day was windy and quite humid here in London, 
but(P) we(Pr) did(AuxV) not(Adv) mind(V), because(P) we(Pr) had(AuxV) planned(V) to(P) stay(V) indoors(Adv) 
   but no problem at all, mate! We had planned to stay at home anyway! 

Obviously, the output I’m trying to get would look as follows:

The(Art) day(N) started(V) well(Adv), windy(Adj) and(C) humid(Adj),
here(Adv) in(P) London(PN),
The(Art) day(N) was windy and quite humid(Adj) here(Adv) in(P)
London(PN),

but(P) we(Pr) did(AV) not(Adv) mind(V), because(P) we(Pr) had(AuxV)
planned(V) to(P) stay(V) indoors(Adv)

but(P) no problem at all, mate! We(Pr) had(AuxV) planned(V) to(P)
stay(V) at home anyway!


My problem is twofold:

a) The script above (where currently I’m trying to substitute just the first and second words), does not work, although I think the regexes are ok (I’ve tested them as Search/Replace in BBEdit).

b) I’m not at all sure this would be the right approach to tackle the task at hand (i.e., adding a whole series of increasingly longer and more complex regexes).

Could someone please tell me what I should do to get it to work, or, alternatively, show me a better way to optimize the task? I’m all ears!

Thank you very much indeed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:38:59+00:00Added an answer on May 25, 2026 at 2:38 am

    Something like this?

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my %tag;
    
    while (<DATA>)
    {
        if (m/\((Adj|Art|AuxV|C|N|PN|V)\)/) # it's an example
        {
            # Loop over tagged words; memorize tag for each
            while (m/(\w+)\((\w+)\)/g)
            {
                # If there were already some tags, add to existing
                $tag{$1} = (defined $tag{$1} ? "$tag{$1}|" : "") . $2;
            }
            print;
            next;
        }
        # else
        # Loop over all words; tag the ones we have a tag for
        s/(\w+)/defined $tag{$1} ? "$1($tag{$1})" : $1 /eg;
        print;
    
        # Flush tags for next iteration
        %tag = ();
    }
    

    Note the support for multiple example lines before the untagged line; and the support for multiple tags for a word.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's suppose I've got a 2D vector template class: template<typename T> class Vec2 {
Let's say, I've got a XmlNode: <A>1</A> How to remove a text from it,
Let's say you create a wizard in an HTML form. One button goes back,
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's say I have the string: hello world; some random text; foo; How could
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.