答案
问题是以前使用过twobj变量,并在代码的这一部分进行了混合。现在一切都正常了。如果你对我的项目有任何疑问,请随便问。
问题
我正在编写一个程序,该程序自动从符合haiku模式的数据库中提取twitter消息(5个音节、7个音节、5个音节),然后在twitter上发布。一切正常,除了音节数。这个过滤器模块用来计数音节,得到一个带有标记化tweet的对象,并检查字典中每个单词的音节数。这本字典很好用。我用单词作为键,音节数作为值。问题是,我无法写出合适的脚本来检查音节数是否与海库模式相匹配。这是我现在的代码:
# twobj_list is a list of tweet objects
for twobj in twobj_list:
syl_cnt = 0
line1 = False
line2 = False
line3 = False
haiku_match = True
# twobj.text is tokenized tweet
for i in range(len(twobj.text)):
# dpw_dict is a (dutch) dictionary with words as keys
# and number of syllables as values
if twobj.text[i] in dpw_dict:
syl_cnt += dpw_dict[twobj.text[i]]
if not line1 and haiku_match:
if syl_cnt > 5:
haiku_match = False
elif syl_cnt == 5:
line1 = True
elif not line2 and haiku_match:
if syl_cnt > 12:
haiku_match = False
elif syl_cnt == 12:
line1 = True
elif not line3 and haiku_match:
if syl_cnt > 17:
haiku_match = False
elif syl_cnt == 17 and i + 1 == len(twobj.text):
line3 = True
break
elif syl_cnt == 17 and not i + 1 < len(twobj.text):
haiku_match = False
if haiku_match:
new_twobj_list.append(twobj)这句话:
if twobj.text[i] in dpw_dict:
syl_cnt += dpw_dict[twobj.text[i]]给出与字典中一个单词匹配的tweet中单词的音节数。据我所知,这是可行的。
我希望有人能告诉我我在这里做错了什么。提前感谢
它的输出包括了一些根本不符合haiku模式的推文。然而,据我所见,所有这些音节都小于17个音节。
发布于 2018-04-04 14:56:01
问题是以前使用过twobj变量,并在代码的这一部分进行了混合。现在一切都正常了。如果你对我的项目有任何疑问,请随便问。
https://stackoverflow.com/questions/49651976
复制相似问题