我試圖制作以感嘆號結尾的句子的頻率直方圖,問號,以及文本中以點結尾的句子(我只是計算了文本中這些字符的數量)。從檔案中讀取文本。我完成的代碼如下所示:
import matplotlib.pyplot as plt
text_file = 'text.txt'
marks = '?!.'
lcount = dict([(l, 0) for l in marks])
for l in open(text_file, encoding='utf8').read():
try:
lcount[l.upper()] = 1
except KeyError:
pass
norm = sum(lcount.values())
fig = plt.figure()
ax = fig.add_subplot(111)
x = range(3)
ax.bar(x, [lcount[l]/norm * 100 for l in marks], width=0.8,
color='g', alpha=0.5, align='center')
ax.set_xticks(x)
ax.set_xticklabels(marks)
ax.tick_params(axis='x', direction='out')
ax.set_xlim(-0.5, 2.5)
ax.yaxis.grid(True)
ax.set_ylabel('Sentences with different ending frequency, %')
plt.show()
但是我不能算其他以省略號結尾的句子(這意味著...),我的代碼算作三個字符,所以三個句子。此外,這是符號的數量,而不是實際的句子。如何改進句子計數,而不是標記和以省略號結尾的句子計數?檔案示例: 想玩嗎?我們走吧!一定會好的!我朋友也這么認為。但是,我不知道……我不喜歡這樣。
uj5u.com熱心網友回復:
您可以嘗試使用regex. 該re.split()函式在這里作業正常:示例代碼:
import re
string = "Wanna play? Let's go! It will be definitely good! My friend also think so. However, I don't know... I don't like this."
print(re.split('\. \s*|! \s*|\? \s*', string))
輸出:
['Wanna play', "Let's go", 'It will be definitely good', 'My friend also think so', "However, I don't know", "I don't like this", '']
uj5u.com熱心網友回復:
從您的示例來看,以及在 NLP 中處理單詞的常用方法(關于詞形還原等),您可以首先在空格上拆分句子:
marks = ['?', '!', '...', '.'] # Ordering of ellipsis first gives prio of checking that condition
mark_count = {}
sentences = 0
for example in file:
words = example.split(" ") # This gives an array of all words togheter with your symbols
# Finds markings in word
for word in words:
for mark in marks:
if mark in word:
# Here you will find and count end of sentence and break if found, as a
# word can be "know..." the first dot will be found and then we break as we know
# thats the end of the sentence.
sentences = 1
if mark_count.get(mark, False):
mark_count[mark] = 1
else:
mark_count[mark] = 1
break
編輯:
保證正確配對
columns = []
values = []
for key in mark_count.keys():
columns.append(key)
values.append(mark_count[key])
plt.bar(columns, values, color='green')
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/454572.html
上一篇:比較Python中的內部字典
