matching phrases

Discussion in 'JavaScript' started by gilgalbiblewheel, Nov 8, 2008.

  1. #1
    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?
     
    gilgalbiblewheel, Nov 8, 2008 IP
  2. wayfarer07

    wayfarer07 Peon

    Messages:
    34
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    actually, the matchs there would be:
    The
    ate
    ate my
    my homework
    homework
    ate my homework

    Though there may be a way to do it with a regular expression (I'm not too good at them), the most straight forward way to do it would be to split() the strings by the spaces, so that each word becomes a single member in an array, then for each member in one array, loop over the other, and keep all matches in a third array. You'll have to work out the exact logic, because my head is feeling a bit fuzzy today :)
     
    wayfarer07, Nov 9, 2008 IP
  3. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    http://www.sitepoint.com/forums/showthread.php?t=581773
     
    MMJ, Nov 9, 2008 IP
  4. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    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:
    
    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;
    
    }
    
    Code (markup):
    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.
     
    gilgalbiblewheel, Nov 10, 2008 IP
  5. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    Here's the 1st column data:
    
    <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>
    
    Code (markup):
     
    gilgalbiblewheel, Nov 12, 2008 IP
  6. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #6
    Here's the 1st column data:
    
    <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>
    
    Code (markup):
     
    gilgalbiblewheel, Nov 12, 2008 IP
  7. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #7
    2nd column:
    
    <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>
    
    Code (markup):
     
    gilgalbiblewheel, Nov 12, 2008 IP
  8. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #8
    Look for name="search_chap_words" and name="search_chap_words2".
     
    gilgalbiblewheel, Nov 12, 2008 IP
  9. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Stop posting in 2 different forums, it isn't polite.
     
    MMJ, Nov 12, 2008 IP