ARK | distributed plugin framework written in C,make
kandi X-RAY | ARK Summary
Support
Quality
Security
License
Reuse
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample Here
ARK Key Features
ARK Examples and Code Snippets
Trending Discussions on ARK
Trending Discussions on ARK
QUESTION
I have an xml file that looks that way :
A1eF4GT
Foo
tag='u'>Bar
Truc
Machin
I want to parse it with Python Etree and I'm just trying to get "A1eF4GT;Foo/Bar;Truc/Machin". So far I used findall to iterate over the df tag='123', but I can't manage to get the right pairs, and all I get is "A1eF4GT;Foo/Bar;Truc/Bar", since "Bar" is the first thing my code meets when using 'find'. I guess that there is some kinf of index that would help me to get the position of my df tag='123' and then assign the right value, but I cannot find something useful in the documentation.The portion of my code dealing with this looks like this:
for record in datafields:
id=record.find(".//cf[@tag='id']")
id=id.text.encode('utf8')
if record.findall("df[@tag='123']/sf[@code='a']"):
listequiv=[]
for equiv in record.findall("df[@tag='123']/sf[@code='a']"):
equiv=equiv.text
equiv2=record.find("df[@tag='123']/sf[@code='u']")
equiv2=equiv2.text
listequiv.append(equiv+'/'+equiv2)
stringequiv=';'.join(listequiv)
print ark+';'+stringequiv
I hope I'm clear enough (sorry I'm not an IT guy...)
ANSWER
Answered 2021-Jun-10 at 12:43Since this is a known tree-like structure, so you have to find df
records first, and then find sf
records inside each of those.
For example, list of df
records
>>> record.findall("df")
[, ]
Iteration example:
>>> for elem in record.findall("df"):
... for sub_elem in elem.findall("sf"):
... print(sub_elem.text)
...
HALLO
Bar
Truc
Machin
Your example (had to drop encode to utf-8
to join
to work
>>> listequiv = []
>>> cf_text = record.find(".//cf[@tag='id']").text
>>> listequiv.append(cf_text)
>>>
>>> for elem in record.findall("df"):
... sf_elems = []
... for sub_elem in elem.findall("sf"):
... sf_elems.append(sub_elem.text)
... listequiv.append("/".join(sf_elems))
...
>>> print(";".join(listequiv))
A1eF4GT;Foo/Bar;Truc/Machin
QUESTION
So I've written this, which is horrific:
def parse_results(string):
space = r"([\s\t]{0,5})"
building_type = r"(([Uu]nit|[Ss]tudio|[Ff]lat)?)"
street_type = (r"((\d+)(\&|\-)*(\d*)(\w*)(\w*)(\s*)(\w*)(\s*)(\w*)(\s*)"
r"([Ee]nd|[Gg]reen|[Cc]auseway|[Cc]heapside|[Cc]rescent|"
r"[Ss]treet|[Ll]ane|[Ww]alk|[Rr]oad|[Aa]venue|[Dd]rive|"
r"[Pp]ark|[Ww]ay|[Pp]lace|[Pp]arade|[Ii]ndustrial"
r"[Ee]state|[Tt]rading [Ee]state|[Hh]ouse|[Gg]reen))")
line_1 = r"(\w*)"
line_2 = r"(\w*)"
line_3 = r"(\w*)"
line_4 = r"(\w*)"
line_5 = r"(\w*)"
postcode = r"(([A-Z0-9][A-Z0-9][A-Z0-9]?[A-Z0-9]? {1,2}[0-9][A-Z]{2}))"
pattern = re.compile(rf"({building_type}{space}{street_type}{space}"
rf"{line_1}{space}{line_2}{space}{line_3}{space}"
rf"{line_4}{space}{line_5}{space}{postcode})")
try:
matches = pattern.finditer(string)
for match in matches:
address = re.sub(r"\s+", r" ", match.group(1))
return address
except Exception as e:
return (f"Error looking for address, exception {e}")
Its purpose is to look for UK addresses in a large text corpus I am using for machine learning training. It is unusably slow however because of the backtracking .
After research, the solution appears to be to use atomic groupings, similar to how it is done in Ruby.
The Python RE module doesn't support this out of the box, however there are workarounds such as this:
Do Python regular expressions have an equivalent to Ruby's atomic grouping?
And apparently there is a Python Regex module that does support atomix groupings out of the box, but almost no one seems to be talking about it in tutorials.
Two questions:
Which is the best approach? RE module work around or the Regex module?
Can someone point me in the direction of examples so I can figure this out for my usecase?
Thank you!
ANSWER
Answered 2021-Jun-09 at 17:13Whether you are using re
or regex
, you will have to fix your pattern, as it is catastrophic backtracking prone. Atomic groupings are not necessary here, you need optional groupings with obligatory patterns. Also, you need to fix your alternations that may start matching at the same location inside a string.
You can use
(?:[Uu]nit|[Ss]tudio|[Ff]lat)?\s{0,5}\d+[&-]*\w*\s*(?:[Ee]nd|[Gg]reen|[Cc]auseway|[Cc]heapside|[Cc]rescent|[Ss]treet|[Ll]ane|[Ww]alk|[Rr]oad|[Aa]venue|[Dd]rive|[Pp]ark|[Ww]ay|[Pp]lace|[Pp]arade|[Ii]ndustrial[Ee]state|[Tt]rading [Ee]state|[Hh]ouse|[Gg]reen)(?:\s{1,5}\w+){0,5}\s{0,5}[A-Z0-9][A-Z0-9][A-Z0-9]?[A-Z0-9]? {1,2}[0-9][A-Z]{2}
See the regex demo.
See the Python demo:
import re
def parse_results(string):
space = r"\s{0,5}"
building_type = r"(?:[Uu]nit|[Ss]tudio|[Ff]lat)?"
street_type = (r"\d+[&-]*\w*\s*"
r"(?:[Ee]nd|[Gg]reen|[Cc]auseway|[Cc]heapside|[Cc]rescent|"
r"[Ss]treet|[Ll]ane|[Ww]alk|[Rr]oad|[Aa]venue|[Dd]rive|"
r"[Pp]ark|[Ww]ay|[Pp]lace|[Pp]arade|[Ii]ndustrial"
r"[Ee]state|[Tt]rading [Ee]state|[Hh]ouse|[Gg]reen)")
postcode = r"[A-Z0-9][A-Z0-9][A-Z0-9]?[A-Z0-9]? {1,2}[0-9][A-Z]{2}"
pattern = re.compile(rf"{building_type}{space}{street_type}(?:\s{{1,5}}\w+){{0,5}}"
rf"{space}{postcode}")
print(pattern.pattern)
try:
return [re.sub(r"\s+", r" ", x) for x in pattern.findall(string)]
except Exception as e:
return (f"Error looking for address, exception {e}")
print(parse_results('Unit 23 End AS4 0SS'))
QUESTION
import os
import random
import time
import math
def stringmanipulator(xy, y=40):
xy= xy.lower()
x = []
x = list(xy)
length = len(x)
y = int(math.floor(length * (y/100)))
while(y):
r =int(random.random()*(length-1))
if(x[r] != '_' and x[r] != ' '):
x[r] = '_'
y = y-1
return x
def printcomplement():
x = int(random.random()*11)
if(x == 0):
print("well done!!")
elif(x == 1):
print("keep going!!")
elif(x == 2):
print("YOU can save him!!")
elif(x == 3):
print("You are the hero no one wanted but everyone deserves.")
elif(x == 4):
print("Genius kid.")
elif(x == 5):
print("You are Smart, not kidding.")
elif(x == 6):
print("You are one who will destroy my carrer using your intellect.")
elif(x == 7):
print("The most kind hearted person I have ever seen till now. Yes I am talking about you")
elif(x == 8):
print("You nailed it.")
elif(x == 9):
print("AND I thought the game was hard.")
elif(x == 10):
print("I will find more difficult words to challenge you with.")
elif(x == 11):
print("How about you put another life on risk after this round.")
def printdis():
x = int(random.random()*11)
if(x == 0):
print("Fool")
elif(x == 1):
print("You will end up killing the fool and then I will hang you next.")
elif(x == 2):
print("What a piece of shit you are.")
elif(x == 3):
print("Hey disgrace to humanity.")
elif(x == 4):
print("Don't cry after the man is dead. You killed him, I gave you a chance to save him.")
elif(x == 5):
print("Dumbass!!")
elif(x == 6):
print("You know what it was my mistake to let such an idiot play.")
elif(x == 7):
print("This is your last game. I don't want fools playing this game.")
elif(x == 8):
print("I see you are already crying.")
elif(x == 9):
print("Even the guy who's life is line is laughing at your stupidity.")
elif(x == 10):
print("My 120 years old grandma has a sharper brain than yours.")
elif(x == 11):
print("Get lost, YOU useless, moronic, unworthy pile of garbage.")
def hangman(i = 0):
if(i == 0):
print("___________")
print("| |")
print("| |")
print("| ")
print("| ")
print("| ")
print("| ")
print("| ")
print("|")
elif(i == 1):
print("___________")
print("| |")
print("| |")
print("| ( ) ")
print("| ")
print("| ")
print("| ")
print("| ")
print("|")
elif(i == 2):
print("___________")
print("| |")
print("| |")
print("| ( ) ")
print("| | ")
print("| | ")
print("| ")
print("| ")
print("|")
elif(i == 3):
print("___________")
print("| |")
print("| |")
print("| ( ) ")
print("| \\ | / ")
print("| | ")
print("| ")
print("| ")
print("|")
elif(i == 4):
print("___________")
print("| |")
print("| |")
print("| \\ ( ) /")
print("| \\ | / ")
print("| ")
print("| ")
print("| ")
print("|")
elif(i == 5):
print("___________")
print("| |")
print("| |")
print("| \\ ( ) /")
print("| \\ | / ")
print("| | ")
print("| / \\")
print("| ")
print("|")
elif(i == 6):
print("___________")
print("| |")
print("| |")
print("| \\ ( ) /")
print("| \\ | / ")
print("| | ")
print("| / \\")
print("| / \\")
print("|")
print("\n\nGAME OVER. You have succesfully killed a person. Better luck next time")
def game(xy, y):
x=[]
i = 0
letter = ''
x = stringmanipulator(xy, y)
xy = xy.lower()
# os.system('cls')
for index in range(len(x)):
if(x[index] == '_'):
while(letter != x[index]):
_= os.system('cls')
hangman(i)
for char in range(len(x)):
print(x[char], end=' ')
print("\n")
letter = input("Enter the letter in the first blank: ")
print(letter+str(i))
if(letter == xy[index]):
print("complement")
x[index] = letter
else:
printdis()
i+=1
dictionary ={}
dictionary["films"] = ["A Space OdysseY", "The GodFather", "Citizen Kane", "Raiders of the lost Ark", "Seven Samurai", "There will be Blood", "Casablanca", "Vertigo", "Notorious", "City Lights"]
dictionary["cities"] = ["Tokyo", "Mecca", "Beijing", "London", "Kolkata", "Washington DC", "Mumbai", "Mexico City", "Delhi", "Shanghai"]
dictionary["fruits"] = ["Damson Plum", "Pomelo", "Blood Orange", "Kumquat", "Blackcurrant", "Acerola", "Avocado", "Pomegrenate", "Apple", "Mango"]
dictionary["country"] = ["Djibouti", "Azerbaijan Azerbaijan,", "Venzuela", "Armenia", "Khazakhstan", "Bangladesh", "Saudi Arabia", "United Kingdom", "United States of America", "India"]
dictionary["flowers"] = ["Monkey Face Orchid", "Naked Man Orchid", "Dancing Girls", "Chamber Maids", "Hibiscus", "Marigold", "Tulip", "Lilies", "Daisy", "Hydrangea"]
print("WELCOME TO THE GAME HANGMAN.\n TAKE THE GAME SERIOUSLY SINCE THE LIFE OF A MAN IS DEPENDING ON YOUR KNOWLEDGE. \n\nI DON'T KNOW HOW MANY CHANCE YOU WILL GET, NOT MANY THAT I CAN CONFIRM.\n SO TRY TO SAVE YOUR FELLOW HUMAN OR LET IT BE MY FOOD. HAHAHAHAHAHAHAHAHAH!!!!!!!")
# x = input("Press 1 for films, 2 for cities, 3 for fruits, 4 for country and 5 for flowers (The most beautiful are usually the hardest): ")
# x = int(x)
x = int(input("Enter a number between 1 and 5: "))
if((x < 1) or(x > 5)):
print("What a moron you are. You couldn't even choose one of the option properly game over good bye, tata, cya")
x = random.randint(1,5)
time.sleep(10)
print("Just kidding you still get to play the game but now I will decide what kind of object you have to guess.")
y = int(input("Enter 40 for easy, 60 for medium and 80 for hard: "))
i = 0
xy = ""
r = random.randint(0,9)
if(x == 1):
xy = dictionary["films"][r]
print("FILMS:")
elif(x == 2):
xy = dictionary["cities"][r]
print("CITIES:")
elif(x == 3):
xy = dictionary["fruits"][r]
print("FRUITS:")
elif(x == 4):
xy = dictionary["country"][r]
print("COUNTRY:")
elif(x == 5):
xy = dictionary["flowers"][r]
print("FLOWERS:")
# hangman(0)
game(xy, y)
Guys I have just started on learning python. In the code below I am asking the user for inputting a letter and checking if the letter matches with the letter in a string xy
then I intend to print a complement by calling the function. However the code is acting beyond my understanding. The code is not at all calling the printcomplement()
until the entire loop is completed(The outermost for loop). Now when the letter is not equal to the intended letter of the string xy
it is not even printing the dis(which I am doing by calling printdis()
). Even the print("complement")
statement is not executing when the condition is hit. Neither is the print(letter+str(i))
statement executing. This function over here is just a part of a larger source code. If any body wants to take a peek at the full code please let me know. Any help will be really appreciated. (I have added the full source code) I have individually checked all the functions except the game()
, since that is the fucntion which controls all the other functions. (ps:- There are some harsh statements written into the code, since this was just a fun project for me. Please don't mind)
ANSWER
Answered 2021-May-31 at 14:43Running your code os.system('cls') is clearing the screen before the input is read in the loop. This makes it seem that there is no output is being displayed when it's really being overwritten.
A quick test can be done to confirm that this is the problem. To do this we add another input read in the game function. like so:
def game(xy, y):
x=[]
i = 0
letter = ''
x = stringmanipulator(xy, y)
xy = xy.lower()
# os.system('cls')
for index in range(len(x)):
if(x[index] == '_'):
while(letter != x[index]):
_= os.system('cls')
hangman(i)
for char in range(len(x)):
print(x[char], end=' ')
print("\n")
letter = input("Enter the letter in the first blank: ")
print(letter+str(i))
if(letter == xy[index]):
print("complement")
x[index] = letter
else:
printdis()
i+=1
input("Press any key to continue: ")
This should change the loop to now look like this:
Now if you run your application with that small change, the output should start showing up because it's not being immediately cleared.
Once you can confirm the this is the problem it should be possible to repair the problem by simply shifting the print statement to be above the input line.
One way you could be like this:
Refactor the dis and complement methods to return a string rather than printing it out directly
Complement method refactored:
complements = [
"well done!!",
"keep going!!",
"YOU can save him!!",
"You are the hero no one wanted but everyone deserves.",
"Genius kid.",
"You are Smart, not kidding.",
"You are one who will destroy my carrier using your intellect.",
"The most kind hearted person I have ever seen till now. Yes I am talking about you",
"You nailed it.",
"AND I thought the game was hard.",
"I will find more difficult words to challenge you with.",
"How about you put another life on risk after this round."
]
def complement() -> str:
return "\n" + complements[int(random.random() * len(complements))] + "\n"
Dis method refactored:
sick_burns = [
"Fool",
"You will end up killing the fool and then I will hang you next.",
"What a piece of shit you are.",
"Hey disgrace to humanity.",
"Don't cry after the man is dead. You killed him, I gave you a chance to save him.",
"Dumbass!!",
"You know what it was my mistake to let such an idiot play."
"This is your last game. I don't want fools playing this game."
"I see you are already crying."
"Even the guy who's life is line is laughing at your stupidity."
"My 120 years old grandma has a sharper brain than yours."
"Get lost, YOU useless, moronic, unworthy pile of garbage."
]
def dis() -> str:
return "\n" + sick_burns[int(random.random() * len(sick_burns))] + "\n"
Game method refactored to print out feedback just before input
def game(xy, y):
x=[]
i = 0
letter = ''
feedback = ''
x = stringmanipulator(xy, y)
xy = xy.lower()
# os.system('cls')
for index in range(len(x)):
if(x[index] == '_'):
while(letter != x[index]):
_= os.system('cls')
hangman(i)
for char in range(len(x)):
print(x[char], end=' ')
print("\n")
print(feedback) # prints feedback of the previous loop
letter = input("Enter the letter in the first blank: ")
print(letter+str(i))
if(letter == xy[index]):
feedback = complement()
x[index] = letter
else:
feedback = dis()
i += 1
Remember feedback is from the previous loop, but it's printed after the for loop wipes the output. You can message me if you have any questions
QUESTION
I have a code (part of it) where I use beautifulsoup to scrape the text from H3
:
company_name = job.find('h3', class_= 'joblist-comp-name').text.strip()
HTML looks like this:
ARK INFOSOFT
(More Jobs)
My Result Looks like this:
Comapny Name: ARK INFOSOFT
(More Jobs)
As I understand, this code grabs the text inside the a
tag which is inside the span
which is inside the h3
. I only wanted the text "ARK INFOSOFT. How can I avoid grabbing any other text within span
's or a
tags in the h3
?
ANSWER
Answered 2021-May-27 at 03:17In order to not get the nested span
:
- Find the class you want.
- Call the
find_next()
method on the found class, which will only return the first found match, and exclude the nestedspan
.
from bs4 import BeautifulSoup
html = """
ARK INFOSOFT
(More Jobs)
"""
soup = BeautifulSoup(html, "html.parser")
company_name = soup.find("h3", class_="joblist-comp-name").find_next(text=True).strip()
Another option: use .contents
:
company_name = soup.find("h3", class_="joblist-comp-name").contents[0].strip()
Output (in both examples):
>>> print(company_name)
ARK INFOSOFT
QUESTION
I need to remove the right icons that are the up and down arrows from a Material UI TextField that I modified from the Material UI documentations (https://material-ui.com/components/autocomplete/#autocomplete) Highlights section.
I tried some solutions from stack overflow like (Remove the arrow and cross that appears for TextField type=“time” material-ui React) and (Remove the arrow and cross that appears for TextField type=“time” material-ui React) but they didn't work and, I ended up with the following code:
App.js:
import React from "react";
import "./styles.css";
import { makeStyles } from "@material-ui/core/styles";
import Autocomplete from "@material-ui/lab/Autocomplete";
import parse from "autosuggest-highlight/parse";
import match from "autosuggest-highlight/match";
import { InputAdornment, TextField } from "@material-ui/core";
import SearchIcon from "@material-ui/icons/Search";
const useStyles = makeStyles(() => ({
noBorder: {
border: "none"
}
}));
export default function Highlights() {
const classes = useStyles();
return (
option.title}
renderInput={(params) => (
),
classes: { notchedOutline: classes.noBorder }
}}
/>
)}
renderOption={(option, { inputValue }) => {
const matches = match(option.title, inputValue);
const parts = parse(option.title, matches);
return (
{parts.map((part, index) => (
{part.text}
))}
);
}}
/>
);
}
// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
{ title: "The Shawshank Redemption", year: 1994 },
{ title: "The Godfather", year: 1972 },
{ title: "The Godfather: Part II", year: 1974 },
{ title: "The Dark Knight", year: 2008 },
{ title: "12 Angry Men", year: 1957 },
{ title: "Schindler's List", year: 1993 },
{ title: "Pulp Fiction", year: 1994 },
{ title: "The Lord of the Rings: The Return of the King", year: 2003 },
{ title: "The Good, the Bad and the Ugly", year: 1966 },
{ title: "Fight Club", year: 1999 },
{ title: "The Lord of the Rings: The Fellowship of the Ring", year: 2001 },
{ title: "Star Wars: Episode V - The Empire Strikes Back", year: 1980 },
{ title: "Forrest Gump", year: 1994 },
{ title: "Inception", year: 2010 },
{ title: "The Lord of the Rings: The Two Towers", year: 2002 },
{ title: "One Flew Over the Cuckoo's Nest", year: 1975 },
{ title: "Goodfellas", year: 1990 },
{ title: "The Matrix", year: 1999 },
{ title: "Seven Samurai", year: 1954 },
{ title: "Star Wars: Episode IV - A New Hope", year: 1977 },
{ title: "City of God", year: 2002 },
{ title: "Se7en", year: 1995 },
{ title: "The Silence of the Lambs", year: 1991 },
{ title: "It's a Wonderful Life", year: 1946 },
{ title: "Life Is Beautiful", year: 1997 },
{ title: "The Usual Suspects", year: 1995 },
{ title: "Léon: The Professional", year: 1994 },
{ title: "Spirited Away", year: 2001 },
{ title: "Saving Private Ryan", year: 1998 },
{ title: "Once Upon a Time in the West", year: 1968 },
{ title: "American History X", year: 1998 },
{ title: "Interstellar", year: 2014 },
{ title: "Casablanca", year: 1942 },
{ title: "City Lights", year: 1931 },
{ title: "Psycho", year: 1960 },
{ title: "The Green Mile", year: 1999 },
{ title: "The Intouchables", year: 2011 },
{ title: "Modern Times", year: 1936 },
{ title: "Raiders of the Lost Ark", year: 1981 },
{ title: "Rear Window", year: 1954 },
{ title: "The Pianist", year: 2002 },
{ title: "The Departed", year: 2006 },
{ title: "Terminator 2: Judgment Day", year: 1991 },
{ title: "Back to the Future", year: 1985 },
{ title: "Whiplash", year: 2014 },
{ title: "Gladiator", year: 2000 },
{ title: "Memento", year: 2000 },
{ title: "The Prestige", year: 2006 },
{ title: "The Lion King", year: 1994 },
{ title: "Apocalypse Now", year: 1979 },
{ title: "Alien", year: 1979 },
{ title: "Sunset Boulevard", year: 1950 },
{
title:
"Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb",
year: 1964
},
{ title: "The Great Dictator", year: 1940 },
{ title: "Cinema Paradiso", year: 1988 },
{ title: "The Lives of Others", year: 2006 },
{ title: "Grave of the Fireflies", year: 1988 },
{ title: "Paths of Glory", year: 1957 },
{ title: "Django Unchained", year: 2012 },
{ title: "The Shining", year: 1980 },
{ title: "WALL·E", year: 2008 },
{ title: "American Beauty", year: 1999 },
{ title: "The Dark Knight Rises", year: 2012 },
{ title: "Princess Mononoke", year: 1997 },
{ title: "Aliens", year: 1986 },
{ title: "Oldboy", year: 2003 },
{ title: "Once Upon a Time in America", year: 1984 },
{ title: "Witness for the Prosecution", year: 1957 },
{ title: "Das Boot", year: 1981 },
{ title: "Citizen Kane", year: 1941 },
{ title: "North by Northwest", year: 1959 },
{ title: "Vertigo", year: 1958 },
{ title: "Star Wars: Episode VI - Return of the Jedi", year: 1983 },
{ title: "Reservoir Dogs", year: 1992 },
{ title: "Braveheart", year: 1995 },
{ title: "M", year: 1931 },
{ title: "Requiem for a Dream", year: 2000 },
{ title: "Amélie", year: 2001 },
{ title: "A Clockwork Orange", year: 1971 },
{ title: "Like Stars on Earth", year: 2007 },
{ title: "Taxi Driver", year: 1976 },
{ title: "Lawrence of Arabia", year: 1962 },
{ title: "Double Indemnity", year: 1944 },
{ title: "Eternal Sunshine of the Spotless Mind", year: 2004 },
{ title: "Amadeus", year: 1984 },
{ title: "To Kill a Mockingbird", year: 1962 },
{ title: "Toy Story 3", year: 2010 },
{ title: "Logan", year: 2017 },
{ title: "Full Metal Jacket", year: 1987 },
{ title: "Dangal", year: 2016 },
{ title: "The Sting", year: 1973 },
{ title: "2001: A Space Odyssey", year: 1968 },
{ title: "Singin' in the Rain", year: 1952 },
{ title: "Toy Story", year: 1995 },
{ title: "Bicycle Thieves", year: 1948 },
{ title: "The Kid", year: 1921 },
{ title: "Inglourious Basterds", year: 2009 },
{ title: "Snatch", year: 2000 },
{ title: "3 Idiots", year: 2009 },
{ title: "Monty Python and the Holy Grail", year: 1975 }
];
styles.css:
.App {
font-family: sans-serif;
text-align: center;
}
/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
/* Firefox */
input[type="search"] {
-moz-appearance: textfield;
}
/* remove outsideof arrows button */
input[type="search"]::-webkit-outside-spin-button {
display: none;
}
ANSWER
Answered 2021-May-14 at 13:22According to this document you need to add freesolo
return (
option.title}
renderInput={(params) => (
),
classes: { notchedOutline: classes.noBorder }
}}
/>
)}
renderOption={(option, { inputValue }) => {
const matches = match(option.title, inputValue);
const parts = parse(option.title, matches);
return (
{parts.map((part, index) => (
{part.text}
))}
);
}}
/>
);
QUESTION
I need to modify the Autocomplete Highlight provided as an example to fit my needs. (https://material-ui.com/components/autocomplete/#autocomplete)
The Highlight example provided has borders so I used the solution from this link (how to remove border in textfield fieldset in material ui) to modify my TextField and remove it's border and it works except that when I type in the search input I don't get the autocomplete suggestions.
I also replaced the Icon, and ended up with the following code:
/* eslint-disable no-use-before-define */
import React from 'react';
import { makeStyles } from "@material-ui/core/styles";
import Autocomplete from '@material-ui/lab/Autocomplete';
import parse from 'autosuggest-highlight/parse';
import match from 'autosuggest-highlight/match';
import { InputAdornment, TextField } from "@material-ui/core";
import SearchIcon from '@material-ui/icons/Search';
const useStyles = makeStyles(() => ({
noBorder: {
border: "none",
},
}));
export default function Highlights() {
const classes = useStyles()
return (
option.title}
renderInput={(params) => (
),
classes:{notchedOutline:classes.noBorder}
}}
/>
//
)}
renderOption={(option, { inputValue }) => {
const matches = match(option.title, inputValue);
const parts = parse(option.title, matches);
return (
{parts.map((part, index) => (
{part.text}
))}
);
}}
/>
);
}
// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
{ title: 'The Shawshank Redemption', year: 1994 },
{ title: 'The Godfather', year: 1972 },
{ title: 'The Godfather: Part II', year: 1974 },
{ title: 'The Dark Knight', year: 2008 },
{ title: '12 Angry Men', year: 1957 },
{ title: "Schindler's List", year: 1993 },
{ title: 'Pulp Fiction', year: 1994 },
{ title: 'The Lord of the Rings: The Return of the King', year: 2003 },
{ title: 'The Good, the Bad and the Ugly', year: 1966 },
{ title: 'Fight Club', year: 1999 },
{ title: 'The Lord of the Rings: The Fellowship of the Ring', year: 2001 },
{ title: 'Star Wars: Episode V - The Empire Strikes Back', year: 1980 },
{ title: 'Forrest Gump', year: 1994 },
{ title: 'Inception', year: 2010 },
{ title: 'The Lord of the Rings: The Two Towers', year: 2002 },
{ title: "One Flew Over the Cuckoo's Nest", year: 1975 },
{ title: 'Goodfellas', year: 1990 },
{ title: 'The Matrix', year: 1999 },
{ title: 'Seven Samurai', year: 1954 },
{ title: 'Star Wars: Episode IV - A New Hope', year: 1977 },
{ title: 'City of God', year: 2002 },
{ title: 'Se7en', year: 1995 },
{ title: 'The Silence of the Lambs', year: 1991 },
{ title: "It's a Wonderful Life", year: 1946 },
{ title: 'Life Is Beautiful', year: 1997 },
{ title: 'The Usual Suspects', year: 1995 },
{ title: 'Léon: The Professional', year: 1994 },
{ title: 'Spirited Away', year: 2001 },
{ title: 'Saving Private Ryan', year: 1998 },
{ title: 'Once Upon a Time in the West', year: 1968 },
{ title: 'American History X', year: 1998 },
{ title: 'Interstellar', year: 2014 },
{ title: 'Casablanca', year: 1942 },
{ title: 'City Lights', year: 1931 },
{ title: 'Psycho', year: 1960 },
{ title: 'The Green Mile', year: 1999 },
{ title: 'The Intouchables', year: 2011 },
{ title: 'Modern Times', year: 1936 },
{ title: 'Raiders of the Lost Ark', year: 1981 },
{ title: 'Rear Window', year: 1954 },
{ title: 'The Pianist', year: 2002 },
{ title: 'The Departed', year: 2006 },
{ title: 'Terminator 2: Judgment Day', year: 1991 },
{ title: 'Back to the Future', year: 1985 },
{ title: 'Whiplash', year: 2014 },
{ title: 'Gladiator', year: 2000 },
{ title: 'Memento', year: 2000 },
{ title: 'The Prestige', year: 2006 },
{ title: 'The Lion King', year: 1994 },
{ title: 'Apocalypse Now', year: 1979 },
{ title: 'Alien', year: 1979 },
{ title: 'Sunset Boulevard', year: 1950 },
{ title: 'Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb', year: 1964 },
{ title: 'The Great Dictator', year: 1940 },
{ title: 'Cinema Paradiso', year: 1988 },
{ title: 'The Lives of Others', year: 2006 },
{ title: 'Grave of the Fireflies', year: 1988 },
{ title: 'Paths of Glory', year: 1957 },
{ title: 'Django Unchained', year: 2012 },
{ title: 'The Shining', year: 1980 },
{ title: 'WALL·E', year: 2008 },
{ title: 'American Beauty', year: 1999 },
{ title: 'The Dark Knight Rises', year: 2012 },
{ title: 'Princess Mononoke', year: 1997 },
{ title: 'Aliens', year: 1986 },
{ title: 'Oldboy', year: 2003 },
{ title: 'Once Upon a Time in America', year: 1984 },
{ title: 'Witness for the Prosecution', year: 1957 },
{ title: 'Das Boot', year: 1981 },
{ title: 'Citizen Kane', year: 1941 },
{ title: 'North by Northwest', year: 1959 },
{ title: 'Vertigo', year: 1958 },
{ title: 'Star Wars: Episode VI - Return of the Jedi', year: 1983 },
{ title: 'Reservoir Dogs', year: 1992 },
{ title: 'Braveheart', year: 1995 },
{ title: 'M', year: 1931 },
{ title: 'Requiem for a Dream', year: 2000 },
{ title: 'Amélie', year: 2001 },
{ title: 'A Clockwork Orange', year: 1971 },
{ title: 'Like Stars on Earth', year: 2007 },
{ title: 'Taxi Driver', year: 1976 },
{ title: 'Lawrence of Arabia', year: 1962 },
{ title: 'Double Indemnity', year: 1944 },
{ title: 'Eternal Sunshine of the Spotless Mind', year: 2004 },
{ title: 'Amadeus', year: 1984 },
{ title: 'To Kill a Mockingbird', year: 1962 },
{ title: 'Toy Story 3', year: 2010 },
{ title: 'Logan', year: 2017 },
{ title: 'Full Metal Jacket', year: 1987 },
{ title: 'Dangal', year: 2016 },
{ title: 'The Sting', year: 1973 },
{ title: '2001: A Space Odyssey', year: 1968 },
{ title: "Singin' in the Rain", year: 1952 },
{ title: 'Toy Story', year: 1995 },
{ title: 'Bicycle Thieves', year: 1948 },
{ title: 'The Kid', year: 1921 },
{ title: 'Inglourious Basterds', year: 2009 },
{ title: 'Snatch', year: 2000 },
{ title: '3 Idiots', year: 2009 },
{ title: 'Monty Python and the Holy Grail', year: 1975 },
];
ANSWER
Answered 2021-May-14 at 01:59In order for autocomplete to work , you also need to pass on the InputProps
down to custom textfield. So I would change your renderInput
function like this:
renderInput={(params) => (
),
classes:{notchedOutline:classes.noBorder}
}}
/>
//
)}
Here is a demo
QUESTION
When trying to run a test a GUI to use with the ARK Taming Calculator I'm working on, GUI test gives me the error
Traceback (most recent call last): File "GUItest.py", line 1, in import PySimpleGUI as sg File "/home/nunya/.local/lib/python2.7/site-packages/PySimpleGUI/init.py, line 2, in from .PySimpleGUI import * File "/home/nunya/.local/lib/python2.7/site-packages/PySimpleGUI.py', line 523 SyntaxError: Non-ASCII character '\xe2' in file /home/nunya/.local/lib/python2.7/site-packages/PySimpleGUI.py on line 523, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
Looking at the site it provides, I don't under stand what it was stating, and havent been able to figure out what is wrong with PySimpleGUI.py
ANSWER
Answered 2021-May-13 at 15:46According to the documentation (under "Python Versions"):
As of 9/25/2018 both Python 3 and Python 2.7 are supported when using
tkinter
version ofPySimpleGUI
! The Python 3 version is namedPySimpleGUI
. The Python 2.7 version isPySimpleGUI27
.
So you should substitute PySimpleGUI
with PySimpleGUI27
(or switch to Python 3).
QUESTION
I have a struct that I'm sending from a C server to a Python client. The C struct is the following one:
// Data structure to be sent to python interface
typedef struct { //
uint32_t componentType; // 4
bool componentId_present; // 1 + 3 padding = 4
uint32_t componentIdType; // 4 + 4 padding = 8
long componentId; // 8
uint32_t componentConfigUpdate_type; // 4 + 4 padding = 8
bool componentConfigUpdate_s1AP_present; // 1 + 3 padding = 4
uint32_t componentConfigUpdate_s1AP_size; // 4
byte componentConfigUpdate_s1AP[128]; // 128
bool componentConfigUpdate_x2AP_present; // 1 + 3 padding = 4
uint32_t componentConfigUpdate_x2AP_size; // 4
byte componentConfigUpdate_x2AP[128]; // 128
} data_E2setupRequest_NodeComponentConfigUpdate_t; // 256 + 3*8 + 6*4 = 256 + 24 + 24 = 304
In Python I compute the size to be received with the following code:
import struct
size = struct.calcsize("i?ili?i128s?i128s") # returns 300
As you can see sizes are different: 304 bytes vs 300 bytes. I have read this on stackoverflow and The Lost Ark of Structure Packing, but I cannot explain why there is such difference with default padding/packing rules.
Anyway I solved by setting the struct in this way (long var one place before):
typedef struct {
uint32_t componentType; // 4
bool componentId_present; // 1 + 3 padding = 4
long componentId; // 8
uint32_t componentIdType; // 4 + 0 padding = 4
uint32_t componentConfigUpdate_type; // 4 + 0 padding = 4
bool componentConfigUpdate_s1AP_present; // 1 + 3 padding = 4
....
} data_E2setupRequest_NodeComponentConfigUpdate_t; // 256 + 8 + 8*4 = 256 + 8 + 32 = 296
and
import struct
size = struct.calcsize("i?lii?i128s?i128s") # returns 296
ANSWER
Answered 2021-Feb-26 at 10:02From the struct
module docs:
Padding is only automatically added between successive structure members. No padding is added at the beginning or the end of the encoded struct.
You've miscalculated the padding for your C structure - assuming typical structure layout and 8-byte longs, sizeof(data_E2setupRequest_NodeComponentConfigUpdate_t
) will be 304 rather than 300, but componentConfigUpdate_s1AP_present
actually goes in the space you thought was padding after componentConfigUpdate_type
. The extra 4 bytes of padding are actually at the end of the structure, and struct
doesn't add that padding.
Also from the struct
module docs:
To align the end of a structure to the alignment requirement of a particular type, end the format with the code for that type with a repeat count of zero.
So if you want struct
to pad the end of the structure to long
alignment, you can add 0l
to the end of the format string.
(Also, bool
is 1 byte, not 4 - there are 3 bytes of padding after all the bools in your struct. You could get rid of some of that padding by putting all the bools next to each other.)
QUESTION
I have datasets like this example, but with 1000 Inputs and 1000 Words for each Input, and 30 values for each Input x Time x Word combination (in cols Copy1..Copy30)
df = read.table(header=T, sep=",", text="
Input,Time,Word,Copy1,Copy2,Copy3,Copy30
ark,1,ark,0.00,0.00,0.00,0.00
ark,1,ad,0.00,0.00,0.00,0.00
ark,1,bark,0.00,0.00,0.00,0.00
ark,50,ark,0.00,0.10,0.05,0.00
ark,50,ad,0.00,0.05,0.03,0.00
ark,50,bark,0.07,0.06,0.00,0.00
ark,100,ark,0.00,0.17,0.55,0.00
ark,100,ad,0.00,0.03,0.11,0.00
ark,100,bark,0.05,0.20,0.00,0.00
bark,1,ark,0.00,0.00,0.00,0.00
bark,1,ad,0.00,0.00,0.00,0.00
bark,1,bark,0.00,0.00,0.00,0.00
bark,50,ark,0.00,0.03,0.09,0.00
bark,50,ad,0.00,0.05,0.03,0.00
bark,50,bark,0.2,0.75,0.00,0.00
bark,100,ark,0.00,0.08,0.32,0.00
bark,100,ad,0.00,0.03,0.11,0.00
bark,100,bark,0.21,0.60,0.00,0.00
") %>% arrange(Input,Time,Word)
df
# Input Time Word Copy1 Copy2 Copy3 Copy30
# 1 ark 1 ad 0.00 0.00 0.00 0
# 2 ark 1 ark 0.00 0.00 0.00 0
# 3 ark 1 bark 0.00 0.00 0.00 0
# 4 ark 50 ad 0.00 0.05 0.03 0
# 5 ark 50 ark 0.00 0.10 0.05 0
# 6 ark 50 bark 0.07 0.06 0.00 0
# 7 ark 100 ad 0.00 0.03 0.11 0
# 8 ark 100 ark 0.00 0.17 0.55 0
# 9 ark 100 bark 0.05 0.20 0.00 0
# 10 bark 1 ad 0.00 0.00 0.00 0
# 11 bark 1 ark 0.00 0.00 0.00 0
# 12 bark 1 bark 0.00 0.00 0.00 0
# 13 bark 50 ad 0.00 0.05 0.03 0
# 14 bark 50 ark 0.00 0.03 0.09 0
# 15 bark 50 bark 0.20 0.75 0.00 0
# 16 bark 100 ad 0.00 0.03 0.11 0
# 17 bark 100 ark 0.00 0.08 0.32 0
# 18 bark 100 bark 0.21 0.60 0.00 0
I want to group by Input and Word, and for each combination, determine which Copy column has the maximum value for each word, and then keep only that column for that Word for that Input. A response to a previous question got me part of the way there. This code identifies which Copy of each Word is the maximum.
max_copy <- df %>%
pivot_longer(starts_with("Copy"), names_to="copy_name", values_to="copy_value") %>%
group_by(Input, Word) %>%
filter(rank(copy_value, ties.method="first") == n()) %>%
group_by(Input, Time)
max_copy
# A tibble: 6 x 5
# Groups: Input, Time [3]
# Input Time Word copy_name copy_value
#
# 1 ark 100 ad Copy3 0.11
# 2 ark 100 ark Copy3 0.55
# 3 ark 100 bark Copy2 0.2
# 4 bark 50 bark Copy2 0.75
# 5 bark 100 ad Copy3 0.11
# 6 bark 100 ark Copy3 0.32
Now what I want to do is use this to reduce the data to the identified copies for each word for each input, so that the result would be:
# A tibble: 18 x 5
# Groups: Input, Time [6]
# Input Time Word copy_name copy_value
#
# 1 ark 1 ad Copy3 0
# 2 ark 1 ark Copy3 0
# 3 ark 1 bark Copy2 0
# 4 ark 50 ad Copy3 0.03
# 5 ark 50 ark Copy3 0.05
# 6 ark 50 bark Copy2 0.06
# 7 ark 100 ad Copy3 0.11
# 8 ark 100 ark Copy3 0.55
# 9 ark 100 bark Copy2 0.2
# 10 bark 1 ad Copy3 0
# 11 bark 1 ark Copy3 0
# 12 bark 1 bark Copy2 0
# 13 bark 50 ad Copy3 0.03
# 14 bark 50 ark Copy3 0.09
# 15 bark 50 bark Copy2 0.75
# 16 bark 100 ad Copy3 0.11
# 17 bark 100 ark Copy3 0.32
# 18 bark 100 bark Copy2 0.6
Is there a method where I can use the max_copy data to reduce df like this?
EDIT: There are problems with some solutions below. @akrun's solution breaks if there are negative values (easy to deal with) or if there are positive values in later Copies than the Copy with the maximum value (I can't see how to fix this). @AnoushiravanR's solution appears to be robust against both conditions, and so do the solutions from @AnilGoyal. Here's an updated dataset with those kinds of conditions included.
df2 = read.table(header=T, sep=",", text="
Input,Time,Word,Copy1,Copy2,Copy3,Copy30
ark,1,ark,0.00,0.00,0.00,-0.01
ark,1,ad,0.00,0.00,0.00,-0.01
ark,1,bark,0.00,0.00,0.00,-0.01
ark,1,bar,0.00,0.00,0.00,-0.01
ark,50,ark,0.00,0.10,0.05,-0.01
ark,50,ad,0.00,0.05,0.03,-0.01
ark,50,bark,0.07,0.06,0.01,-0.01
ark,50,bar,0.07,0.06,0.01,-0.01
ark,100,ark,0.00,0.17,0.55,-0.01
ark,100,ad,0.00,0.03,0.11,-0.01
ark,100,bark,0.05,0.20,0.01,-0.01
ark,100,bar,0.04,0.15,0.01,-0.01
bark,1,ark,0.00,0.00,0.00,-0.01
bark,1,ad,0.00,0.00,0.00,-0.01
bark,1,bark,0.00,0.00,0.00,-0.01
bark,1,bar,0.00,0.00,0.00,-0.01
bark,50,ark,0.00,0.03,0.09,-0.01
bark,50,ad,0.00,0.05,0.03,-0.01
bark,50,bark,0.2,0.75,0.01,0.01
bark,50,bar,0.2,0.7,0.00,-0.01
bark,100,ark,0.00,0.08,0.32,-0.01
bark,100,ad,0.00,0.03,0.11,-0.01
bark,100,bark,0.21,0.60,0.01,-0.01
bark,100,bar,0.15,0.4,0.01,-0.01
") %>% arrange(Input,Time,Word)
Desired output for df2:
# A tibble: 24 x 5
# Input Time Word copy_name Value
#
# 1 ark 1 ad Copy3 0
# 2 ark 1 ark Copy3 0
# 3 ark 1 bar Copy2 0
# 4 ark 1 bark Copy2 0
# 5 ark 50 ad Copy3 0.03
# 6 ark 50 ark Copy3 0.05
# 7 ark 50 bar Copy2 0.06
# 8 ark 50 bark Copy2 0.06
# 9 ark 100 ad Copy3 0.11
# 10 ark 100 ark Copy3 0.55
# 11 ark 100 bar Copy2 0.15
# 12 ark 100 bark Copy2 0.2
# 13 bark 1 ad Copy3 0
# 14 bark 1 ark Copy3 0
# 15 bark 1 bar Copy2 0
# 16 bark 1 bark Copy2 0
# 17 bark 50 ad Copy3 0.03
# 18 bark 50 ark Copy3 0.09
# 19 bark 50 bar Copy2 0.7
# 20 bark 50 bark Copy2 0.75
# 21 bark 100 ad Copy3 0.11
# 22 bark 100 ark Copy3 0.32
# 23 bark 100 bar Copy2 0.4
# 24 bark 100 bark Copy2 0.6
ANSWER
Answered 2021-May-04 at 15:39This can be done with summarise
. After reshaping to 'long' format with pivot_longer
, do a group by 'Input', 'Time' Word', then summarise
to create the 'copy_value' based on a condition that if
all
values are 0 then return 0 or else
return the last
non-zero value of 'copy_value'
library(dplyr)
library(tidyr)
df %>%
pivot_longer(cols = starts_with('Copy'), names_to = 'copy_name',
values_to = 'copy_value') %>%
group_by(Input, Time, Word) %>%
summarise(copy_value = if(all(copy_value == 0)) 0
else last(copy_value[copy_value != 0]), .groups = 'drop')
-output
# A tibble: 18 x 4
# Input Time Word copy_value
# *
# 1 ark 1 ad 0
# 2 ark 1 ark 0
# 3 ark 1 bark 0
# 4 ark 50 ad 0.03
# 5 ark 50 ark 0.05
# 6 ark 50 bark 0.06
# 7 ark 100 ad 0.11
# 8 ark 100 ark 0.55
# 9 ark 100 bark 0.2
#10 bark 1 ad 0
#11 bark 1 ark 0
#12 bark 1 bark 0
#13 bark 50 ad 0.03
#14 bark 50 ark 0.09
#15 bark 50 bark 0.75
#16 bark 100 ad 0.11
#17 bark 100 ark 0.32
#18 bark 100 bark 0.6
If we need the 'copy_name' as well, then use the same logical expression in slice
to return the row that meets the condition i.e. if
all
values are 0, return last row (n()
- doesn't matter) or get the index of last
non-zero of copy_value. Now, we do a group by 'Input', 'Word' and mutate
the 'copy_name' by replacing those with the corresponding 'copy_name' where the 'copy_value' is max
df %>%
pivot_longer(cols = starts_with('Copy'), names_to = 'copy_name',
values_to = 'copy_value') %>%
group_by(Input, Time, Word) %>%
arrange(copy_value) %>%
slice(if(all(copy_value <= 0)) n()
else tail(which(copy_value > 0), 1))%>%
group_by(Input, Word) %>%
mutate(copy_name = copy_name[which.max(copy_value)]) %>%
ungroup
-output
# A tibble: 18 x 5
# Input Time Word copy_name copy_value
#
# 1 ark 1 ad Copy3 0
# 2 ark 1 ark Copy3 0
# 3 ark 1 bark Copy2 0
# 4 ark 50 ad Copy3 0.03
# 5 ark 50 ark Copy3 0.05
# 6 ark 50 bark Copy2 0.06
# 7 ark 100 ad Copy3 0.11
# 8 ark 100 ark Copy3 0.55
# 9 ark 100 bark Copy2 0.2
#10 bark 1 ad Copy3 0
#11 bark 1 ark Copy3 0
#12 bark 1 bark Copy2 0
#13 bark 50 ad Copy3 0.03
#14 bark 50 ark Copy3 0.09
#15 bark 50 bark Copy2 0.75
#16 bark 100 ad Copy3 0.11
#17 bark 100 ark Copy3 0.32
#18 bark 100 bark Copy2 0.6
QUESTION
- I have data files from word recognition simulations with neural networks.
- The network has a lexicon of 1000 words.
- The output has 30,000 nodes -- 30 copies of each word at a different alignment.
- I run separate simulations with each word as the Input (1000 simulations). The output is a 100-step time series like this (here showing 2 Input examples (ark, bark), with 4 of the Copies for each word, tracking only 4 words, and showing only 5 time steps)
[Edit: 3 May 2021, dataset now includes realistic conditions that previous solutions could not handle. I apologize for changing the data, but I don't see a better way to clarify the gap in the previously suggested solution.]
xf = read.table(header=T, sep=",", text="
Input,Time,Word,Copy1,Copy2,Copy3,Copy30
ark,10,ark,-0.1,-0.1,-0.1,-0.1
ark,20,ark,0.0,0.5,0.55,0.01
ark,30,ark,0.01,0.1,0.2,0.05
ark,40,ark,0.02,0.3,0.5,0.1
ark,50,ark,0.01,0.2,0.4,-0.1
ark,10,ad,-0.1,-0.1,-0.1,-0.1
ark,20,ad,0.0,0.01,0.02,0.01
ark,30,ad,0.01,0.03,0.1,0.04
ark,40,ad,0.02,0.12,0.15,0.04
ark,50,ad,0.01,0.01,0.05,0.02
ark,10,bark,-0.1,-0.1,-0.1,-0.1
ark,20,bark,0.02,0.12,0.1,0.01
ark,30,bark,0.03,0.15,0.12,0.02
ark,40,bark,0.02,0.22,0.1,0.03
ark,50,bark,0.01,0.1,0.05,0.02
ark,10,bar,-0.1,-0.1,-0.1,-0.1
ark,20,bar,0.01,0.1,0.02,-0.05
ark,30,bar,0.01,0.12,0.03,0
ark,40,bar,0.02,0.15,0.03,0.01
ark,50,bar,0.01,0.05,0.02,0.01
bark,10,ark,-0.1,-0.1,-0.1,-0.1
bark,20,ark,0.0,0.04,0.05,0.01
bark,30,ark,0.01,0.08,0.1,0.05
bark,40,ark,0.02,0.05,0.2,0.1
bark,50,ark,0.01,0.01,0.3,-0.1
bark,10,ad,-0.1,-0.1,-0.1,-0.1
bark,20,ad,0.0,0.01,0.01,0.01
bark,30,ad,0.01,0.02,0.05,0.04
bark,40,ad,0.02,0.03,0.06,0.04
bark,50,ad,0.01,0.02,0.01,0.02
bark,10,bark,-0.1,-0.1,-0.1,-0.1
bark,20,bark,0.02,0.15,0.1,0.01
bark,30,bark,0.03,0.3,0.12,0.02
bark,40,bark,0.02,0.7,0.1,0.03
bark,50,bark,0.01,0.7,0.05,0.02
bark,10,bar,-0.1,-0.1,-0.1,-0.1
bark,20,bar,0.01,0.13,0.04,-0.05
bark,30,bar,0.01,0.25,0.06,0
bark,40,bar,0.02,0.4,0.08,0.01
bark,50,bar,0.01,0.35,0.01,0.01
") %>% arrange(Input,Word,Time)
I want to reduce this data in 2 ways.
(1) For each Input x Word combination, select one Copy for a word based on maximum values over the whole time series, and
(2) based on maximum values for the retained Copies (1 per Input x Word), reduce to the 'topX' Words.
My original question was unclear and became very unwieldy. @DanChaltiel provided partial answers using pivot_longer that came quite close to the completely solution, but I couldn't explain the first reduction clearly enough. So I broke that into a separate question where @akrun extended @DanChaltiel's solution like this, solving the first part (updated on 3 May 2021 to reflect repairs to the solution):
library(tidyverse)
# Reduce data to one Copy of each Input x Word combination
# based on maxima for entire time series, no matter what
# Time those maxima occur. Using pivot_longer was due to
# answer from @DanChaltiel, but getting it to work on
# Input x Word maxima over the whole time series (rather
# than maxima of Input x Word x Time) was due to @akrun
# for https://stackoverflow.com/questions/67351185/
xf2 <- xf %>%
pivot_longer(cols = starts_with('Copy'), names_to = 'copy_name',
values_to = 'Value') %>%
group_by(Input, Time, Word) %>%
arrange(Value) %>%
slice(if(all(Value <= 0)) n()
else tail(which(Value > 0), 1))%>%
group_by(Input, Word) %>%
mutate(copy_name = copy_name[which.max(Value)]) %>%
ungroup
print((xf2 %>% arrange(Input, Word)), n = nrow(xf2)) # print all rows
# A tibble: 40 x 5
# Input Time Word copy_name Value
#
# 1 ark 10 ad Copy3 -0.1
# 2 ark 20 ad Copy3 0.02
# 3 ark 30 ad Copy3 0.1
# 4 ark 40 ad Copy3 0.15
# 5 ark 50 ad Copy3 0.05
# 6 ark 10 ark Copy3 -0.1
# 7 ark 20 ark Copy3 0.55
# 8 ark 30 ark Copy3 0.2
# 9 ark 40 ark Copy3 0.5
# 10 ark 50 ark Copy3 0.4
# 11 ark 10 bar Copy2 -0.1
# 12 ark 20 bar Copy2 0.1
# 13 ark 30 bar Copy2 0.12
# 14 ark 40 bar Copy2 0.15
# 15 ark 50 bar Copy2 0.05
# 16 ark 10 bark Copy2 -0.1
# 17 ark 20 bark Copy2 0.12
# 18 ark 30 bark Copy2 0.15
# 19 ark 40 bark Copy2 0.22
# 20 ark 50 bark Copy2 0.1
# 21 bark 10 ad Copy3 -0.1
# 22 bark 20 ad Copy3 0.01
# 23 bark 30 ad Copy3 0.05
# 24 bark 40 ad Copy3 0.06
# 25 bark 50 ad Copy3 0.02
# 26 bark 10 ark Copy3 -0.1
# 27 bark 20 ark Copy3 0.05
# 28 bark 30 ark Copy3 0.1
# 29 bark 40 ark Copy3 0.2
# 30 bark 50 ark Copy3 0.3
# 31 bark 10 bar Copy2 -0.1
# 32 bark 20 bar Copy2 0.13
# 33 bark 30 bar Copy2 0.25
# 34 bark 40 bar Copy2 0.4
# 35 bark 50 bar Copy2 0.35
# 36 bark 10 bark Copy2 -0.1
# 37 bark 20 bark Copy2 0.15
# 38 bark 30 bark Copy2 0.3
# 39 bark 40 bark Copy2 0.7
# 40 bark 50 bark Copy2 0.7
So this succeeds in reducing the data to a single Copy for each Input x Word combination based on the maximum values in the Time 1..100 series.
The second challenge is to reduce that data to only the topX words per Input.
The approach @AnilGoyal suggested worked for simpler sample data, but due to an accidental contingency between the number of time steps included and the value of topX.
What I have been able to do so far, based on @AnilGoyal's example, is to identify the topX Words for each Input based on their maximum values. Here are 2 examples finding the top 3 and the top 2:
topX = 3
xftop3 <- xf2 %>% group_by(Input, Word) %>%
slice_max(Value, with_ties=FALSE) %>%
arrange(desc(Value)) %>%
group_by(Input) %>%
filter(1:n() <= topX) %>%
arrange(Input, Value)
xftop3
# A tibble: 6 x 5
# Groups: Input [2]
# Input Time Word copy_name Value
#
# 1 ark 40 ad Copy3 0.15
# 2 ark 40 bark Copy2 0.22
# 3 ark 20 ark Copy3 0.55
# 4 bark 50 ark Copy3 0.3
# 5 bark 40 bar Copy2 0.4
# 6 bark 40 bark Copy2 0.7
topX = 2
xftop2 <- xf2 %>% group_by(Input, Word) %>%
slice_max(Value, with_ties=FALSE) %>%
arrange(desc(Value)) %>%
group_by(Input) %>%
filter(1:n() <= topX) %>%
arrange(Input, Value)
xftop2
# A tibble: 4 x 5
# Groups: Input [2]
# Input Time Word copy_name Value
#
# 1 ark 40 bark Copy2 0.22
# 2 ark 20 ark Copy3 0.55
# 3 bark 40 bar Copy2 0.4
# 4 bark 40 bark Copy2 0.7
What I can't figure out how to do is then to use that tibble to reduce the data set to only those Input x Word combinations at all Times. The desired output for the sample data and topX = 2 would be:
# A tibble: 20 x 5
Input Time Word copy_name Value
1 ark 10 ark Copy3 -0.1
2 ark 20 ark Copy3 0.55
3 ark 30 ark Copy3 0.2
4 ark 40 ark Copy3 0.5
5 ark 50 ark Copy3 0.4
6 ark 10 bark Copy2 -0.1
7 ark 20 bark Copy2 0.12
8 ark 30 bark Copy2 0.15
9 ark 40 bark Copy2 0.22
10 ark 50 bark Copy2 0.1
11 bark 10 bar Copy2 -0.1
12 bark 20 bar Copy2 0.13
13 bark 30 bar Copy2 0.25
14 bark 40 bar Copy2 0.4
15 bark 50 bar Copy2 0.35
16 bark 10 bark Copy2 -0.1
17 bark 20 bark Copy2 0.15
18 bark 30 bark Copy2 0.3
19 bark 40 bark Copy2 0.7
20 bark 50 bark Copy2 0.7
I would be grateful for any advice.
ANSWER
Answered 2021-May-04 at 06:56This answer has been rewritten (twice), see the edition log for the record.
There are two steps to your problem:
-
- find the max values
-
- select the rows that pertain to those max values
Finding the max value is a simple matter of filtering. However, you might have wanted to select the Input-Word pairs based on the mean/median across all times, instead of the occurrence of the max on a single time. This would be a matter of summarising (dplyr::summarise()
.
Once you have the pairs, you simply need to select the right rows. There might be lots of ways, but I chose to use right_join()
.
I chose to separate these steps for didactic purpose, but you can obviously merge them into one pipeline.
topX=2
xf2bis = xf2 %>%
group_by(Input, Word) %>%
filter(rank(Value, ties.method="first") == n()) %>%
group_by(Input) %>%
filter(rank(Value, ties.method="first") > n() - topX) %>%
select(Input, Word)
xf2bis
#> # A tibble: 4 x 2
#> # Groups: Input [2]
#> Input Word
#>
#> 1 ark ark
#> 2 ark bark
#> 3 bark bar
#> 4 bark bark
xftop2 = xf2 %>%
right_join(xf2bis, by=c("Input", "Word"))
xftop2
#> # A tibble: 20 x 5
#> Input Time Word copy_name Value
#>
#> 1 ark 10 ark Copy3 -0.1
#> 2 ark 10 bark Copy2 -0.1
#> 3 ark 20 ark Copy3 0.55
#> 4 ark 20 bark Copy2 0.12
#> 5 ark 30 ark Copy3 0.2
#> 6 ark 30 bark Copy2 0.15
#> 7 ark 40 ark Copy3 0.5
#> 8 ark 40 bark Copy2 0.22
#> 9 ark 50 ark Copy3 0.4
#> 10 ark 50 bark Copy2 0.1
#> 11 bark 10 bar Copy2 -0.1
#> 12 bark 10 bark Copy2 -0.1
#> 13 bark 20 bar Copy2 0.13
#> 14 bark 20 bark Copy2 0.15
#> 15 bark 30 bar Copy2 0.25
#> 16 bark 30 bark Copy2 0.3
#> 17 bark 40 bar Copy2 0.4
#> 18 bark 40 bark Copy2 0.7
#> 19 bark 50 bar Copy2 0.35
#> 20 bark 50 bark Copy2 0.7
Created on 2021-05-04 by the reprex package (v2.0.0)
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install ARK
Support
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesExplore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits
Save this library and start creating your kit
Share this Page