aspose file tools*
The moose likes Beginning Java and the fly likes match the starting word Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "match the starting word " Watch "match the starting word " New topic
Author

match the starting word

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
hi all,

Good morning .happy week end .. I have a pattern matching code to match the particular word from the sentence. In this i am getting result . But the problem is i am getting the result of words that is even in the middle.

code is


input text are reader.txt
Guindy National Park is located in Chennai, South India, one of the last remnants of tropical dry evergreen forest of the Coromandel Coast, Guindy Park was originally a game reserve.

next input of 2.txt

located
biosphere
reserve


The current output is
Number of times Tim is: located
Number of times Tim is: serve

the expected output is
Number of times Tim is: located

i want to match only when all the words are there from the 2.txt. if it starts from the middle, It should not match. How to perform that?
Help needed!


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8418
    
  23

deepika deepi wrote:i want to match only when all the words are there from the 2.txt. if it starts from the middle, It should not match. How to perform that?
Help needed!

That's because your Matcher is not checking for whole words.

Personally, I reckon you're way overthinking this. If I was writing a "word matcher", I wouldn't use regexes at all.

How about this?
1. Read in your text.
2. Break it up into words (see String.split()).
3. Put each of the words into a List - let's call it textWords.
3. Read in your "words to find" and put each word into a List (or Set, depending on how you want to do the match) - let's call that one wordsToFind.
4. For each word in wordsToFind, see if textWords contains that word. If it does, remove it from both Lists.
5. Now check if there are any words left in wordsToFind. If there are, then your match failed. Indeed, your match can fail as soon as it discovers a word that is NOT in textWords.

You could get a lot fancier with it, but right now concentrate on solving the problem.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
i am trying to use an boundary match in this line.



But is does not show any change in the output. for the value given directly it matches.If i use this code it gives the result



How to match for the string instead of key words directly that match with the sentence?

I also will try to do try do you Winston Gutkowski.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8418
    
  23

deepika deepi wrote:How to match for the string instead of key words directly that match with the sentence?

You need to read in your "words to find" and generate a string that looks exactly like your 2nd regex1 string above - and personally, I'd use a StringBuilder, not a String; but it's not essential.

Winston
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
Winston Gutkowski wrote:
deepika deepi wrote:i want to match only when all the words are there from the 2.txt. if it starts from the middle, It should not match. How to perform that?
Help needed!

That's because your Matcher is not checking for whole words.

Personally, I reckon you're way overthinking this. If I was writing a "word matcher", I wouldn't use regexes at all.

How about this?
1. Read in your text.
2. Break it up into words (see String.split()).
3. Put each of the words into a List - let's call it textWords.
3. Read in your "words to find" and put each word into a List (or Set, depending on how you want to do the match) - let's call that one wordsToFind.
4. For each word in wordsToFind, see if textWords contains that word. If it does, remove it from both Lists.
5. Now check if there are any words left in wordsToFind. If there are, then your match failed. Indeed, your match can fail as soon as it discovers a word that is NOT in textWords.

You could get a lot fancier with it, but right now concentrate on solving the problem.

Winston


I have split the sentence using



i am trying to use that word for matching


But getting an error:
IEinitial.java:438: cannot find symbol
symbol : variable word
location: class IEinitial
Matcher matcher1 = pattern1.matcher(word);


How to use that word for matching and get the result?
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 193

Whenever you get error of "can not find symbol" check if you have declared the variable which is causing the trouble, or the variable that you are using should be in the scope.
from you code I can see that the "word" is declared inside the for loop, thats why this will not be available outside for loop.


Do not wait to strike till the iron is hot; but make it hot by striking....
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
Manoj Kumar Jain wrote:Whenever you get error of "can not find symbol" check if you have declared the variable which is causing the trouble, or the variable that you are using should be in the scope.
from you code I can see that the "word" is declared inside the for loop, thats why this will not be available outside for loop.


ya. how to make the word available inside the loop use it outside the loop ?
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

deepika deepi wrote:
I have split the sentence using



i am trying to use that word for matching


But getting an error:
IEinitial.java:438: cannot find symbol
symbol : variable word
location: class IEinitial
Matcher matcher1 = pattern1.matcher(word);


Where you are writing the code for matching the word
I hope not outside your for loop
Consider your code. It is iterating over the String Array and printing each word..
And, if you want to match anything with each of these words,, you need to do that inside that loop only.. Because there's where the variable will be visible..

Deepika, You really need to re-learn the basics of Variable Scope and lifetime.
And I personaly suggest you to write algorithm for whatever problem you have to solve... Go step by step..
And when you are done with the algorithm, go and implement it step by step (One logic at a time)..

Unless the problem domain and logic is not clear in your mind, you may take hours to solve a problem that, you will realise later, just needed few lines of code..

So, think upon it..
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
ya. i have changed the loop corrected. But i could not see any change in the output even after the split of string into words



can any one tell how to get the result . and why i am not getting ? what i am missing ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: match the starting word