Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to apply rules of grammar?
Forum Updated to NodeBB v4.3 + New Features

How to apply rules of grammar?

Scheduled Pinned Locked Moved Unsolved General and Desktop
32 Posts 6 Posters 10.7k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • BlasterB Blaster

    Hello, I'm doing a text editor and I want to apply grammar rules to it. Any idea how to do it?
    I was thinking of loading a file with the rules so I could add rules in an easier way, but I can not think how to do it.
    Can anybody help me?

    VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by VRonin
    #18

    @Blaster said in How to apply rules of grammar?:

    I was thinking of loading a file with the rules

    without defining what the letter

    I'm confused now...

    let's say you have a UTF-8 text file (rules.txt) that contains:

    x
    xy
    

    You can use

    QString stringToTranslate("xyxyxx");
    QFile rulesFile("rules.txt");
    if(rulesFile.open(QFile::ReadOnly | QFile::Text)){
    QTextStream rulesStream(&rulesFile);
    rulesStream.setCodec("UTF-8");
    QString line;
    while (stream.readLineInto(&line))
    stringToTranslate.replace(QRegularExpression("(?:"+QRegularExpression::escape(line)+")+",line);
    }
    qDebug() << "Translated String: "+stringToTranslate;
    

    Is this what you wanted?

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    BlasterB 1 Reply Last reply
    1
    • VRoninV VRonin

      @Blaster said in How to apply rules of grammar?:

      I was thinking of loading a file with the rules

      without defining what the letter

      I'm confused now...

      let's say you have a UTF-8 text file (rules.txt) that contains:

      x
      xy
      

      You can use

      QString stringToTranslate("xyxyxx");
      QFile rulesFile("rules.txt");
      if(rulesFile.open(QFile::ReadOnly | QFile::Text)){
      QTextStream rulesStream(&rulesFile);
      rulesStream.setCodec("UTF-8");
      QString line;
      while (stream.readLineInto(&line))
      stringToTranslate.replace(QRegularExpression("(?:"+QRegularExpression::escape(line)+")+",line);
      }
      qDebug() << "Translated String: "+stringToTranslate;
      

      Is this what you wanted?

      BlasterB Offline
      BlasterB Offline
      Blaster
      wrote on last edited by
      #19

      @VRonin
      What I really want to know is how to make a regular expression that checks the multiple occurrence of any pattern, not including some specific character in the expression.
      Something like, for example

      "x{2,}"
      

      that would match any string containing 2 or more repeated characters regardless of the character

      VRoninV 1 Reply Last reply
      0
      • ? A Former User

        @Blaster https://en.wikipedia.org/wiki/Context-free_grammar, https://en.wikipedia.org/wiki/Backus–Naur_form

        BlasterB Offline
        BlasterB Offline
        Blaster
        wrote on last edited by
        #20

        @Wieland
        It is not grammar free of context, it is a gramática similar to the one of our languages

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #21

          Imagine the input is xxxyxy do you want the output to be xxy or xy?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          BlasterB 1 Reply Last reply
          0
          • VRoninV VRonin

            Imagine the input is xxxyxy do you want the output to be xxy or xy?

            BlasterB Offline
            BlasterB Offline
            Blaster
            wrote on last edited by
            #22

            @VRonin
            Just xy

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #23

              I know it's very inefficient but my brain can't optimise on Monday. At least it works:

              
              QString removeDuplicates(QString source){
                  for(int matchLen = source.size()/2; matchLen>0;--matchLen){
                      for(int startOfset=0;(2*matchLen)+startOfset<=source.size();){
                          if(source.midRef(startOfset,matchLen)==source.midRef(matchLen+startOfset,matchLen))
                              source.remove(matchLen+startOfset,matchLen);
                          else
                              ++startOfset;
                      }
                  }
                  return source;
              }
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              BlasterB 1 Reply Last reply
              0
              • VRoninV VRonin

                I know it's very inefficient but my brain can't optimise on Monday. At least it works:

                
                QString removeDuplicates(QString source){
                    for(int matchLen = source.size()/2; matchLen>0;--matchLen){
                        for(int startOfset=0;(2*matchLen)+startOfset<=source.size();){
                            if(source.midRef(startOfset,matchLen)==source.midRef(matchLen+startOfset,matchLen))
                                source.remove(matchLen+startOfset,matchLen);
                            else
                                ++startOfset;
                        }
                    }
                    return source;
                }
                
                BlasterB Offline
                BlasterB Offline
                Blaster
                wrote on last edited by
                #24

                @VRonin
                I appreciate the effort, but I need to avoid using functions and that's why I'm talking about regular expressions. The code before the last one helps a lot.

                VRoninV 1 Reply Last reply
                0
                • BlasterB Blaster

                  @VRonin
                  I appreciate the effort, but I need to avoid using functions and that's why I'm talking about regular expressions. The code before the last one helps a lot.

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #25

                  @Blaster said in How to apply rules of grammar?:

                  but I need to avoid using functions

                  I'm confused... can I ask why?

                  The code before the last one helps a lot

                  QString stringToTranslate("zxxxyxyxyxxzz");
                  
                  for(int matchLen = stringToTranslate.size()/2; matchLen>0;--matchLen){
                          for(int startOfset=0;(2*matchLen)+startOfset<=stringToTranslate.size();){
                              if(stringToTranslate.midRef(startOfset,matchLen)==stringToTranslate.midRef(matchLen+startOfset,matchLen))
                                  stringToTranslate.remove(matchLen+startOfset,matchLen);
                              else
                                  ++startOfset;
                          }
                      }
                  
                  qDebug() << "Translated String: "+stringToTranslate;
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  BlasterB 1 Reply Last reply
                  1
                  • VRoninV VRonin

                    @Blaster said in How to apply rules of grammar?:

                    but I need to avoid using functions

                    I'm confused... can I ask why?

                    The code before the last one helps a lot

                    QString stringToTranslate("zxxxyxyxyxxzz");
                    
                    for(int matchLen = stringToTranslate.size()/2; matchLen>0;--matchLen){
                            for(int startOfset=0;(2*matchLen)+startOfset<=stringToTranslate.size();){
                                if(stringToTranslate.midRef(startOfset,matchLen)==stringToTranslate.midRef(matchLen+startOfset,matchLen))
                                    stringToTranslate.remove(matchLen+startOfset,matchLen);
                                else
                                    ++startOfset;
                            }
                        }
                    
                    qDebug() << "Translated String: "+stringToTranslate;
                    
                    BlasterB Offline
                    BlasterB Offline
                    Blaster
                    wrote on last edited by
                    #26

                    @VRonin
                    That helps, because I can put in the file all the combinations and then apply them to the string

                    1 Reply Last reply
                    0
                    • BlasterB Blaster

                      @VRonin
                      What I really want to know is how to make a regular expression that checks the multiple occurrence of any pattern, not including some specific character in the expression.
                      Something like, for example

                      "x{2,}"
                      

                      that would match any string containing 2 or more repeated characters regardless of the character

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #27

                      @Blaster said in How to apply rules of grammar?:

                      how to make a regular expression that checks the multiple occurrence of any pattern

                      @Blaster said in How to apply rules of grammar?:

                      because I can put in the file all the combinations and then apply them to the string

                      I'm totally lost now. If somebody else has ideas please step forward

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      BlasterB 1 Reply Last reply
                      0
                      • VRoninV VRonin

                        @Blaster said in How to apply rules of grammar?:

                        how to make a regular expression that checks the multiple occurrence of any pattern

                        @Blaster said in How to apply rules of grammar?:

                        because I can put in the file all the combinations and then apply them to the string

                        I'm totally lost now. If somebody else has ideas please step forward

                        BlasterB Offline
                        BlasterB Offline
                        Blaster
                        wrote on last edited by
                        #28

                        @VRonin
                        Let's see, I am Cuban guy and my English is not very strong, I only defend a little.
                        I explain to you better so you understand.
                        I have an application in which it is necessary to apply certain rules, one of which I mentioned to you, that of the patterns. This application does not work only in a language and I need to make that the processing of the rules be independent of the language of the application, so I need the rules in a file to be able to make changes when you want to change in some rules or to be able to add a new rule without having to program again

                        1 Reply Last reply
                        0
                        • JKSHJ Offline
                          JKSHJ Offline
                          JKSH
                          Moderators
                          wrote on last edited by
                          #29

                          Hi @Blaster, I'll repeat @VRonin's question:

                          Why do you need to avoid using functions?

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          BlasterB 1 Reply Last reply
                          0
                          • VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by VRonin
                            #30

                            If I understand correctly you want a file containing all operations that should be done to the string (removing duplicates being just one of them).

                            If your rules can all be expressed in terms of regular expression replaces then it's fairly easy, for example the duplicates one would be: (.+)\1+

                            otherwise you'll need to use some form of syntax. probably the most straightforward way is to express operations in terms of ECMAScript (JavaScript) functions and use Qt Script to run them

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            BlasterB 1 Reply Last reply
                            3
                            • JKSHJ JKSH

                              Hi @Blaster, I'll repeat @VRonin's question:

                              Why do you need to avoid using functions?

                              BlasterB Offline
                              BlasterB Offline
                              Blaster
                              wrote on last edited by
                              #31

                              @JKSH said in How to apply rules of grammar?:

                              Hi @Blaster, I'll repeat @VRonin's question:

                              Why do you need to avoid using functions?

                              Because I will change the rules after finishing the application

                              1 Reply Last reply
                              0
                              • VRoninV VRonin

                                If I understand correctly you want a file containing all operations that should be done to the string (removing duplicates being just one of them).

                                If your rules can all be expressed in terms of regular expression replaces then it's fairly easy, for example the duplicates one would be: (.+)\1+

                                otherwise you'll need to use some form of syntax. probably the most straightforward way is to express operations in terms of ECMAScript (JavaScript) functions and use Qt Script to run them

                                BlasterB Offline
                                BlasterB Offline
                                Blaster
                                wrote on last edited by
                                #32

                                @VRonin said in How to apply rules of grammar?:

                                If I understand correctly you want a file containing all operations that should be done to the string (removing duplicates being just one of them).
                                If your rules can all be expressed in terms of regular expression replaces then it's fairly easy, for example the duplicates one would be: (.+)\1
                                otherwise you'll need to use some form of syntax. probably the most straightforward way is to express operations in terms of ECMAScript (JavaScript) functions and use Qt Script to run them

                                Yes, you understood correctly.
                                That's what I'm talking about.

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved