根据词性分类

该程序演示了如何遍历一个包含词性与单词的字符串数组,将动词和名词分别写入verb.txt和noun.txt文件。通过判断字符串是否包含'verb'或'noun'来筛选词性,并在每个单词后添加换行符以确保文件中的单词独立成行。
摘要由CSDN通过智能技术生成

一个words数组,数组中每个字符串的格式为“词性:单词”

  • String[] words = {“verb:eat”,“verb:drink”,“verb:sleep”,“verb:play”,“noun:rice”,
    “noun:meat”,“noun:hand”,“noun:hair”};
    根据单词性质动词verb全部存入verb.txt文件中
    根据单词性质名词noun全部存入noun.txt文件中
import java.io.FileOutputStream;
import java.io.IOException;

public class Work2 {
    public static void main(String[] args) throws IOException {
        String[] words = {"verb:eat", "verb:drink", "verb:sleep", "verb:play",
                "noun:rice", "noun:meat", "noun:hand", "noun:hair"};
        //用String类里的contains判断当前字符串包含noun还是verb
        //把同词性的单词拼接到一个字符串中
        String verbs = "", nouns = "";//最终字符串
        String v = "verb", n = "noun";//参照字符串
        for (int i = 0; i < words.length; i++) {
            if (words[i].contains(v)) {
                verbs += words[i];
                verbs += "\n";//为了复制到文件能自动换行
            }
            if (words[i].contains(n)) {
                nouns += words[i];
                nouns += "\n";
            }
        }
        //jvm把数据写到外存
        FileOutputStream out = new FileOutputStream("verb.txt");
        out.write(verbs.getBytes());
        out = new FileOutputStream("noun.txt");
        out.write(nouns.getBytes());
        //资源释放
        out.close();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值