Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: JAVA Text File Processing with REGEX

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JAVA Text File Processing with REGEX

    I am new in Java (some experience but not essential) and I am trying to build a small app that will do the following: Lets say that I have a list of names in a .txt file like the following:

    nick
    george
    john

    What I want to do now is to process each line depending on the letter the name ends. So for example the name "nick" ending in a K will be processed in a certain way. The same goes for the rest of the entries. Then I want to write onto a new file with the original entries in the first column and the processed names next to the original ones in the same line like the following:

    nick nick's nicolas
    george george's giorgio
    john john's johnny

    As I understand I will have to process each line match the regex for that name and write the entry with its variants in the new file. Then the next line etc... The problem is that while trying to find some stuff online I got really confused and lost.

    Any help will be more than welcome!

    Regards.

    nick


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,976
    Thanks
    52
    Thanked 2,270 Times in 2,242 Posts

    Default Re: JAVA Text File Processing with REGEX

    What have you done so far and where are you having problems?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA Text File Processing with REGEX

    Ι am trying to reach somewhere, when I do I will post the code!

    I have some code for reading the .txt file, some for the regex and some for writing to the output txt but the problem is that I cannot figure out the structure of the classes.
    I will first have to read the txt file line by line. Then each line must be check for the ending letters so to be processed accordingly (i.e. names that end in a "k" must be written in the output txt file with a certain way.

    So basically I will have to match each line with a regex and then store it in the output file.

    My problem is that I am loosing the feel of how to structure the code so to do the processing of each line and then continue to the next one.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,976
    Thanks
    52
    Thanked 2,270 Times in 2,242 Posts

    Default Re: JAVA Text File Processing with REGEX

    do the processing of each line and then continue to the next one.
    Use a loop.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA Text File Processing with REGEX

    Quote Originally Posted by Norm View Post
    Use a loop.
    Thank you for your answer.

    I got it to work but I am now facing an other problem.

    Lets say that we have a single entry (below) in the text file.

    -john

    My code can match the 2 last letters "hn" and then print in the console the original name and the nickname of that name as follows:

    -john johnny

    The problem is that while I can have multiple regex in my pattern :
    Pattern namePattern = Pattern.compile("hn$|ck$");

    I will have to treat each pattern differently because I need to replace the last letters with something different.

    How the hell am I going to do that? I will have the build the pattern/matcher/replace block for all the ending letter combos I need or there is a smarter way?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,976
    Thanks
    52
    Thanked 2,270 Times in 2,242 Posts

    Default Re: JAVA Text File Processing with REGEX

    Can you post a small complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA Text File Processing with REGEX

    	private static void printScannedLines(final List<String> scannedLines){
    	//matching kai printing edw
    		String replace1 = "άκη";
     
    			for(String item : scannedLines){
    				Pattern namePattern = Pattern.compile("ακης$|άκης$|ουλος$");
    				Matcher matcher = namePattern.matcher(item);
    				while(matcher.find()){
    				String item1 = item;
    				item1 = matcher.replaceAll(replace1);
    				System.out.println(item+"\t"+item1+"\t"+item1);
    				}
    			}


    RESULTS:
    Nominative Genitive Accusative
    παπαδάκης παπαδάκη παπαδάκη


    This is what I get as a result. Imagine that the "παπαδάκης" is a Greek Surname and then I need this surname in other forms (genitive, accusative)
    So I get the 3 forms of that surname as you can see above.
    The problem is that for an other surname with a different ending the formes must be replaces from other letters, for example the surname "παπαδόπουλος" has an ending of "ουλος" where in the other two forms the ending must be replaces by "ουλου" and "ούλο" for the genitive and accusative form respectively.

    So my problem is that I will have to match each surname with a specific regex and then replace the last letters with different letters in order to get the forms of that surname out.

    I am not sure If I made my self clear...

  8. #8
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: JAVA Text File Processing with REGEX

    Perhaps you need to explain what this is for and what you want to achieve. A computer program is only useful in this case if it saves repetitive manual actions. If there are 100's of exceptions that have to be coded, then perhaps you need to rethink the algorithm. {is this part of Natural Language Processing (NLP) ?}

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA Text File Processing with REGEX

    Yes it is,

    Its basically and algorithm I need in order to process Last names from a DB that I have in a txt file and then have them in all forms in order to achieve better recognition results. So I get a list of Last names that are in the nominative form and I want to have these names and their forms in an other file as seen below :

    Nominative genitive accusative
    name1 name1 name1
    name2 name2 name2

    The exceptions are about 10 or so.

  10. #10
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA Text File Processing with REGEX

    Any suggestions?

    As I can see with an if loop I will have to create a quite big number of pattern/matcher block and then have the exceptions that will replace the ending of the word for each one. So I guess there must be a more robust way to do things...I cannot figure out which is it though!


Similar Threads

  1. java file processing beginner level
    By candoa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 20th, 2012, 08:54 PM
  2. Reading from Text File and Processing Information
    By royalslim in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 13th, 2010, 02:27 PM
  3. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  4. Text Processing with Regular Expressions explained in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 6th, 2008, 02:03 AM
  5. Text Processing with Regular Expressions explained in Java
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: August 6th, 2008, 02:03 AM