Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算每个用户的词频_Python - Fatal编程技术网

Python 计算每个用户的词频

Python 计算每个用户的词频,python,Python,我试图为用户生成词频统计数据,就像他们在评论中给出的那样,比如 用户1:词频 用户2:词频等 我该怎么做 在这里,我试图访问每个用户的评论,但它给了我一个错误 请建议方法和sudo代码 import json from pprint import pprint file = open('/Users/mack/Downloads/WKA/task/reviews.json','r') content = file.read() file = json.loads(content) for e

我试图为用户生成词频统计数据,就像他们在评论中给出的那样,比如

用户1:词频

用户2:词频等

我该怎么做

在这里,我试图访问每个用户的评论,但它给了我一个错误

请建议方法和sudo代码

import json
from pprint import pprint

file = open('/Users/mack/Downloads/WKA/task/reviews.json','r')
content = file.read()
file = json.loads(content)

for eid, txt in file["id"]["text"]:
    print(eid, txt)
像这样的大json:

[
    {
       "id": 1,
       "text": "Bought this over a month ago and everything came like advertise. I got the purple cover and it looks wonderful. The outlet works just fine and charges my kindle without a problem. I also bought it on sale so it was $20 cheaper. Best. Deal. Ever. Love my kindle paperwhite (love being able to read in the dark too!) Also makes reading at work much easier than a traditional book. Thanks Amazon.",
    },
    {
       "id": 2,
       "text": "Why three stars? Skip the next two paragraphs. Purchased the bundle on Black Friday - great price. The device works as advertised and I'm enjoying it. However, the lighting (even on max) is underwhelming. The features are handy and easy to use (i.e. dictionary, highlighting, bookmark, etc.) The case is attractive and sturdy enough, but the magnetic closure is rather weak. I suspect the case would open easily if the device were dropped.. In retrospect, I probably would have been dollars ahead to purchase a less expensive case separately rather than bundling. The reason for the three (3) stars? The promoted $15 credit towards purchase of ebook(s). After two unsuccessful attempts to redeem the credit and visiting with an Amazon rep, it appears the credit only works for Amazon digital/published books and is NOT applicable to third party publisher/sellers such as HarperCollins, Random House, Simon and Schuster, Penguin, Tyndale, Scholastic, Thomas Nelson, etc. etc. After respectfully telling the rep that this promotion seems very misleading and asking where I could find a list of authors and/or books for which the credit is applicable, he could offer no such list or database. He suggested finding an author on the Amazon ebook list, clicking on a title, putting the book into the order box and then noting the publisher in the order box. If it didn't say Amazon, I would know the credit could not be applied. I have since located several of my favorite writers and pulled up many of their ebooks. As I expected, NONE were available for purchase with the credit. ALL were published by major publishing houses. NONE were published by Amazon digital. I cannot imagine any prolific author of note not being affiliated with major publishing houses - which leaves the enticing ebook credit pretty much useless to me. The language in the Terms and Conditions seems vague at best regarding this restriction. This lack of clarity gives the consumer little, if any, pause regarding the use of the credit. After trying to use it, I felt like I had been scammed. I would NOT recommend purchasing the bundle - even on special pricing days like Black Friday. I feel like I simply gave $15 to Amazon and got virtually nothing in return. If I had it to do over again, I definitely would purchase the Paperwhite. I also would buy the Amazon charger and probably a less expensive case. (Even though I suspect a 5watt iPhone charger would work perfectly, I would still purchase the Amazon charger. In the event the device became problematic, the charger would be on the invoice thereby suggesting the device had been properly charged and disallowing refusal to repair or replace due to improper charging.) The device has been wonderful to use, the case is okay, haven't had to use the charger yet (impressive), but the $15 ebook credit seems virtually worthless.",
    }
]
输入: id及其在json中的相对文本

输出: id和文本中出现的字数

文件=\
[
{
“id”:1,
“文本”:“一个多月前买的,一切都像广告一样。我得到了紫色的封面,它看起来很棒。插座工作正常,对我的kindle充电没有问题。我还特价买了它,所以它便宜了20美元。最棒。最划算。永远。爱我的kindle paperwhite(也爱能在黑暗中阅读!)这也使得在工作中阅读比传统书籍容易得多。谢谢亚马逊。”,
},
{
“id”:2,
“文字”:“为什么是三星级?跳过下面两段。在黑色星期五购买了该捆绑包-价格很高。该设备按照广告的方式工作,我很享受。但是,照明(即使在max上)不太吸引人。这些功能方便易用(即字典、突出显示、书签等)这个箱子很吸引人,也很坚固,但是磁性外壳很弱。我怀疑如果设备掉下来的话,箱子会很容易打开。回想起来,我可能会预付美元单独购买一个较便宜的箱子,而不是捆绑购买。原因是三(3)stars?购买电子书的15美元优惠.在两次尝试赎回信用卡并访问亚马逊代表失败后,信用卡似乎仅适用于亚马逊数字/出版图书,不适用于第三方出版商/销售商,如HarperCollins、Random House、Simon and Schuster、Penguin、Tyndale、Scholastic、Thomas Nelson等。在恭敬地告知之后该代表表示,这项推广似乎非常误导,并询问我在哪里可以找到作者和/或书籍的列表,他无法提供此类列表或数据库。他建议在亚马逊电子书列表中找到一位作者,单击标题,将书放入订购框,然后在订购框中注明出版商。如果它没有说亚马逊,我就知道这项信用无法申请。我已经找到了我最喜欢的几位作家,并找到了他们的许多电子书。正如我所料,没有一本可以用信用购买。所有这些都是由大出版社出版的。没有一本是由亚马逊数字出版的。我无法想象有任何多产的作家值得注意的是,由于不隶属于主要出版社,这使得诱人的电子书信用证对我来说几乎毫无用处。条款和条件中关于这一限制的措辞充其量只是含糊不清。这种缺乏明确性的情况使消费者几乎没有(如果有的话)暂停信用证的使用。在尝试使用它之后,我感觉自己广告被骗了。我不建议购买该捆绑包——即使是在黑色星期五这样的特殊定价日。我觉得我只是给了亚马逊15美元,却几乎没有得到任何回报。如果我要重做一次,我肯定会购买Paperwhite。我也会购买亚马逊充电器,可能还有一个更便宜的盒子。(尽管我怀疑一个5瓦的iPhone充电器可以很好地工作,但我还是会购买亚马逊充电器。如果设备出现问题,充电器会出现在发票上,从而表明设备已正确充电,并且不允许因充电不当而拒绝维修或更换。)这款设备使用起来非常好,机箱还可以,还没有使用过充电器(令人印象深刻),但15美元的电子书信用卡看起来几乎一文不值。”,
}
]

字典
count={}
对于文件中的用户:
计数[用户['id']]={}
对于用户['text']中的word。拆分()
count[user['id']][word]=count[user['id']]。获取(word,0)+1
输出:

{1:{'work':1,'so':1,'like':1,'come':1,'and':3,'problem':1,'over':1,'dark':1,'the':2,'just':1,'than':1,'Deal':1,'being':1,'purple':1,'make':1,'on':1,'Love':1,'(爱:1,‘好的’:1,‘永远’:1,‘paperwhite’:1,‘谢谢’:1,‘to’:1,‘买的’:1,‘书’:1,‘at’:1,‘传统的’:1,‘读的’:1,‘看的’:1,‘in’:1,‘封面’:1,‘kindle’:2‘便宜’:1,‘太便宜了!)“:1,‘最佳’:1,‘有效’:1,‘亚马逊’:1,‘它’:3,‘更容易’:1,‘这个’:1,‘得到’:1,‘销售’:1,‘销售’:1,‘出口’:1,‘没有’:1,‘也’:1,‘广告’:1,‘买了’:1,‘很多’:1,‘能够’:1,‘一切’:1,‘我’:2,‘以前’:1,‘是’:1,‘a’:3,‘收费’:1,‘月份’:1’:2:{'repair':1,'many':1,'applied':1,'noting':1,'恭敬地]:1,'expected':1,'best':1,'very':1,'being':1,'best':1,'telling':1,'any':1,'novely':1,'to':12,'trying':1,'use':1,'if':2,'been':1,'closure':1,'is':6,'structured':1,'buy':1,“Nelson':1,“features':1,“lighting':1,”“After':3'(3)':1,”“finding':1,”“Puting':1,”“of':7,”“Successful':1,”“say':1,”“simply':1,”“which':2,”“device':5,”“only':1,”“attractive':1,”“Maxs)“:1,'提供':1,'无':1,'缺乏':1,'随机':1,'拉动':1,'纸白':1,'这个':2,'感觉':1,'访问':1,'出现':1,'出版商/销售商':1,'两':2,'电子书':1,'是':1,'专业':2,'廷代尔':1,'漂亮':1,'清晰':1,'美元':1,'企鹅':1,'偶数':1,'诱人':1'(令人印象深刻),1,,:1,,:1,,:13,,:over,:1,,,,似乎,:3,,,没有,:1,,,顺序:,,2,,,小,:1,,,亚马逊,:1,,,原因:,,1,,,有:,,2,,,建议:,,1,,,数字,,:1,,,,(偶数:,:1,,,赎回:,,1,,,否:,,1,,,定价:,,1,,,西蒙:,,1,,,暂停:,,1