How to printout data from a list in web2py - web2py-modules

In a code such as shown below, I have data that is dumped to a list as shown:
def foo():
list1=[It’s possible Arianne could appear in future seasons, but as Kotaku notes, the official character bio of Trystane describes him as the heir to Dorne, while Ariane’s love interest, Aerys Oakheart, has also not been cast. The exclusion comes as a surprise to fans, not just because Arianne is a major player in the fantasy novels’ numerous plot twists, but also because she was a strong, complex female character in a fictional universe that doesn’t have too many of those.]
return list1
In view html:
{{extend 'layout.html'}}
<h1>{{=list1}}</h1>
How do I make it to print out each sentence as a bullet point?

Controller:
def foo():
list1=["It's possible Arianne could appear in future seasons, but as Kotaku notes, the official character bio of Trystane describes him as the heir to Dorne, while Ariane’s love interest, Aerys Oakheart, has also not been cast.", "The exclusion comes as a surprise to fans, not just because Arianne is a major player in the fantasy novels’ numerous plot twists, but also because she was a strong, complex female character in a fictional universe that doesn’t have too many of those."]
return dict(list1=list1)
View:
{{extend 'layout.html'}}
<ul>
{{for row in list1:}}
<li>{{=row}} </li>
{{pass}}
</ul>
The view iterates through the list and creates one "li" tag per each item passed by the controller.

Related

Extracting sentences with Spacy POS/DEP : actor and action

Thank you for your assistance. I am using spacy to parse though documents to find instances of certain words and extract the sentence in a new df[column].
Here are some texts:
text = 'Many people like Germany. It is a great country. Germany exports lots of technology. France is also a great country. France exports wine. Europeans like to travel. They spend lot of time of beaches. Spain is one of their travel locations. Spain appreciates tourists. Spain's economy is strengthened by tourism. Spain has asked and Germany is working to assist with the travel of tourists to Spanish beaches. Spain also like to import French wine. France would like to sell more wine to Spain.'
My code works like this:
def sent_matcher(text: str) -> list:
doc = nlp(text)
sent_list = []
phrase_matcher = PhraseMatcher(nlp.vocab)
phrases = ['Germany', 'France']
patterns = nlp(data) for data in phrases]
phrase_matcher.add('EU entity', None, * patterns)
for sent in doc.sents:
for match_id, start, end in phrase_matcher(nlp(sent.text)):
if nlp.vocab.strings[match_id] in ['EU entity']:
sent_list.append(sent)
text = (sent_list)
return text
This code works fine and pulls all the sentences that include the EU entity.
However, I wanted to take this to the next level and pull out sentences where the EU entity is the actor and identify what type of action they were taking. I tried using POS/Dependency to pull out Proper nouns combined with the verb but the nsubj was not always correct or the nsubj was linked to another word in a compound noun structure. I tried extracting instances where the country was the first actor (if token == 'x') but I always threw a string error even if I tokenized the word. I also tried using noun_chunks but then I couldn't isolate the instance of the country or tie that chunk back to the verb.
I am pretty new to NLP so any thoughts would be greatly appreciated on how to code this and reap the desired output.
Thank you for your help!
It sounds like if you use merge_entities and follow the rule-based matching guide for the DependencyMatcher you should be able to do this pretty easily. It won't be perfect but you should be able to match many instances.

python decode partial hex strings

I'm using beautiful soup to parse email invoices and I'm running into consistent problem involving special characters.
The text I am trying to parse is shown in the image.
But what I get from beautiful soup after finding the element and calling elem.text is this:
'Hi Mike, It=E2=80=\r\n=99s probably not a big drama if you are having problems separating product=\r\ns from classes. It is not uncommon to receive an order for pole classes and=\r\n a bottle of Dry Hands.\r\nAlso, remember that we will have just straight up product orders that your =\r\nsystem will not be able to place into a class list, hence having the extra =\r\nsheet for any =E2=80=9Cerroneous=E2=80=9D orders will be handy.'
As you can see the apostrophe is now represented by "=E2=80=99", double quotes are "=E2=80=9C" and "=E2=80=9D" and there are seemingly random newlines in the text, for example "product=\r\ns".
The newlines don't seem to appear in the image.
Apparently "E2 80 99" is the unicode hex representation of ' , but I don't understand why I can still see it in this form after having done email.decode('utf-8') before sending it to beautiful soup.
This is the element
<td border:="" class='3D"td"' left="" middle="" padding:="" solid="" style='3D"color:' text-align:="" v="ertical-align:">Hi Mike, It=E2=80=
=99s probably not a big drama if you are having problems separating product=
s from classes. It is not uncommon to receive an order for pole classes and=
a bottle of Dry Hands.
Also, remember that we will have just straight up product orders that your =
system will not be able to place into a class list, hence having the extra =
sheet for any =E2=80=9Cerroneous=E2=80=9D orders will be handy.</td>
I can post my code if required but I figure I must be making a simple mistake.
I checked out the answer to this question
Decode Hex String in Python 3
but i think that expects the entire string to be hex rather than just having random hex parts.
but I'm honestly not even sure how to search for "decode partial hex strings"
My final questions are
Q1 How do I convert
'Hi Mike, It=E2=80=\r\n=99s probably not a big drama if you are having problems separating product=\r\ns from classes. It is not uncommon to receive an order for pole classes and=\r\n a bottle of Dry Hands.\r\nAlso, remember that we will have just straight up product orders that your =\r\nsystem will not be able to place into a class list, hence having the extra =\r\nsheet for any =E2=80=9Cerroneous=E2=80=9D orders will be handy.'
into
'Hi Mike, It's probably not a big drama if you are having problems separating products from classes. It is not uncommon to receive an order for pole classes and=\r\n a bottle of Dry Hands.Also, remember that we will have just straight up product orders that your system will not be able to place into a class list, hence having the extra sheet for any "erroneous" orders will be handy.'
using python 3, without manually fixing each string and writing a replace method for each possible character.
Q2 Why does this "=\r\n" appear everywhere in my string but not in the rendered html?
#JosefZ's comment lead me to the answer.
Q1 has an answer.
>>> import quopri
>>> print(quopri.decodestring(mystring).decode('utf-8'))
Hi Mike, It’s probably not a big drama if you are having problems separating products from classes. It is not uncommon to receive an order for pole classes and a bottle of Dry Hands.
Also, remember that we will have just straight up product orders that your system will not be able to place into a class list, hence having the extra sheet for any “erroneous” orders will be handy.
Q2 Thanks to #snakecharmerb I now know that the seemingly random unrepresented line endings are to enforce a line length of 80 characters.
#snakecharmerb wrote a much better answer than this one to someone with the same problem as me here.
https://stackoverflow.com/a/55295640/992644

How to capitalize every word in a string in cases where title() doesn't 100% work out in python

Hi,
I am relatively new to python, and I was wondering why the code below doesn't remain applicable to all of the sample tests in Codewars ("Jaden Casing strings") which is as follows:
Jaden Casing Strings:
Jaden Smith, the son of Will Smith, is the star of films such as The Karate Kid (2010) and After Earth (2013). Jaden is also known for some of his philosophy that he delivers via Twitter. When writing on Twitter, he is known for almost always capitalizing every word. For simplicity, you'll have to capitalize each word, check out how contractions are expected to be in the example below.
Your task is to convert strings to how they would be written by Jaden Smith. The strings are actual quotes from Jaden Smith, but they are not capitalized in the same way he originally typed them.
Example:
Not Jaden-Cased: "How can mirrors be real if our eyes aren't real"
Jaden-Cased: "How Can Mirrors Be Real If Our Eyes Aren't Real"
Link to Jaden's former Twitter account #officialjaden via archive.org
My code:
def to_jaden_case(string):
for word in string:
if "'" in word:
word.capitalize()
else:
word.title()
return string
I am also new to Python I tired below method which seems to work:
def to_jaden_case(string):
return ' '.join(i.capitalize() for i in string.split())
I was trying to use .title() in different ways but couldn't seem get solution with that but i could split string and capitalize every word.

when calculating the cooccurance of two words, do we sepate the sentences or linking all sentences?

For example, I get I document that contains 2 sentences: I am a person. He also likes apples.
Do we need to count the cooccurrence of "person" and "He" ?
Each document is separated with a line break. Context windows of cooccurrences are limited to each document.
Based on the implementation here.
A newline is taken as indicating a new document (contexts won't cross newline).
So, depending on how you prepare sentences, you may get different results:
Setting 1: ('He', 'person') cooccurred
...
I am a person. He also likes apples.
...
Setting 2: ('He', 'person') not cooccurred
...
I am a person.
He also likes apples.
...

Python 3.4 Inheritance

I have a medical form class. This form would be given to both male and female. I would like NOT to have to differentiate between male and female when I instantiate the class. It would be nice if I could instantiate the same way regardless of the gender and ask same questions regardless of the gender. But the program which will realize it is stupid to ask if a man is pregnant, then it will either not ask that question or better yet print saying it is irrelevant question and skip it.
This is basic code I have.
if male:
form = medForm("Bill")
form.MedHistory.isSmoking()
if female:
form = medForm("Sarah")
form.MedHistory.isSmoking()
form.MedHistory.isPregnent()
class maleSection():
def isSmoking(self):
#some code here exactly same as female, so create a base class later
class femaleSection():
def isPregnent(self):
#some code here
def isSmoking(self):
#some code here exactly same as male, so create a base class later
class medForm(maleSection, femaleSection):
def _init_(self, nameOfCustomer):
#At the moment not sure if I need anything here
Those two if statements are ugly. Whats the best way to handle this? Also I heard using Super can be tricky, therefore, if possible I would like to not use Super and keep it simple. This is being run on Win7, Python 3.4. sys.version_info(major=3, minor=4, micro=3, releaselevel='final', serial=0)
I appreciate any help, thank you.
For this type of problem, the best approach is to utilize inheritance. It saves you a lot of time, especially if you need to change something later.
Try something like this:
class patientSection():
def isSmoking(self):
#the code here is the same for both male and female
class maleSection(patientSection):
#no code here, since we have access to isSmoking already
class femaleSection(patientSection):
#again, we have access to isSmoking
def isPregnant(self):
#something here
You can declare an instance of just a patientSection if you want to ask only gender neutral questions; however, you'll need to use femaleSection to utilize the isPregnant method.

Resources