Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Comparing paragraphs of text

    I'm looking to compare one paragraph of text to another and output any words that don't appear in the original paragraph. Both are similar but one has additional words written into it and I just need to output these extra words if that makes sense.

    I know some javascript but I'm pretty unsure how to go about this. Guessing it would be along the lines of breaking the words down into strings and progressing from there... Or this may be much harder than I originally thought it might be.

    Any help with the general code or pointers would be much appreciated!!

    All the best,
    Max

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,448
    Thanks
    11
    Thanked 598 Times in 578 Posts
    Code:
    p1="I'm looking to compare one paragraph of text to another and output any words that don't appear in the original paragraph.\
     Both are similar but one has additional words written into it and I just need to output these extra words if that makes sense.";
    
    p2="I'm looking to Japan compare one paragraph of text to another and output any words that Egypt don't appear in the original paragraph.\
     Both Spain are similar but one has additional words written into it and I just need to output these extra Mexico words if that makes sense.";
    
    function unique(that){for(var b=[],e=that.length,a=0;a<e;a++)-1==b.indexOf(that[a])&&(b[b.length]=that[a]);return b};
    
    
    unique(p2.split(/\s+/)).filter(function(term){
      return this.indexOf(term)==-1;
    }, unique(p1.split(/\s+/))  );
    
    // produces: ["Japan", "Egypt", "Spain", "Mexico"]
    Last edited by rnd me; 02-18-2014 at 07:27 PM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • Users who have thanked rnd me for this post:

    MaxRyan (03-01-2014)

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks! that was an amazingly quick response! Sorry about being a bit late with the reply and thanks. My explanation of the problem wasn't that great I've realised. rather than there being words added in to the second paragraph it's more of a case of finding words which do not appear in the first Paragraph. The second will not necessarily have the same word order or amount of words in it although the greater part will be formed of words present in the first paragraph.

    As a gibberish example:

    P1-'the rain in spain falls mostly down the drain'
    P2-'spain mostly the cat down'

    so the only word I'd be looking to output there would be 'cat'.

    Is it simple to adapt that code to do so? or am I being blind and does it already do that?...

    Thanks!!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,252
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Quote Originally Posted by MaxRyan View Post
    Thanks! that was an amazingly quick response! Sorry about being a bit late with the reply and thanks. My explanation of the problem wasn't that great I've realised. rather than there being words added in to the second paragraph it's more of a case of finding words which do not appear in the first Paragraph. The second will not necessarily have the same word order or amount of words in it although the greater part will be formed of words present in the first paragraph.

    As a gibberish example:

    P1-'the rain in spain falls mostly down the drain'
    P2-'spain mostly the cat down'

    so the only word I'd be looking to output there would be 'cat'.

    Is it simple to adapt that code to do so? or am I being blind and does it already do that?...

    Thanks!!
    rnd me's code already does that.

    Code:
    <!DOCTYPE html>
    <head>
    </head>
    <body>
    
    <script type = "text/javascript">
    
    var p1 = 'The rain in Spain falls mostly down the drain';
    var p2 = 'Spain mostly the cat down';
    p1 = p1.toLowerCase();
    p2 = p2.toLowerCase();
    
    function unique(that){for(var b=[],e=that.length,a=0;a<e;a++)-1==b.indexOf(that[a])&&(b[b.length]=that[a]);return b};
    
    var x  = (unique(p2.split(/\s+/)).filter(function(term) {
      return this.indexOf(term)==-1;
    }, unique(p1.split(/\s+/))  ));
    
    alert ("The following words appear in p2 but not in p1:- " + x);
    
    </script>
    
    </body>
    </html>
    Last edited by Philip M; 03-01-2014 at 05:48 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    MaxRyan (03-02-2014)

  • #5
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    so it was me being an idiot... thanks!


  •  

    LinkBacks (?)

    1. 03-02-2014, 01:37 PM

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •