【问题标题】:Text File Problems文本文件问题
【发布时间】:2020-12-09 23:15:33
【问题描述】:

我不知道我能问出这个问题有多好,但是给定一个文本文件,我需要解析并提取 productID 数据并将其存储在 HashSet、userID 数据并将其存储在 HashSet 中,以及评论/分数并将其存储在 ArrayList 中。它们还需要用于创建一个图,其中 productID 与 userID 之间的一条边相连。

数据在这里找到http://snap.stanford.edu/data/web-FineFoods.html 您可以忽略评论/时间、评论/帮助、评论/摘要和评论/文本信息,它们不需要存储在内存中。

我当前的代码如下所示:

import java.io.*;
import java.util.*;
import java.nio.charset.*;

public class Reviews
{
    String fileName = "newfinefoods.txt";
    GraphType<String> foodReview;
    HashSet<String> productID;
    HashSet<String> userID;
    ArrayList<String> review;
    
    int counter; //was using this to make sure I'm counting all the lines which I think I am
    
    public Reviews(){
        foodReview = new GraphType<>();
        productID = new HashSet<>();
        userID = new HashSet<>();
        review = new ArrayList<>();
        counter = 0;
    }
    
    public int numReviews(){
        return review.size();
    }
    
    public int numProducts(){
        return productID.size();
    }
    
    public int numUsers(){
        return userID.size();
    }
    
    public void setupGraph(){
        Scanner fileScanner;
        String line = "";
        try{
            fileScanner = new Scanner (new File (fileName), "UTF-8");
            String pr = "";
            while(fileScanner.hasNextLine()){
                line = fileScanner.nextLine();
                String[] reviewInfo = line.split(": ");
                String productInfo = reviewInfo[1];
                System.out.println(productInfo);
            }
        }
        
        catch (IOException e){
            System.out.println(e);
        }
    }
    
    
    
    public static void main(String[] args){
        Reviews review = new Reviews();
        review.setupGraph();
        System.out.println("Number of Reviews:" + review.numReviews());
        System.out.println("Number of Products:" + review.numProducts());
        System.out.println("Number of Users:" + review.numUsers());
        
    }
}

每当我运行代码时,在数组 reviewInfo 中查找 1 时,它只会打印一组数据,但如果我将其更改为 0,它似乎会打印所有信息(而不是我需要的信息)。我需要创建此图表并从数据中获取信息,但我真的只是超级卡住,任何提示或帮助将不胜感激!

以下是数据示例:

product/productId: B001E4KFG0
review/userId: A3SGXH7AUHU8GW
review/profileName: delmartian
review/helpfulness: 1/1
review/score: 5.0
review/time: 1303862400
review/summary: Good Quality Dog Food
review/text: I have bought several of the Vitality canned dog food products and have found them all to be of good quality. The product looks more like a stew than a processed meat and it smells better. My Labrador is finicky and she appreciates this product better than  most.

product/productId: B00813GRG4
review/userId: A1D87F6ZCVE5NK
review/profileName: dll pa
review/helpfulness: 0/0
review/score: 1.0
review/time: 1346976000
review/summary: Not as Advertised
review/text: Product arrived labeled as Jumbo Salted Peanuts...the peanuts were actually small sized unsalted. Not sure if this was an error or if the vendor intended to represent the product as "Jumbo".

product/productId: B000LQOCH0
review/userId: ABXLMWJIXXAIN
review/profileName: Natalia Corres "Natalia Corres"
review/helpfulness: 1/1
review/score: 4.0
review/time: 1219017600
review/summary: "Delight" says it all
review/text: This is a confection that has been around a few centuries.  It is a light, pillowy citrus gelatin with nuts - in this case Filberts. And it is cut into tiny squares and then liberally coated with powdered sugar.  And it is a tiny mouthful of heaven.  Not too chewy, and very flavorful.  I highly recommend this yummy treat.  If you are familiar with the story of C.S. Lewis' "The Lion, The Witch, and The Wardrobe" - this is the treat that seduces Edmund into selling out his Brother and Sisters to the Witch.

product/productId: B000UA0QIQ

【问题讨论】:

  • 文本文件中还有一些空行,我认为我需要以某种方式处理,但我认为这应该是一个简单的 if 语句,对吧?
  • 你链接的那个文件很大,很难下载。您能否在您的问题中提供一个示例?
  • @Cardinal-ReinstateMonica 是的,它对我来说几乎太大了,无法在 ym 计算机上处​​理,哈哈。刚刚更新了,谢谢你的提示!
  • 我对目标模型有点困惑。您确定需要将数据存储在两个 HashSet 和一个 ArrayList 中吗?
  • @LittleSanti 不,我不确定这是我想要使用的!我认为 HashSet 将允许我存储 userID 和 productID 并自动处理重复项,然后打印出 HashSet 的大小(因为我们的任务之一是显示审核了多少独特产品以及审核了多少独特用户产品)

标签: java data-structures text graph


【解决方案1】:

您的设计的初始方法是正确的,但您应该将其结构化:

方法 setupGraph 应该拆分为一些特定的参数化方法:

  • 由于用户和产品是类状态的一部分,我认为类的构造函数接收扫描仪作为输入参数会更好。然后,在初始化状态变量之后,它应该调用setupGraph(应该是私有的)传递输入扫描器。
  • setupGraph 应接收输入扫描器并负责从中读取行,并对可能出现的 IOExceptions 给予适当的处理。在每一行上,它应该只调用另一个私有方法来处理读取的行。如果您想计算所有读取的行数,则应在此处放置增量。
  • 处理线方法应接收输入字符串,并负责决定它是否包含产品数据、用户数据、分数数据或不包含。这必须通过正确解析其内容来完成。 在这里,您可以使用String.split() 获取每行的名称和值,然后评估名称以决定将值存储在何处。如果您想计算所有已处理的行数,则应在此处放置增量。
  • 最后,main 方法应负责实例化扫描仪并在构造评论对象时传递它。这样,您可以从命令行接收文件名作为输入参数,因此您的程序将变得灵活

意识到你的类的唯一公共方法应该是构造函数和getter。并且状态变量应该是私有的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-11
    • 2019-04-20
    • 1970-01-01
    • 2013-11-20
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多