【问题标题】:Using Regular Expressions to get bigrams into a dictionary使用正则表达式将二元组放入字典
【发布时间】:2022-01-17 08:25:50
【问题描述】:

我正在尝试使用 RegEx 制作一个字典,其中的键是文本文件中的二元组,其值是这些二元组在文本中出现的次数。

我有这段代码可以让我得到二元组。它并不完美,因为二元组应该像 "hello, world" , "world, full" ""full, of" "of, wonderful" "wonderful, things",但在我的打印输出中,二元组的顺序与此不同,所以我不确定我是否做得对。

我不知道如何将这些让二元组进入字典的正则表达式短语与那些二元组的键和反映其在整个原始文本文件中的计数的值相关联。非常感谢任何帮助。

import re
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer

with open('/Users/adamstark/PycharmProjects/Computational_Methods_Course/Assignments/Final/Jules_Verne_From_the_Earth_to_Moon.txt') as file:
    txt1 = file.readlines()

# Getting bigrams
txt1 = [remove_string_special_characters(s) for s in txt1]
vectorizer = CountVectorizer(ngram_range = (2,2))
X1 = vectorizer.fit_transform(txt1)
features = (vectorizer.get_feature_names())
print("\n\nFeatures : \n", features)
print("\n\nX1 : \n", X1.toarray())

【问题讨论】:

  • 你永远不会在这段代码中的任何地方调用remove_string_special_characters。还有更多你没有给我们看的吗?您可以删除text1 = [] 行。这既是一个错字,也是不必要的。
  • 不,这就是我所拥有的一切。我不确定接下来要采取什么步骤。
  • 您的代码运行良好。这组二元组按字母顺序打印,因此它们不会按源材料的顺序排列。并且可以删除不调用的预处理函数;看起来矢量化器已经为您完成了所有这些工作。
  • 谢谢,我整理了它以反映您所指出的内容,并结合了 Manial A 在下面向我展示的内容,您还提到了“remove_string_special_characters”的缺失。现在我正在尝试解决名称错误:“名称'remove_string_special_characters'未定义”并希望将二元组格式化为所描述的字典格式。

标签: python regex dictionary


【解决方案1】:

你不会在任何地方打电话给remove_string_special_characters。如果你调用它来删除特殊字符,它就可以了。

例如:

txt1 #= the text of your question here
txt1 = [remove_string_special_characters(s) for s in txt1]
vectorizer = CountVectorizer(ngram_range = (2,2))
X1 = vectorizer.fit_transform(txt1)
features = (vectorizer.get_feature_names())
print("\n\nFeatures : \n", features)
print("\n\nX1 : \n", X1.toarray())

这吐了:

Features : 
 ['am not', 'and values', 'and whose', 'any help', 'are ordered', 'are the', 'are those', 'be like', 'because bigrams', 'bigrams are', 'bigrams from', 'bigrams in', 'bigrams its', 'bigrams should', 'bigrams to', 'but in', 'code that', 'correlate these', 'count throughout', 'dictionary with', 'did it', 'differently than', 'file and', 'file any', 'from text', 'full of', 'get the', 'gets me', 'go into', 'got this', 'greatly appreciated', 'hello world', 'help greatly', 'how to', 'if did', 'im not', 'im trying', 'in my', 'in the', 'into dictionary', 'it right', 'its not', 'ive got', 'keys that', 'like hello', 'make dictionary', 'me the', 'my print', 'not perfect', 'not sure', 'number of', 'occurrences of', 'of occurrences', 'of of', 'of those', 'of wonderful', 'ordered differently', 'original text', 'out the', 'phrases that', 'reflect their', 'regex phrases', 'regex to', 'should be', 'so im', 'sure how', 'text file', 'than that', 'that are', 'that get', 'that gets', 'that reflect', 'that so', 'the bigrams', 'the number', 'the original', 'the text', 'their count', 'these regex', 'things but', 'this code', 'those bigrams', 'throughout the', 'to correlate', 'to go', 'to make', 'to work', 'trying to', 'values are', 'values that', 'whose values', 'with keys', 'with regex', 'wonderful things', 'wonderful wonderful', 'work with', 'world full', 'world world']

X1 : 
 [[0 0 0 ... 1 0 0]
 [0 0 1 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 1 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

【讨论】:

  • 谢谢,这很有希望。但是我遇到了问题,因为它会在我运行代码时生成名称错误:“名称'remove_string_special_characters'未定义”,因为我现在已经对其进行了编辑,这反映了更改。我不确定如何将上面的第一行与我最初打开和读取的 text1 文件联系起来。
  • 好吧,您更改了您的问题(事后),将呼叫添加到remove_special_characters。如果你要调用它,那么你需要添加它。但正如我在回答中提到的,你不需要它。 sklearn 代码已经完成了所有这些工作。
  • 我误解了你的意思。我刚刚删除了,remove_special_characters。它又开始工作了。谢谢你。我现在将通过计算每个二元组来努力填写字典。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-09
相关资源
最近更新 更多