mirror of
https://expo.survex.com/repositories/expoweb/.git/
synced 2025-03-23 10:41:55 +00:00
[svn r8159] randomLogbookSentance.py renamed to randSent.py, and expanded
This commit is contained in:
parent
e543df8b83
commit
8edaff3045
30
troggle/expo/randSent.py
Normal file
30
troggle/expo/randSent.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import troggle.settings as settings
|
||||||
|
from django import forms
|
||||||
|
from expo.models import LogbookEntry
|
||||||
|
import random
|
||||||
|
import re
|
||||||
|
|
||||||
|
def weighted_choice(lst):
|
||||||
|
n = random.uniform(0,1)
|
||||||
|
for item, weight in lst:
|
||||||
|
if n < weight:
|
||||||
|
break
|
||||||
|
n = n - weight
|
||||||
|
return item
|
||||||
|
|
||||||
|
def randomLogbookSentence():
|
||||||
|
randSent={}
|
||||||
|
|
||||||
|
#Choose a random logbook entry
|
||||||
|
randSent['entry']=LogbookEntry.objects.order_by('?')[0]
|
||||||
|
|
||||||
|
#Choose again if there are no sentances (this happens if it is a placeholder entry)
|
||||||
|
while len(re.findall('[A-Z].*?\.',randSent['entry'].text))==0:
|
||||||
|
randSent['entry']=LogbookEntry.objects.order_by('?')[0]
|
||||||
|
|
||||||
|
#Choose a random sentence from that entry. Store the sentence as randSent['sentence'], and the number of that sentence in the entry as randSent['number']
|
||||||
|
sentenceList=re.findall('[A-Z].*?\.',randSent['entry'].text)
|
||||||
|
randSent['number']=random.randrange(0,len(sentenceList))
|
||||||
|
randSent['sentence']=sentenceList[randSent['number']]
|
||||||
|
|
||||||
|
return randSent
|
Loading…
x
Reference in New Issue
Block a user