帮留学生做的一道题(存档)

Posted ଳxin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了帮留学生做的一道题(存档)相关的知识,希望对你有一定的参考价值。

题目: 

您需要实现一个应用程序,在可能的情况下通过缩写单词来缩短消息。(例如,这可能对试图将其推文压缩为140个字符的Twitter用户很有用。)您应该首先实现一个处理缩短消息的类,然后是一个命令行缩短应用程序。
您的类和应用程序应该阅读一个缩写文本文件,该文件提供单词及其缩写的列表。缩写文本文件中的每一行由一个单词和一个缩写组成,用逗号分隔;例如:
 

you,u 

hello,lo 

any,ne 

one,1 

anyone,ne1 

提供了一个名为缩略语.txt的缩略语文件(通过点击这里下载)。您应该使用缩写.txt作为程序的默认缩写文件。为简单起见,您可以假定所有将被缩短的消息都是小写的。
点击这里下载文件Shortener.java。Shortener类的对象表示具有特定缩写集的消息缩短器。根据Shortener.java中给出的需求完成该类的实现。此外,Shortener应该是可变的;也就是说,应该可以通过设置不同的文件来更改使用的缩写集。您应该添加此行为所需的字段和方法。
点击这里下载文件ShortenerUtility.java。修改该文件以实现一个命令行应用程序ShortenerUtility,该应用程序将要缩短的消息作为单个命令行参数,并将缩短的消息打印到标准输出。一个例子是:

> java ShortenerUtility "hello world! anyone out there? it's lonely here." 

lo world! ne1 out there? it's lonely here. 

有一个txt文件:

one,1
two,2
three,3
four,4
five,5
six,6
seven,7
eight,8
nine,9
you,u
hello,lo
any,ne
anyone,ne1
thanks,thx
your,yr
probably,prob
people,ppl
sorry,sry
download,d/l
upload,u/l
with,w/
without,w/o
good,gd

 代码如下:

package com.kevin.springbootkevin1.util;

/*
 * Name: xxx
 * Student number: xxx
 */

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Shortener {
    // This class is only a starting point. You should complete all members
    // below, but you may also need to add other fields and methods to
    // finish the implementation as per the question on the assignment sheet.
    List<String> list;
    Map<String, String> map;
    /*
     * Default constructor that will load a default abbreviations text file.
     */
    public Shortener() {
        // to be completed
        list = new ArrayList<>();
        map = new HashMap<>();
    }

    /*
     * Constructor that will load the abbreviations file represented by the
     * File parameter.
     */
    public Shortener( File inAbbreviationsFile ) throws IOException {
        // to be completed
        list = new ArrayList<>();
        map = new HashMap<>();
        BufferedReader br = new BufferedReader(new FileReader(inAbbreviationsFile));
        String line = null;
        while ((line = br.readLine()) != null) {
            String k = line.split(",")[0];
            String v = line.split(",")[1];
            this.list.add(k);
            this.map.put(k , v);
        }
        br.close();
    }

    /*
     * This method attempts to shorten a word by finding its abbreviation. If
     * no abbreviation exists for this word, then this method will return the
     * original (i.e., unshortened) word.
     *
     * You may assume that words are always lower case.
     *
     * `inWord` should be a single word (no spaces). It may optionally be
     * followed by one of the five following punctuation characters:
     *   ,
     *   ?
     *   .
     *   !
     *   ;
     * If one of these characters is at the end of the word this method will
     * shorten the word (ignoring the punctuation) and return the shortened
     * word with the punctuation character at the end.
     * For example,
     *     shortenerObject.shortenWord( "hello?" )
     * should return
     *     "lo?"
     *
     * You may assume that words are always lower case.
     */
    public String shortenWord( String inWord ) {
        // to be completed
        while(true) {
            int isok = 0;
            for(int i=0;i<this.list.size();i++) {
                String curList = this.list.get(i);
                if(inWord.contains(curList)) {
                    inWord = inWord.replace(curList , this.map.get(curList));
                    isok = 1;
                }
            }
            if(isok == 0) {
                return inWord;
            }
        }
    }

    /*
     * Attempts to shorten a message by replacing words with their
     * abbreviations.
     *
     * You may assume that messages are always lower case.
     *
     * Punctuation characters (,?.!;) should be retained after shortening. See
     * `shortenWord( String inWord )` for more information.
     */
    public String shortenMessage( String inMessage ) {
        // to be completed
        String[] ss = new String[1000];
        ss = inMessage.split(" ");
        //Faster than String when append String.So I choose StringBuffer.
        StringBuffer sbf = new StringBuffer();
        for(int i=0;i<ss.length;i++) {
            //System.out.println(ss[i]);
            sbf.append(shortenWord(ss[i]));
            if(i != ss.length-1) sbf.append(" ");
        }
        return sbf.toString();
    }
}
package com.kevin.springbootkevin1.util;

/*
 * Name: xxx
 * Student number: xxx
 */

import java.io.File;
import java.io.IOException;

/*
 * A command-line application that shortens a message.
 */
public class ShortenerUtility {
    // to be completed
    public static void main(String[] args) throws IOException {
        File file = new File("C://abbreviations.txt");
        Shortener s = new Shortener(file);
        String sv = "hello world! anyone out there? it's lonely here.";
        System.out.println(sv);
        System.out.println(s.shortenMessage(sv));
    }
}

 

以上是关于帮留学生做的一道题(存档)的主要内容,如果未能解决你的问题,请参考以下文章

NLP商战——好心≠好报,好产品≠好业绩

每日一道中考题最短路径问题

表妹问我一道大一算法题,想半天不知道怎么以简单点的方式帮她解决。。。,最后看到答案很惭愧。

表妹问我一道大一算法题,想半天不知道怎么以简单点的方式帮她解决。。。,最后看到答案很惭愧。

2023年美国大学生数学建模竞赛资料分享

2023年美国大学生数学建模竞赛资料分享