SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    matching phrases

    Let's say I have two sentences of which I want to extract the phrases in common:
    The dog ate my homework.
    The cat ate my homework.

    Match found: ate my homework.

    I guess it involves a for loop but what else?
    Compare bible texts (and other tools):
    TheWheelofGod

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    The simplest method would be to split each phrase up, delimited by the space, and then go through a double loop to compare the words to each other, and each time you find a common one, add it to a composite string.

    Here is a simple example: http://www.arwebdesign.net/test/common_phrases.html

    It has much room for improvement, but you get the idea.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What should the results be here?

    The dog ate my homework.
    The cat ate my HOMEWORK.
    ?

    The big red dog ate my homework.
    The big red cat ate my homework.
    ?

    The big red dog ate my home.
    The big red cat ate my homework.
    ?

  4. #4
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samanime View Post
    The simplest method would be to split each phrase up, delimited by the space, and then go through a double loop to compare the words to each other, and each time you find a common one, add it to a composite string.

    Here is a simple example: http://www.arwebdesign.net/test/common_phrases.html

    It has much room for improvement, but you get the idea.
    I'm working on your given code. I added a small sentence at the end.
    Is there any way that the code would render a " + " at the end of each phrase match? Like this:
    There is a book on the + Hey there
    Compare bible texts (and other tools):
    TheWheelofGod

  5. #5
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    What should the results be here?

    The dog ate my homework.
    The cat ate my HOMEWORK.
    ?

    The big red dog ate my homework.
    The big red cat ate my homework.
    ?

    The big red dog ate my home.
    The big red cat ate my homework.
    ?
    Anything no matter what caps as long it picks up the pattern but I figured it out:
    Code JavaScript:
    	// The phrases
    	var phrase1 = document.getElementById('phrase1').innerHTML;
    	var phrase2 = document.getElementById('phrase2').innerHTML;
     
    	// First, break the first phrases up into an array.
    	var phrase1_split = phrase1.split(' ');
    	var phrase2_split = phrase2.split(' ');
     
    	// Our common phrase goes here.
    	var common_phrase = "";
     
    	// Now go through a double-loop and match each common word.
    	for(var i=0; i < phrase1_split.length; i++){
    		for(var j=0; j < phrase2_split.length; j++){
    			if(phrase1_split[i] == phrase2_split[j]){
    				common_phrase += phrase1_split[i] + " ";
    				// Move it forward to the next word in the phrase1.
    				i++;
     
    			}
     
    		}
    		if(i!=phrase1_split.length-1){
    		common_phrase += "+ ";
    		}
    	}
    	// Clean up the extra space at the end.
    	common_phrase = common_phrase.substr(0, common_phrase.length-1);
    	document.getElementById('result').innerHTML=common_phrase;
    }
    Compare bible texts (and other tools):
    TheWheelofGod

  6. #6
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I have a problem. A dilemma.
    Code HTML4Strict:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Javascript - Common Phrases</title>
    <script type="text/javascript">
    function common_phrases() {
    	// The phrases
    	var phrase1 = document.getElementById('phrase1').innerHTML;
    	var phrase2 = document.getElementById('phrase2').innerHTML;
     
    	// First, break the first phrases up into an array.
    	var phrase1_split = phrase1.split(' ');
    	var phrase2_split = phrase2.split(' ');
     
    	// Our common phrase goes here.
    	var common_phrase = "";
     
    	// Now go through a double-loop and match each common word.
    	for(var i=0; i < phrase1_split.length; i++){
    		for(var j=0; j < phrase2_split.length; j++){
    			if(phrase1_split[i] == phrase2_split[j]){
    				common_phrase += phrase1_split[i] + " ";
    				// Move it forward to the next word in the phrase1.
    				i++;
     
    			}
     
    		}
    		if(i!=phrase1_split.length-1){
    		common_phrase += "+ ";
    		}
    	}
    	// Clean up the extra space at the end.
    	common_phrase = common_phrase.substr(0, common_phrase.length-1);
    	document.getElementById('result').innerHTML=common_phrase;
    }
    </script>
    </head>
     
    <body>
    <p>The phrases used are:<br>
    	<span id="phrase1">There is a book on the shelf. Hey there dog! Get a life.</span><br />
    	<span id="phrase2">There is a book on the table. Hey there cat! Find a book.</span><br />
    	<span id="result"></span><br />
     
    </p>
     
    <p><input type="button" value="Click me to show common phrase" onclick="common_phrases()"></p>
    </body>
    </html>
    The smaller the words the bigger this problem. Whenever it finds the small word a alone (not in the example above though) it has to be left out because:
    1. it slows down the computer;
    2. it breaks the coding such as <span> becomes <sp<span>a</span>> messing the code.
    Compare bible texts (and other tools):
    TheWheelofGod

  7. #7
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I spent a good number of hours of trying to figure out on how to skip the smallest words except when it's part of a common phrase between the two strings. This is what I have so far:
    Code javascript:
    function getPhrases(){
    	var searchChapWords=document.getElementsByName('search_chap_words'); //grabs from the 1st column
    	var searchChapWords2=document.getElementsByName('search_chap_words2'); //grabs from the 2nd column
    	document.getElementById('keyword0').innerHTML = "";
    	document.getElementById('keyword1').innerHTML = "";
     
    	var myOldString = "";
    	var myOldString2 = "";
    	for(i=0;i<searchChapWords.length;i++){
    		myOldString += searchChapWords[i].innerHTML + " "; //joins 1st column in one string
    	}
    	for(i=0;i<searchChapWords2.length;i++){
    		myOldString2 += searchChapWords2[i].innerHTML + " "; //joins 2nd column in one string
    	}
    	var myNewString = myOldString.replace(/[\:\;\?\.\,\(\)\']/gi, "");//to eliminate the punctuations
    	var myNewString2 = myOldString2.replace(/[\:\;\?\.\,\(\)\']/gi, "");//to eliminate the punctuations
     
    	var mySplitString = myNewString.split(" ");
    	var mySplitString2 = myNewString2.split(" ");
     
    	// Our common phrase goes here.
    	var common_phrase = "";
    	var phraseCollection =  new Array();
    	// Now go through a double-loop and match each common word.
    	//for(k=0; k<15; k++){
    		k=0;
    		phraseCollection[k] = "";
    		for(var i=0; i < mySplitString.length; i++){ //length of 1st array
    			if(i!=0){
    				//common_phrase += " ";
    				phraseCollection[k] += " ";
    			}
    			for(var j=0; j < mySplitString2.length; j++){ //picks a word and runs through 2nd array
    				if(mySplitString[i] == mySplitString2[j]){ //when match found
    					phraseCollection[k] += mySplitString[i]; //records word
    					/*
    					//common_phrase += mySplitString[i];
    					//common_phrase += " ";
    					if(j!=mySplitString2.length-1){
    						phraseCollection[k] += " ";
    						//common_phrase += " ";
    					}
    					*/
    					// Move it forward to the next word in the phrase1.
    					i++; // goes to the next word
    				}else{
    					k++;//skips to the next array element
    					i++;// goes to the next word
    				} 
    			}
    			/*
    			if(i!=mySplitString.length-1){
    				phraseCollection[k] += ",,";
    				//common_phrase += ",,";
    			}
    			*/
    		}
    	//}
    	for(k=0; k<phraseCollection.length; k++){
    		common_phrase += phraseCollection[k] + ",,";
    	}
    	// Clean up the extra space at the end.
    	common_phrase = common_phrase.substr(0, common_phrase.length-1);
    	document.getElementById('keyword0').innerHTML=common_phrase;
     
    }
    What I thought of doing is to create an array of phraseCollection and whatever is a good find, a match, would be part of this array. But I'm getting undefined as an answer.
    Last edited by gilgalbiblewheel; Nov 14, 2008 at 00:48.
    Compare bible texts (and other tools):
    TheWheelofGod

  8. #8
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok 1st column:
    Code HTML4Strict:
    <div id="notForForum" style="border: thin dashed ; position: absolute; left: -30px; display: block; margin-left: 2em; overflow-y: scroll; overflow-x: hidden; height: 300px; width: 300px; padding-right: 5px;">
    	 <span style="padding: 5px; background-color: rgb(235, 255, 255); font-weight: bold; width: 300px; display: block;">2 Chronicles 16
    </span><br><span id="regular[]" style="padding: 5px; font-weight: bold;">1</span>
    <span name="search_chap_words" id="14_16_1" style="">In the six and thirtieth year of the reign of Asa Baasha king of Israel came up against Judah, and built Ramah, to the intent that he might let none go out or come in to Asa king of Judah.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">2</span>
    <span name="search_chap_words" id="14_16_2" style="">Then Asa brought out silver and gold out of the treasures of the house of the LORD and of the king's house, and sent to Benhadad king of Syria, that dwelt at Damascus, saying,
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">3</span>
    <span name="search_chap_words" id="14_16_3" style="">There is a league between me and thee, as there was between my father and thy father: behold, I have sent thee silver and gold; go, break thy league with Baasha king of Israel, that he may depart from me.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">4</span>
     
    <span name="search_chap_words" id="14_16_4" style="">And Benhadad hearkened unto king Asa, and sent the captains of his armies against the cities of Israel; and they smote Ijon, and Dan, and Abelmaim, and all the store cities of Naphtali.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">5</span>
    <span name="search_chap_words" id="14_16_5" style="">And it came to pass, when Baasha heard it, that he left off building of Ramah, and let his work cease.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">6</span>
    <span name="search_chap_words" id="14_16_6" style="">Then Asa the king took all Judah; and they carried away the stones of Ramah, and the timber thereof, wherewith Baasha was building; and he built therewith Geba and Mizpah.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">7</span>
    <span name="search_chap_words" id="14_16_7" style="">And at that time Hanani the seer came to Asa king of Judah, and said unto him, Because thou hast relied on the king of Syria, and not relied on the LORD thy God, therefore is the host of the king of Syria escaped out of thine hand.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">8</span>
    <span name="search_chap_words" id="14_16_8" style="">Were not the Ethiopians and the Lubims a huge host, with very many chariots and horsemen? yet, because thou didst rely on the LORD, he delivered them into thine hand.
     
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">9</span>
    <span name="search_chap_words" id="14_16_9" style="">For the eyes of the LORD run to and fro throughout the whole earth, to show himself strong in the behalf of them whose heart is perfect toward him. Herein thou hast done foolishly: therefore from henceforth thou shalt have wars.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">10</span>
    <span name="search_chap_words" id="14_16_10" style="">Then Asa was wroth with the seer, and put him in a prison house; for he was in a rage with him because of this thing. And Asa oppressed some of the people the same time.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">11</span>
    <span name="search_chap_words" id="14_16_11" style="">And, behold, the acts of Asa, first and last, lo, they are written in the book of the kings of Judah and Israel.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">12</span>
    <span name="search_chap_words" id="14_16_12" style="">And Asa in the thirty and ninth year of his reign was diseased in his feet, until his disease was exceeding great: yet in his disease he sought not to the LORD, but to the physicians.
    </span><br>
     
    <span id="regular[]" style="padding: 5px; font-weight: bold;">13</span>
    <span name="search_chap_words" id="14_16_13" style="">And Asa slept with his fathers, and died in the one and fortieth year of his reign.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">14</span>
    <span name="search_chap_words" id="14_16_14" style="">And they buried him in his own sepulchres, which he had made for himself in the city of David, and laid him in the bed which was filled with sweet odorous and divers kinds of spices prepared by the apothecaries' art: and they made a very great burning for him.
    </span><br>
    </div>
    Last edited by gilgalbiblewheel; Nov 14, 2008 at 00:55.
    Compare bible texts (and other tools):
    TheWheelofGod

  9. #9
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    2nd column:
    Code HTML4Strict:
    <div id="notForForum2" style="border: thin dashed ; position: absolute; left: -30px; display: block; margin-left: 2em; overflow-y: scroll; overflow-x: hidden; height: 300px; width: 300px; padding-right: 5px;">
    	 <span style="padding: 5px; background-color: rgb(235, 255, 255); font-weight: bold; width: 300px; display: block;">Zechariah 4
    </span><br><span id="regular[]" style="padding: 5px; font-weight: bold;">1</span>
    <span name="search_chap_words2" id="38_4_1" style="">And the angel that talked with me came again, and waked me, as a man that is wakened out of his sleep.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">2</span>
    <span name="search_chap_words2" id="38_4_2" style="">And said unto me, What seest thou? And I said, I have looked, and behold a candlestick all of gold, with a bowl upon the top of it, and his seven lamps thereon, and seven pipes to the seven lamps, which are upon the top thereof:
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">3</span>
    <span name="search_chap_words2" id="38_4_3" style="">And two olive trees by it, one upon the right side of the bowl, and the other upon the left side thereof.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">4</span>
     
    <span name="search_chap_words2" id="38_4_4" style="">So I answered and spake to the angel that talked with me, saying, What are these, my lord?
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">5</span>
    <span name="search_chap_words2" id="38_4_5" style="">Then the angel that talked with me answered and said unto me, Knowest thou not what these be? And I said, No, my lord.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">6</span>
    <span name="search_chap_words2" id="38_4_6" style="">Then he answered and spake unto me, saying, This is the word of the LORD unto Zerubbabel, saying, Not by might, nor by power, but by my spirit, saith the LORD of hosts.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">7</span>
    <span name="search_chap_words2" id="38_4_7" style="">Who art thou, O great mountain? before Zerubbabel thou shalt become a plain: and he shall bring forth the headstone thereof with shoutings, crying, Grace, grace unto it.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">8</span>
    <span name="search_chap_words2" id="38_4_8" style="">Moreover the word of the LORD came unto me, saying,
     
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">9</span>
    <span name="search_chap_words2" id="38_4_9" style="">The hands of Zerubbabel have laid the foundation of this house; his hands shall also finish it; and thou shalt know that the LORD of hosts hath sent me unto you.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">10</span>
    <span name="search_chap_words2" id="38_4_10" style="">For who hath despised the day of small things? for they shall rejoice, and shall see the plummet in the hand of Zerubbabel with those seven; they are the eyes of the LORD, which run to and fro through the whole earth.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">11</span>
    <span name="search_chap_words2" id="38_4_11" style="">Then answered I, and said unto him, What are these two olive trees upon the right side of the candlestick and upon the left side thereof?
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">12</span>
    <span name="search_chap_words2" id="38_4_12" style="">And I answered again, and said unto him, What be these two olive branches which through the two golden pipes empty the golden oil out of themselves?
    </span><br>
     
    <span id="regular[]" style="padding: 5px; font-weight: bold;">13</span>
    <span name="search_chap_words2" id="38_4_13" style="">And he answered me and said, Knowest thou not what these be? And I said, No, my lord.
    </span><br>
    <span id="regular[]" style="padding: 5px; font-weight: bold;">14</span>
    <span name="search_chap_words2" id="38_4_14" style="">Then said he, These are the two anointed ones, that stand by the LORD of the whole earth.
    </span><br>
    </div>
    Last edited by gilgalbiblewheel; Nov 14, 2008 at 00:57.
    Compare bible texts (and other tools):
    TheWheelofGod

  10. #10
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look for name="search_chap_words" and name="search_chap_words2".
    Compare bible texts (and other tools):
    TheWheelofGod

  11. #11
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess you don't understand what I'm doing. I want to grab all the name="search_chap_words" join them together then break it down to words and search it into name="search_chap_words2".

    The problem comes afterward. If the search found only small words like a or the it would be insignificant. But if the a or the the is accompanied with other words in common then it should be included.

    What I had in mind is to create another array which would include all the phrases the search has found.
    Code "javascript":
    function getPhrases(){
        var searchChapWords=document.getElementsByName('search_chap_words'); //grabs from the 1st column
        var searchChapWords2=document.getElementsByName('search_chap_words2'); //grabs from the 2nd column
        document.getElementById('keyword0').innerHTML = "";
        document.getElementById('keyword1').innerHTML = "";
     
        var myOldString = "";
        var myOldString2 = "";
        for(i=0;i<searchChapWords.length;i++){
            myOldString += searchChapWords[i].innerHTML + " "; //joins 1st column in one string
        }
        for(i=0;i<searchChapWords2.length;i++){
            myOldString2 += searchChapWords2[i].innerHTML + " "; //joins 2nd column in one string
        }
        var myNewString = myOldString.replace(/[\:\;\?\.\,\(\)\']/gi, "");//to eliminate the punctuations
        var myNewString2 = myOldString2.replace(/[\:\;\?\.\,\(\)\']/gi, "");//to eliminate the punctuations
     
        var mySplitString = myNewString.split(" ");
        var mySplitString2 = myNewString2.split(" ");
     
        // Our common phrase goes here.
        var common_phrase = "";
        var phraseCollection =  new Array();
        // Now go through a double-loop and match each common word.
        //for(k=0; k<15; k++){
            k=0;
            phraseCollection[k] = "";
            for(var i=0; i < mySplitString.length; i++){ //length of 1st array
                if(i!=0){
                    //common_phrase += " ";
                    phraseCollection[k] += " ";
                }
                for(var j=0; j < mySplitString2.length; j++){ //picks a word and runs through 2nd array
                    if(mySplitString[i] == mySplitString2[j]){ //when match found
                        phraseCollection[k] += mySplitString[i]; //records word
                        /*
                        //common_phrase += mySplitString[i];
                        //common_phrase += " ";
                        if(j!=mySplitString2.length-1){
                            phraseCollection[k] += " ";
                            //common_phrase += " ";
                        }
                        */
                        // Move it forward to the next word in the phrase1.
                        i++; // goes to the next word
                    }else{
                        k++;//skips to the next array element
                        i++;// goes to the next word
                    }
                }
                /*
                if(i!=mySplitString.length-1){
                    phraseCollection[k] += ",,";
                    //common_phrase += ",,";
                }
                */
            }
        //}
        for(k=0; k<phraseCollection.length; k++){
            common_phrase += phraseCollection[k] + ",,";
        }
        // Clean up the extra space at the end.
        common_phrase = common_phrase.substr(0, common_phrase.length-1);
        document.getElementById('keyword0').innerHTML=common_phrase;
     
    }
    Instead of //common_phrase I have the array phraseCollection[k]. But this method gives error. Is there way to make phraseCollection[k] work in the for loop?
    Last edited by gilgalbiblewheel; Nov 14, 2008 at 01:01.
    Compare bible texts (and other tools):
    TheWheelofGod


Bookmarks

Posting Permissions

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