i am currently working on P. Giacomelli's great Mahout Cookbook and I am stuck with a problem of deprecated code.
I am building the Text NaiveBayesClassifier using the code and It is written:
final BayesParameters params = new BayesParameters();
params.setGramSize( 1 );
params.set( "verbose", "true" );
params.set( "classifierType", "bayes" );
params.set( "defaultCat", "OTHER" );
params.set( "encoding", "UTF-8" );
params.set( "alpha_i", "1.0" );
params.set( "dataSource", "hdfs" );
params.set( "basePath", "/tmp/output" );
try {
Path input = new Path( "/tmp/input" );
TrainClassifier.trainNaiveBayes( input, "/tmp/output",params );
Algorithm algorithm = new BayesAlgorithm();
Datastore datastore = new InMemoryBayesDatastore( params );
ClassifierContext classifier = new ClassifierContext( algorithm, datastore );
classifier.initialize();
final BufferedReader reader = new BufferedReader( new FileReader( args[ 0 ] ) );
String entry = reader.readLine();
while( entry != null ) {
List< String > document = new NGrams( entry, Integer.parseInt( params.get( "gramSize" ) ) ).generateNGramsWithoutLabel();
ClassifierResult result = classifier.classifyDocument(document.toArray( new String[ document.size() ]),params.get( "defaultCat" ) );
entry = reader.readLine();
}
} catch( final IOException ex ) {
ex.printStackTrace();
} catch( final InvalidDatastoreException ex ) {
ex.printStackTrace();}
My problem is that I can't find the classes BayesParameters, Datastore, TrainClassifier etc... Looks like they are deprecated. Can someone write me the equivalent with the modern classes, would be awesome?!
Related
A few years ago I wrote a simple wrapper based on MSDN - AesManaged Class code, to obscure values saved in registry (simply to prevent manual tampering with these, nothing more):
public static string Encrypt( string s, byte[] key, byte[] iv )
{
byte[] enc;
using( AesManaged aes = new AesManaged( ) )
{
ICryptoTransform ict = aes.CreateEncryptor( key, iv );
using( MemoryStream ms= new MemoryStream( ) )
using( CryptoStream cs= new CryptoStream( ms, ict, CryptoStreamMode.Write ) )
using( StreamWriter sw= new StreamWriter( cs ) )
{
sw.Write( s ); enc = ms.ToArray( );
}
}
return Convert.ToBase64String( enc );
}
public static string Decrypt( string p, byte[] key, byte[] iv )
{
string s= null;
using( AesManaged aes = new AesManaged( ) )
{
ICryptoTransform ict = aes.CreateDecryptor( key, iv );
using( MemoryStream ms= new MemoryStream( Convert.FromBase64String( p ) ) )
using( CryptoStream cs= new CryptoStream( ms, ict, CryptoStreamMode.Read ) )
using( StreamReader sr= new StreamReader( cs ) )
{
s= sr.ReadToEnd( );
}
}
return s;
}
These methods worked perfectly all this time .. until yesterday, when Encrypt produced a null result on a valid string. Changing key and iv does not make any difference. Tried executing on several machines - same result. No exceptions are thrown. However, decryption still works fine!
Why does Encrypt( ) suddenly fail? Is there some Windows Update that changed the play-field?
After finding and studying several similar questions (Aes decryptor gives empty string; Using AES encryption in .NET - CryptographicException saying the padding is invalid and cannot be removed; “Padding is invalid and cannot be removed” using AesManaged; Padding is invalid and cannot be removed Exception while decrypting string using “AesManaged” C#) and looking at my code again, I noticed the difference with MSDN sample. Indeed, i made an optimization and that is, what broke the execution! The code must be spelled this way:
public static string Encrypt( string s, byte[] key, byte[] iv )
{
byte[] enc;
using( AesManaged aes = new AesManaged( ) )
{
ICryptoTransform ict = aes.CreateEncryptor( key, iv );
using( MemoryStream ms= new MemoryStream( ) )
{
using( CryptoStream cs= new CryptoStream( ms, ict, CryptoStreamMode.Write ) )
{
using( StreamWriter sw= new StreamWriter( cs ) )
{
sw.Write( s );
}
}
enc = ms.ToArray( );
}
}
return Convert.ToBase64String( enc );
}
Notice the presence of curly braces after each using(..)! Yes, that means the CryptoStream is closed - and therefore flushed - before i attempt to use the buffer, making this approach safe.
No idea, why solution by #GregS and #HansPassant did not do it, but since the code works now (reverted to original version :), my issue is closed. Thank god for version control! :))
Thank you guys for guiding me to the solution!
I have installed a plugin from this question -->
Show most recent search terms in wordpress. I have the plugin working on my site, but i would like to ask that, how to clear the recent searched keywords that displaying on my website. I am running out of idea how to make it happen.
As you can see the result from my site: www.ncc.my. There are now 10 keywords on top of search box. I want to clear all the keyword. I have searched over the google but yet to get the answer. Here is the php code of the widget. See if you can get the solution to clear the keywords. Thanks!
<?php
/*
Plugin Name: Recent Searches Widget
Plugin URI: http://www.poradnik-webmastera.com/projekty/recent_searches_widget/
Description: Shows recent searches in a sidebar widget.
Author: Daniel Frużyński
Version: 1.2
Author URI: http://www.poradnik-webmastera.com/
Text Domain: recent-searches-widget
*/
/* Copyright 2009-2010 Daniel Frużyński (email : daniel [A-T] poradnik-webmastera.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( !class_exists( 'RecentSearchesWidget' ) ) {
class RecentSearchesWidget {
// Constructor
function RecentSearchesWidget() {
// Initialize plugin
add_action( 'init', array( &$this, 'init' ) );
// Page load
add_action( 'template_redirect', array( &$this, 'template_redirect' ) );
// Widgets initialization
add_action( 'widgets_init', array( &$this, 'widgets_init' ) );
}
// Plugin initialization
function init() {
load_plugin_textdomain( 'recent-searches-widget', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
}
// Page load
function template_redirect() {
if ( is_search() ) {
// Store search term
$query = $this->strtolower( trim( get_search_query() ) );
$options = get_option( 'recent_searches_widget' );
if ( !is_array( $options ) ) {
$options = $this->get_default_options();
}
$max = $options['max'];
$data = get_option( 'recent_searches_widget_data', array() );
if ( !is_array( $data ) ) {
if ( isset( $options['data'] ) ) {
$data = $options['data'];
unset( $options['data'] );
update_option( 'recent_searches_widget', $options );
}
if ( !is_array( $data ) ) {
$data = array();
}
}
$pos = array_search( $query, $data );
if ( $pos !== false ) {
if ( $pos != 0 ) {
$data = array_merge( array_slice( $data, 0, $pos ),
array( $query ), array_slice( $data, $pos + 1 ) );
}
} else {
array_unshift( $data, $query );
if ( count( $data ) > $max ) {
array_pop( $data );
}
}
update_option( 'recent_searches_widget_data', $data );
}
}
// Widgets initialization
function widgets_init() {
$widget_ops = array(
'classname' => 'widget_rsw',
'description' => __('Shows recent searches', 'recent-searches-widget'),
);
wp_register_sidebar_widget( 'recentsearcheswidget', __('Recent Searches', 'recent-searches-widget'),
array( &$this, 'widget_rsw' ), $widget_ops );
wp_register_widget_control( 'recentsearcheswidget', __('Recent Searches', 'recent-searches-widget'),
array( &$this, 'widget_rsw_control' ) );
}
function widget_rsw( $args ) {
extract( $args );
$title = isset( $options['title'] ) ? $options['title'] : '';
$title = apply_filters( 'widget_title', $title );
if ( empty($title) )
$title = ' ';
echo $before_widget . $before_title . $title . $after_title, "\n";
$this->show_recent_searches( "<ul>\n<li>", "</li>\n</ul>", "</li>\n<li>" );
echo $after_widget;
}
function show_recent_searches( $before_list, $after_list, $between_items ) {
$options = get_option( 'recent_searches_widget' );
if ( !is_array( $options ) ) {
$options = $this->get_default_options();
}
$data = get_option( 'recent_searches_widget_data' );
if ( !is_array( $data ) ) {
if ( isset( $options['data'] ) ) {
$data = $options['data'];
}
if ( !is_array( $data ) ) {
$data = array();
}
}
if ( count( $data ) > 0 ) {
echo $before_list;
$first = true;
foreach ( $data as $search ) {
if ( $first ) {
$first = false;
} else {
echo $between_items;
}
echo '<a href="', get_search_link( $search ), '"';
if ( $options['nofollow'] ) {
echo ' rel="nofollow"';
}
echo '>', wp_specialchars( $search ), '</a>';
}
echo $after_list, "\n";
} else {
_e('No searches yet', 'recent-searches-widget');
}
}
function widget_rsw_control() {
$options = $newoptions = get_option('recent_searches_widget', array() );
if ( count( $options ) == 0 ) {
$options = $this->get_default_options();
update_option( 'recent_searches_widget', $options );
}
if ( isset( $_POST['rsw-submit'] ) ) {
$options['title'] = strip_tags( stripslashes( $_POST['rsw-title'] ) );
$options['max'] = (int)( $_POST['rsw-max'] );
$options['nofollow'] = isset( $_POST['rsw-nofollow'] );
if ( count( $options['data'] ) > $options['max'] ) {
$options['data'] = array_slice( $options['data'], 0, $options['max'] );
}
update_option( 'recent_searches_widget', $options );
}
$title = attribute_escape( $options['title'] );
$max = attribute_escape( $options['max'] );
$nofollow = $options['nofollow'];
?>
<p><label for="rsw-title"><?php _e('Title:', 'recent-searches-widget'); ?> <input class="widefat" id="rsw-title" name="rsw-title" type="text" value="<?php echo $title; ?>" /></label></p>
<p><label for="rsw-max"><?php _e('Max searches:', 'recent-searches-widget'); ?> <input id="rsw-max" name="rsw-max" type="text" size="3" maxlength="5" value="<?php echo $max; ?>" /></label></p>
<p><label for="rsw-nofollow"><?php _e('Add <code>rel="nofollow"</code> to links:', 'recent-searches-widget'); ?> <input id="rsw-nofollow" name="rsw-nofollow" type="checkbox" value="yes" <?php checked( $nofollow, true ); ?>" /></label></p>
<input type="hidden" id="rsw-submit" name="rsw-submit" value="1" />
<?php
}
// Make string lowercase
function strtolower( $str ) {
if ( function_exists( 'mb_strtolower' ) ) {
return mb_strtolower( $str );
} else {
return strtolower( $str );
}
}
function get_default_options() {
return array(
'title' => '',
'max' => 4,
'nofollow' => true,
);
}
}
// Add functions from WP2.8 for previous WP versions
if ( !function_exists( 'esc_html' ) ) {
function esc_html( $text ) {
return wp_specialchars( $text );
}
}
if ( !function_exists( 'esc_attr' ) ) {
function esc_attr( $text ) {
return attribute_escape( $text );
}
}
// Add functions from WP3.0 for previous WP versions
if ( !function_exists( 'get_search_link' ) ) {
function get_search_link( $query = '' ) {
global $wp_rewrite;
if ( empty($query) )
$search = get_search_query();
else
$search = stripslashes($query);
$permastruct = $wp_rewrite->get_search_permastruct();
if ( empty( $permastruct ) ) {
$link = home_url('?s=' . urlencode($search) );
} else {
$search = urlencode($search);
$search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
$link = str_replace( '%search%', $search, $permastruct );
$link = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $link, 'search' );
}
return apply_filters( 'search_link', $link, $search );
}
}
$wp_recent_searches_widget = new RecentSearchesWidget();
// Show recent searches anywhere in the theme
function rsw_show_recent_searches( $before_list = "<ul>\n<li>", $after_list = "</li>\n</ul>", $between_items = "</li>\n<li>" ) {
global $wp_recent_searches_widget;
$wp_recent_searches_widget->show_recent_searches( $before_list, $after_list, $between_items );
}
} // END
?>
I can't see which part is for clearing up the keywords. Any suggestion? Thanks!
Updated part:
After the clearing history issue solved, this is the next issue i got.
function get_default_options() {
return array(
'title' => '',
'max' => 4, <---it was originally set to 10
'nofollow' => true,
);
}
}
I have set the searched keywords to "4" which was originally set as 10, it should work and display only maximum 4 keywords by right. But, i don't know why the setting seems to follow the very first time i use this plugin. No matter how i tried to set from 0 to 5, the setting never take effect and the keywords there are still displaying as maximum 10 searches. Need help in this!
Any solution?
I found an "archaic" way to do it.
If you search for "store" in the script", at the end of it you'll find the string
update_option( 'recent_searches_widget_data', $data );
If you set $data to 0 (e.g. $data=0;)right before this line and then you try to search something, you'll be able to delete the search history.
Remember to delete the line where you set $data to 0, otherwise the plugin won't start to work again
Edit:
Sorry for the late response, but I have the answers for your question (and also another way to do the previous point).
What I understood is that from the script you can't change after the initialisation the number of elements that have to be shown. When does this happen? When you install the plugin. So you should unzip the folder, change the number of elements in the .php file, zip the folder and install again the plugin.
Not so tricky, but quite long.
Now, the good news.
I don't know if you have ever managed with the database that is behind wp, but there is a table (wp_options) where many part of code that wordpress uses are stored.
Usually here you can also find the data that the plugins set up.
And here we go: in that table there are two rows, called recent_searches_widget and recent_searches_widget_data.
In the first you can find the setting for the number of elements to be shown (its the i:10; that is set by default, if you haven't changed the script yet), in the other you can find the previous searches (and, if you want to delete them, you just need to change the value of the row to 0).
I want to create a new application group in every team project and grant rights to it. What I've done so far is iterating over all projects and creating the group, if not already present:
static void Main( string[] args )
{
m_TfsServer = new TfsTeamProjectCollection(
new Uri( "http://server:port/vdir" ),
System.Net.CredentialCache.DefaultNetworkCredentials,
new UICredentialsProvider( ) );
m_TfsServer.EnsureAuthenticated( );
m_TfsSecurityService = m_TfsServer.GetService<IGroupSecurityService>( );
var structService = m_TfsServer.GetService<ICommonStructureService>( );
foreach ( var p in structService.ListAllProjects( ) )
{
string groupSid;
if ( !GroupExist( p.Uri, GroupName ) )
{
groupSid = m_TfsSecurityService.CreateApplicationGroup(
p.Uri,
GroupName,
GroupDescription );
}
else
{
groupSid = GetApplicationGroupSid( p.Uri, GroupName );
}
Identity userIdentity = m_TfsSecurityService.ReadIdentityFromSource(
SearchFactor.AccountName,
UserName );
if ( !m_TfsSecurityService.IsMember( groupSid, userIdentity.Sid ) )
{
m_TfsSecurityService.AddMemberToApplicationGroup(
groupSid,
userIdentity.Sid );
}
}
}
private static bool GroupExist( string projectUri, string groupName )
{
bool result = false;
Identity[] groups =
m_TfsSecurityService.ListApplicationGroups( projectUri );
foreach ( Identity group in groups )
{
result |= group.SecurityGroup && group.DisplayName.Equals( groupName );
}
return result;
}
private static string GetApplicationGroupSid(
string projectUri,
string groupName)
{
return m_TfsSecurityService.ListApplicationGroups( projectUri )
.Where( g => g.DisplayName.Equals( groupName ) )
.Select( g => g.Sid )
.First( );
}
The only thing left is to grant the "View project-level information" right to the group.
[Edit]
I found something to grant rights using the VersionControlService:
var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection( ServerUri );
tfs.EnsureAuthenticated( );
var vcs = tfs.GetService<VersionControlServer>( );
//vcs.SetPermissions( new SecurityChange[] { } ); ???
But I did not find any documentation how to grant rights to a group, so I'm even not sure whether this solution is the correct approach.
[/Edit]
Has anyone experiences with the TFS rights management or anyone who already granted rights via TFS API?
Not exactly what you're asking for, but it may be similar enough to get you started.
http://blogs.microsoft.co.il/blogs/shair/archive/2009/02/03/tfs-api-part-12-set-security-for-area-iteration.aspx
I have two TCP-server apps that are based on the same code, but for some reason exhibit different behavior and i'm ready to pull my hair out trying to figure out why. The code pattern is as follows:
public class TcpServer
{
public static void Start( bool bService )
{
..
oTcpListnr= new TcpListener( ip, iOutPort );
aTcpClient= new ArrayList( );
bListen= true;
oTcpListnr.Start( );
thOutComm= new Thread( new ThreadStart( AcceptTcpConn ) );
thOutComm.Name= "App-i.AcceptTcpConn";
thOutComm.Start( );
..
}
public static void Stop( )
{
bListen= false;
if( thOutComm != null )
{
thOutComm.Join( iTimeout );
thOutComm= null;
}
if( oTimer != null )
{
oTimer.Change( Timeout.Infinite, Timeout.Infinite );
oTimer.Dispose( );
}
}
public static void AcceptTcpConn( )
{
TcpState oState;
Socket oSocket= null;
while( bListen )
{
try
{
// if( oTcpListnr.Pending( ) )
{
oSocket= oTcpListnr.AcceptSocket( );
oState= new TcpState( oSocket );
if( oSocket.Connected )
{
Utils.PrnLine( "adding tcp: {0}", oSocket.RemoteEndPoint.ToString( ) );
Monitor.Enter( aTcpClient );
aTcpClient.Add( oState );
Monitor.Exit( aTcpClient );
oSocket.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.DontFragment, true );
oSocket.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.DontLinger, true );
// / oSocket.BeginReceive( oState.bData, 0, oState.bData.Length, SocketFlags.None, // no need to read
// / new AsyncCallback( AsyncTcpComm ), oState ); // for output only
}
else
{
Utils.PrnLine( "removing tcp: {0}", oSocket.RemoteEndPoint.ToString( ) );
Monitor.Enter( aTcpClient );
aTcpClient.Remove( oState );
Monitor.Exit( aTcpClient );
}
}
// Thread.Sleep( iTcpWake );
}
#region catch
catch( Exception x )
{
bool b= true;
SocketException se= x as SocketException;
if( se != null )
{
if( se.SocketErrorCode == SocketError.Interrupted )
{
b= false;
if( oSocket != null )
Utils.PrnLine( "TcpConn:\tclosing tcp: {0} ({1})", oSocket.RemoteEndPoint.ToString( ), se.SocketErrorCode );
}
}
if( b )
{
Utils.HandleEx( x );
}
}
#endregion
}
}
}
I omitted exception handling in Start/Stop methods for brevity. Variation in behavior is during program termination: one app shuts down almost immediately while the other gets stuck in oTcpListnr.AcceptSocket( ) call. I know that this is a blocking call, but in that case why does it not present an issue for the 1st app?
Usage of this class cannot be any simpler, e.g. for a command-line tool:
class Program
{
public static void Main( string[] args )
{
TcpServer.Start( false );
Console.Read( );
Console.WriteLine( "\r\nStopping.." );
TcpServer.Stop( );
Console.WriteLine( "\r\nStopped. Press any key to exit.." );
Console.Read( );
}
}
Whether any clients have connected or not does not make a difference, 2nd app always gets stuck.
I found a potential solution (commented lines) by checking TcpListener.Pending( ) prior to .AcceptSocket( ) call, but this immediately affects CPU utilization, therefore an inclusion of smth like Thread.Sleep(.) is a must. Altogether though I'd rather avoid this approach if possible, because of extra connection wait times and CPU utilization (small as it is).
Still, the main question is: what may cause the same exact code to execute differently? Both apps are compiled on .NET 4 Client Profile, x86 (32-bit), no specific optimizations. Thank you in advance for good ideas!
Finally found the root cause: I missed a couple of important lines [hidden in a #region] in the Stop( ) method, which starts the ball rolling. Here's how it should look:
public static void Stop( )
{
bListen= false;
if( thOutComm != null )
{
try
{
oTcpListnr.Stop( );
}
catch( Exception x )
{
Utils.HandleEx( x );
}
thOutComm.Join( iTimeout );
thOutComm= null;
}
if( oTimer != null )
{
oTimer.Change( Timeout.Infinite, Timeout.Infinite );
oTimer.Dispose( );
}
}
The call to TcpListener.Stop( ) kicks out the wait-cycle inside .AcceptSocket( ) with "A blocking operation was interrupted by a call to WSACancelBlockingCall" exception, which is then "normally ignored" (check for SocketError.Interrupted) by the code that i originally had.
Using SubSonic3, I have this generic method (thanks to linq guy, James Curran):
public List<T> GetFromList<T>( List<Guid> _IDs,
Func<T, Guid> GetID,
Func<IQueryable<T>> GetAll )
where T : class, IActiveRecord
{
List<T> rc = null;
var Results =
from item in GetAll( )
where ( _IDs as IEnumerable<Guid> ).Contains( GetID( item ) )
select item;
rc = Results.ToList<T>( );
return rc;
}
It is called with something like
List<Job> jresults = GetFromList( IDList,
item => item.JobID,
( ) => Job.All( ) );
Where IDList is a List of guids that are keys to the table.
When not generic, the linq looks like this and works perfectly. I was quite impressed that SubSonic's linq provider could take this code and turn it into SELECT * FROM Job WHERE JobID IN (a, b, c):
var Results =
from item in Job.All( )
where ( _IDs as IEnumerable<Guid> ).Contains( item.JobID )
select item;
I want to be able to call this method on tables other than Job, with keys other than JobID. The GetAll Func works because it returns the same IQueryable that Job.All( ) does, but GetID throws a run-time exception, "LINQ expression node of type Invoke is not supported". GetID returns a value, but what I really need from it is something that Contains( item.JobID) would recognize as a column name and that the "where" syntax would accept. (I don't show it here, but I have the same problem with orderby.)
Is that possible, with what you know of SubSonic3?
My solution was to pass in the expression that Where needed:
public List<T> GetFromList( List<Guid> _IDs,
Func<IQueryable<T>> GetAll,
Expression<Func<T, bool>> _where )
where T : class, U, IActiveRecord
{
List<T> rc = new List<T>( );
if ( 0 < _IDs.Count )
{
if ( MAX_ITEMS > _IDs.Count )
{
var Results = GetAll( ).Where( _where );
rc = Results.ToList( );
}
else
{
var Results =
from id in _IDs
join item in GetAll( ) on id equals item.KeyValue( )
select item;
rc = Results.ToList( );
}
}
return rc;
}
called by
rc = GetFromList(
IDList,
( ) => Job.All( ),
( item => ( IDList as IEnumerable<Guid> ).Contains( item.JobID ) ) );