【发布时间】: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