Skip to content

Commit

Permalink
add querykey
Browse files Browse the repository at this point in the history
  • Loading branch information
AImager committed Nov 4, 2017
1 parent 6bacb54 commit f682d2a
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 63 deletions.
Binary file added atom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions bak/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace alfmarks;

class Query {

public $term;

public $regex;

public $accuracy = 0.5;

public function __construct($term) {
$this->term = $term;
}

public function multiScore(array $words) {
$max = 0;
foreach ($words as $word) {
$max = max($max, $this->score($word));
}
return $max;
}

public function score($string) {
preg_match($this->regex(), $string, $matches);
$matched_chars = array_slice(array_values(array_filter($matches, 'strlen')), 2);
if (empty($matches)) return;
// How many characters you matched from the term
$positive_score = strlen(implode('', $matched_chars)) / strlen($this->term);
// Deductions for longer strings
$negative_score = abs(strlen($matches[1]) - strlen($this->term)) / 100;
return $positive_score - $negative_score;
}

public function regex() {
if (!$this->regex) {
$this->regex = '/.*?((';
$this->regex .= implode(')).*?|.*?((', array_map(function($gram) {
$gram_chars = str_split($gram);
return implode(').*?(', array_map(function($gram_char) {
return preg_quote($gram_char);
}, $gram_chars));
}, $this->grams()));
$this->regex .= ')).*?/i';
}
return $this->regex;
}

public function grams() {
$max = strlen($this->term);
$min = ceil($this->accuracy * $max);
$grams = array();
foreach (range($max, $min) as $length) {
$grams = array_merge($grams, $this->gramsByLength($length));
}
return $grams;
}

public function gramsByLength($length) {
$ngrams = array();
$stop = strlen($this->term) - $length;
foreach (range(0, $stop) as $pos) {
$ngrams[] = substr($this->term, $pos, $length);
}
return $ngrams;
}

}
195 changes: 195 additions & 0 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,47 @@
<string></string>
<key>connections</key>
<dict>
<key>055ECEC4-B7F5-4100-8CDE-A023FF61A467</key>
<array>
<dict>
<key>destinationuid</key>
<string>2E4D25EE-2CC9-4E3F-BC71-A6A7D83A7357</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
<string></string>
<key>vitoclose</key>
<false/>
</dict>
</array>
<key>2E4D25EE-2CC9-4E3F-BC71-A6A7D83A7357</key>
<array/>
<key>C1563D58-8291-4E0F-A111-35D965760584</key>
<array>
<dict>
<key>destinationuid</key>
<string>2E4D25EE-2CC9-4E3F-BC71-A6A7D83A7357</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
<string></string>
<key>vitoclose</key>
<false/>
</dict>
</array>
<key>ED8263F6-7B34-41EF-9127-43C32B9F1243</key>
<array>
<dict>
<key>destinationuid</key>
<string>2E4D25EE-2CC9-4E3F-BC71-A6A7D83A7357</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
<string></string>
<key>vitoclose</key>
<false/>
</dict>
</array>
<key>FA97111C-147E-495D-8A96-BB0E6F708500</key>
<array>
<dict>
Expand Down Expand Up @@ -100,18 +139,174 @@
<key>version</key>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>alfredfiltersresults</key>
<false/>
<key>argumenttrimmode</key>
<integer>0</integer>
<key>argumenttype</key>
<integer>0</integer>
<key>escaping</key>
<integer>127</integer>
<key>keyword</key>
<string>atom</string>
<key>queuedelaycustom</key>
<integer>1</integer>
<key>queuedelayimmediatelyinitially</key>
<false/>
<key>queuedelaymode</key>
<integer>0</integer>
<key>queuemode</key>
<integer>1</integer>
<key>runningsubtext</key>
<string></string>
<key>script</key>
<string>PROFILE="atom;~/.atom/projects.cson" CURRENT_FOLDER=`osascript src/get_current_folder.applescript` EDITOR="atom" php src/index.php {query}</string>
<key>scriptargtype</key>
<integer>0</integer>
<key>scriptfile</key>
<string></string>
<key>subtext</key>
<string></string>
<key>title</key>
<string>open project in atom</string>
<key>type</key>
<integer>0</integer>
<key>withspace</key>
<true/>
</dict>
<key>type</key>
<string>alfred.workflow.input.scriptfilter</string>
<key>uid</key>
<string>ED8263F6-7B34-41EF-9127-43C32B9F1243</string>
<key>version</key>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>alfredfiltersresults</key>
<false/>
<key>argumenttrimmode</key>
<integer>0</integer>
<key>argumenttype</key>
<integer>0</integer>
<key>escaping</key>
<integer>127</integer>
<key>keyword</key>
<string>vscode</string>
<key>queuedelaycustom</key>
<integer>1</integer>
<key>queuedelayimmediatelyinitially</key>
<false/>
<key>queuedelaymode</key>
<integer>0</integer>
<key>queuemode</key>
<integer>1</integer>
<key>runningsubtext</key>
<string></string>
<key>script</key>
<string>PROFILE="vscode;~/Library/Application Support/Code/User/projects.json" CURRENT_FOLDER=`osascript src/get_current_folder.applescript` EDITOR="vscode" php src/index.php {query}</string>
<key>scriptargtype</key>
<integer>0</integer>
<key>scriptfile</key>
<string></string>
<key>subtext</key>
<string></string>
<key>title</key>
<string>open project in vscode</string>
<key>type</key>
<integer>0</integer>
<key>withspace</key>
<true/>
</dict>
<key>type</key>
<string>alfred.workflow.input.scriptfilter</string>
<key>uid</key>
<string>C1563D58-8291-4E0F-A111-35D965760584</string>
<key>version</key>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>alfredfiltersresults</key>
<false/>
<key>argumenttrimmode</key>
<integer>0</integer>
<key>argumenttype</key>
<integer>0</integer>
<key>escaping</key>
<integer>127</integer>
<key>keyword</key>
<string>code</string>
<key>queuedelaycustom</key>
<integer>1</integer>
<key>queuedelayimmediatelyinitially</key>
<false/>
<key>queuedelaymode</key>
<integer>0</integer>
<key>queuemode</key>
<integer>1</integer>
<key>runningsubtext</key>
<string></string>
<key>script</key>
<string>PROFILE="vscode;~/Library/Application Support/Code/User/projects.json" CURRENT_FOLDER=`osascript src/get_current_folder.applescript` EDITOR="vscode" php src/index.php {query}</string>
<key>scriptargtype</key>
<integer>0</integer>
<key>scriptfile</key>
<string></string>
<key>subtext</key>
<string></string>
<key>title</key>
<string>open project in vscode</string>
<key>type</key>
<integer>0</integer>
<key>withspace</key>
<true/>
</dict>
<key>type</key>
<string>alfred.workflow.input.scriptfilter</string>
<key>uid</key>
<string>055ECEC4-B7F5-4100-8CDE-A023FF61A467</string>
<key>version</key>
<integer>2</integer>
</dict>
</array>
<key>readme</key>
<string></string>
<key>uidata</key>
<dict>
<key>055ECEC4-B7F5-4100-8CDE-A023FF61A467</key>
<dict>
<key>xpos</key>
<integer>240</integer>
<key>ypos</key>
<integer>580</integer>
</dict>
<key>2E4D25EE-2CC9-4E3F-BC71-A6A7D83A7357</key>
<dict>
<key>xpos</key>
<integer>530</integer>
<key>ypos</key>
<integer>120</integer>
</dict>
<key>C1563D58-8291-4E0F-A111-35D965760584</key>
<dict>
<key>xpos</key>
<integer>240</integer>
<key>ypos</key>
<integer>430</integer>
</dict>
<key>ED8263F6-7B34-41EF-9127-43C32B9F1243</key>
<dict>
<key>xpos</key>
<integer>230</integer>
<key>ypos</key>
<integer>280</integer>
</dict>
<key>FA97111C-147E-495D-8A96-BB0E6F708500</key>
<dict>
<key>xpos</key>
Expand Down
Binary file modified media/vscode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 4 additions & 63 deletions src/node/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,8 @@
namespace alfmarks;

class Query {

public $term;

public $regex;

public $accuracy = 0.5;

public function __construct($term) {
$this->term = $term;
}

public function multiScore(array $words) {
$max = 0;
foreach ($words as $word) {
$max = max($max, $this->score($word));
}
return $max;
}

public function score($string) {
preg_match($this->regex(), $string, $matches);
$matched_chars = array_slice(array_values(array_filter($matches, 'strlen')), 2);
if (empty($matches)) return;
// How many characters you matched from the term
$positive_score = strlen(implode('', $matched_chars)) / strlen($this->term);
// Deductions for longer strings
$negative_score = abs(strlen($matches[1]) - strlen($this->term)) / 100;
return $positive_score - $negative_score;
static function get_score($term, $string) {
foreach()
}

public function regex() {
if (!$this->regex) {
$this->regex = '/.*?((';
$this->regex .= implode(')).*?|.*?((', array_map(function($gram) {
$gram_chars = str_split($gram);
return implode(').*?(', array_map(function($gram_char) {
return preg_quote($gram_char);
}, $gram_chars));
}, $this->grams()));
$this->regex .= ')).*?/i';
}
return $this->regex;
}

public function grams() {
$max = strlen($this->term);
$min = ceil($this->accuracy * $max);
$grams = array();
foreach (range($max, $min) as $length) {
$grams = array_merge($grams, $this->gramsByLength($length));
}
return $grams;
}

public function gramsByLength($length) {
$ngrams = array();
$stop = strlen($this->term) - $length;
foreach (range(0, $stop) as $pos) {
$ngrams[] = substr($this->term, $pos, $length);
}
return $ngrams;
}

}
}

Binary file added vscode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f682d2a

Please sign in to comment.