문제

I have a large text file with phrases such as:

citybred JJ 
Brestowe NNP 
STARS NNP NNS
negative JJ NN
investors NNS NNPS
mountain NN 

My objective is to keep the first word of each line, without the spaces, and also make them lowercase. EX:

citybred 
brestowe
stars
negative
investors
mountain

Would be returned if the above text was evaluated.

Any help?

Current code:

public class FileLinkList
{
    public static void main(String args[])throws IOException{
        String content = new String();
        File file = new File("abc.txt");
        LinkedList<String> list = new LinkedList<String>();

        try {
            Scanner sc = new Scanner(new FileInputStream(file));
            while (sc.hasNextLine()){
                content = sc.nextLine();
                list.add(content);
            }
            sc.close();
        } catch(FileNotFoundException fnf){
            fnf.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("\nProgram terminated Safely...");
        }

        Collections.reverse(list);
        Iterator i = list.iterator();
        while (i.hasNext()) {
            System.out.print("Node " + (count++) + " : ");
            System.out.println(i.next());
        }
    }
}
도움이 되었습니까?

해결책

If your token and its POS tag is separated by space :

public class FileLinkList{

    public static void main(String[] args) {

        BufferedReader br = null;
            LinkedList<String> list = new LinkedList<String>();
            String word;
        try {
            String sCurrentLine;
            br = new BufferedReader(new FileReader("LEXICON.txt"));
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
                            word = sCurrentLine.trim().split(" ")[0];
                            list.add(word.toLowerCase());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                                br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

다른 팁

Add the following:

content = sc.nextLine();
string[] tokens = content.split(new char[] {' '}, StringSplitOptions.RemovEemptyEntries);
// You can add some validations here...
string word = tokens[0].ToLowerCase();

Try this :

public class FileLinkList {
    public static void main(String args[])throws IOException{
        String content = new String();
        int count=1;
        File file = new File("abc.txt");
        LinkedList<String> list = new LinkedList<String>();

        try {
            Scanner sc = new Scanner(new FileInputStream(file));
            while (sc.hasNextLine()){
                content = sc.nextLine();
                if (content != null && content.length() > 0)) {
                    list.add(content.trim().split(" ")[0].toLowerCase());
                }
            }
            sc.close();
        } catch(FileNotFoundException fnf){
            fnf.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("\nProgram terminated Safely...");
        }

        for (String listItem : list) {
            System.out.println(listItem);
        }
    }
}

With Apache Commons IO it is much simpler to read a file into a list of Strings.

import org.apache.commons.io.FileUtils;

List<String> lines = FileUtils.readLines(new File("abc.txt"));
List<String firstWords = new ArrayList<>();
for (String line : lines) {
  String firstWord = line.split(" ")[0].toLowerCase();
  firstWords.add(firstWord);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top