简体   繁体   English

从文本文件 java 中读取某些单词

[英]Read Certain words from text file java

So i have a text file ( tagged.txt ) full of different sentences.所以我有一个充满不同句子的文本文件( tagged.txt )。 What I am looking to do is read this text file into Java, and extract specific words from this file.我要做的是将这个文本文件读入 Java,并从这个文件中提取特定的单词。

eg例如

Sentence : I went to the shop to buy a new pair of trainers一句话: I went to the shop to buy a new pair of trainers

If the sentence has the words shop , buy , or trainers in it, I would like that sentence to equal an opinion.如果句子中有单词shopbuytrainers ,我希望这句话等于一个意见。

I have created a list of the words in a text file ( words.txt ), in which I would like to stream against the tagged.txt file.我在文本文件 ( words.txt ) 中创建了一个单词列表,我想在其中对tagged.txt文件进行流式传输。

I just wanted to know if there is a way of implementing this in Java.我只是想知道是否有办法在 Java 中实现这一点。

  • Read the words from words.txt and store in an array and sort it using Arrays.sort(arr) .words.txt读取单词并存储在一个数组中,然后使用Arrays.sort(arr)对其进行Arrays.sort(arr)
  • Read the lines from tagged.txt line by line and store it in a String . tagged.txt读取tagged.txt的行并将其存储在String Then you could use split() to split the line into different words and store the words in an array.然后您可以使用split()将行拆分为不同的单词并将这些单词存储在数组中。
  • Then loop through the words of the line, and use the array's binarysearch method to search through the words from the words array.然后循环遍历该行的单词,并使用数组的binarysearch方法从 words 数组中搜索单词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM